Skip to content

Commit

Permalink
[Enhanced Switch][Null] Missing Null pointer access warning with
Browse files Browse the repository at this point in the history
total/unconditional patterns in case labels

* Fixes #3381
  • Loading branch information
srikanth-sankaran committed Dec 3, 2024
1 parent eaff460 commit 9821164
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -462,13 +462,7 @@ else if ((statement.bits & ASTNode.DocumentedFallthrough) == 0) // the case is n
}
}
private boolean isNullHostile() {
if (this.containsNull)
return false;
if ((this.expression.implicitConversion & TypeIds.UNBOXING) != 0)
return true;
if (this.expression.resolvedType != null && (this.expression.resolvedType.id == T_JavaLangString || this.expression.resolvedType.isEnum()))
return true;
return this.totalPattern == null;
return !this.containsNull && this.expression.resolvedType instanceof ReferenceBinding;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1244,4 +1244,46 @@ static Stuff match(PatternMatching pm, int v) {
""";
runner.runNegativeTest();
}

// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3381
// [Enhanced Switch][Null] Missing Null pointer access warning with total/unconditional patterns
public void testIssue3381() {
Runner runner = getDefaultRunner();
runner.testFiles = new String[] {
"X.java",
"""
import org.eclipse.jdt.annotation.Nullable;
public class X {
void foo() {
@Nullable Integer i = null;
switch (i) {
case Integer ii -> System.out.println();
}
}
void goo() {
@Nullable Integer i = null;
switch (i) {
default -> System.out.println();
}
}
}
"""
};
runner.expectedCompilerLog =
"""
----------
1. ERROR in X.java (at line 6)
switch (i) {
^
Null pointer access: The variable i can only be null at this location
----------
2. ERROR in X.java (at line 12)
switch (i) {
^
Null pointer access: This expression of type Integer is null but requires auto-unboxing
----------
""";
runner.runNegativeTest();
}
}

0 comments on commit 9821164

Please sign in to comment.