Skip to content

False Negative : Statically non-null expressions are not reported by RedundantNullCheck #5674

@Carlson-JLQ

Description

@Carlson-JLQ

False Negative : Statically non-null expressions are not reported by RedundantNullCheck

Affects errorprone Version:

2.48.0

Checker:

RedundantNullCheck

Description:

The official RedundantNullCheck description says it reports null checks on expressions that are statically determined to be non-null. The following samples all fit that rule: final fields initialized with non-null literals, expressions proven non-null by control flow, and values whose construction makes null impossible.

Code Sample demonstrating the issue:

// PosCase2
private final String NAME = "constant";
if (NAME != null) {
    System.out.println("always true");
}

// PosCase3
private final String NAME = "constant";
if (Objects.isNull(NAME)) {
    System.out.println("impossible");
}

// PosCase4
Object obj = "string";
String casted = (String) obj;
if (casted == null) {
    System.out.println("impossible");
}

// PosCase6 / PosCase7
String result = flag ? "yes" : "no";
if (result == null) {
    System.out.println("impossible");
}
if (Objects.nonNull(result)) {
    System.out.println("always true");
}

// PosCase8 / PosCase9
if (s != null) {
    if (s == null) {
        System.out.println("impossible");
    }
}
if (obj != null) {
    if (Objects.nonNull(obj)) {
        System.out.println("always true");
    }
}

These examples are all redundant for the same reason: the checked expression is already known to be non-null, or already known to satisfy the opposite nullness fact, before the reported condition is evaluated.

Expected outcome:

Error Prone should report these redundant null checks, but it doesn't. This is a false-negative.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions