-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't crash on switch expressions or switch statements with arrow cases
- Loading branch information
Showing
10 changed files
with
418 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// @below-java17-jdk-skip-test | ||
public class NullnessSwitchArrows { | ||
public enum Day { | ||
SUNDAY, | ||
MONDAY, | ||
TUESDAY, | ||
WEDNESDAY, | ||
THURSDAY, | ||
FRIDAY, | ||
SATURDAY; | ||
} | ||
|
||
void method1() { | ||
Object o; | ||
Day day = Day.WEDNESDAY; | ||
switch (day) { | ||
case MONDAY, FRIDAY, SUNDAY -> o = "hello"; | ||
case TUESDAY -> o = null; | ||
case THURSDAY, SATURDAY -> o = "hello"; | ||
case WEDNESDAY -> o = "hello"; | ||
default -> throw new IllegalStateException("Invalid day: " + day); | ||
} | ||
|
||
// :: error: (dereference.of.nullable) | ||
o.toString(); | ||
} | ||
|
||
void method2() { | ||
Object o; | ||
Day day = Day.WEDNESDAY; | ||
switch (day) { | ||
case MONDAY, FRIDAY, SUNDAY -> o = "hello"; | ||
case TUESDAY -> o = "hello"; | ||
case THURSDAY, SATURDAY -> o = "hello"; | ||
case WEDNESDAY -> o = "hello"; | ||
default -> throw new IllegalStateException("Invalid day: " + day); | ||
} | ||
|
||
// TODO: this is a false positive. It works for case: statements; see below. | ||
// :: error: (dereference.of.nullable) | ||
o.toString(); | ||
} | ||
|
||
void method2b() { | ||
Object o; | ||
Day day = Day.WEDNESDAY; | ||
switch (day) { | ||
case MONDAY, FRIDAY, SUNDAY: | ||
o = "hello"; | ||
break; | ||
case TUESDAY: | ||
o = "hello"; | ||
break; | ||
case THURSDAY, SATURDAY: | ||
o = "hello"; | ||
break; | ||
case WEDNESDAY: | ||
o = "hello"; | ||
break; | ||
default: | ||
throw new IllegalStateException("Invalid day: " + day); | ||
} | ||
|
||
o.toString(); | ||
} | ||
|
||
void method3() { | ||
Object o; | ||
Day day = Day.WEDNESDAY; | ||
switch (day) { | ||
case MONDAY, FRIDAY, SUNDAY -> o = "hello"; | ||
case TUESDAY -> o = "hello"; | ||
case THURSDAY, SATURDAY -> o = "hello"; | ||
case WEDNESDAY -> o = "hello"; | ||
default -> o = "hello"; | ||
} | ||
|
||
o.toString(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
checker/tests/nullness/java17/NullnessSwitchExpressions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// @below-java17-jdk-skip-test | ||
public class NullnessSwitchExpressions { | ||
public enum Day { | ||
SUNDAY, | ||
MONDAY, | ||
TUESDAY, | ||
WEDNESDAY, | ||
THURSDAY, | ||
FRIDAY, | ||
SATURDAY; | ||
} | ||
|
||
void method1() { | ||
Day day = Day.WEDNESDAY; | ||
Object o = | ||
switch (day) { | ||
case MONDAY, FRIDAY, SUNDAY -> "hello"; | ||
case TUESDAY -> null; | ||
case THURSDAY, SATURDAY -> "hello"; | ||
case WEDNESDAY -> "hello"; | ||
default -> throw new IllegalStateException("Invalid day: " + day); | ||
}; | ||
|
||
// :: error: (dereference.of.nullable) | ||
o.toString(); | ||
} | ||
|
||
void method2() { | ||
Day day = Day.WEDNESDAY; | ||
Object o = | ||
switch (day) { | ||
case MONDAY, FRIDAY, SUNDAY -> "hello"; | ||
case TUESDAY -> "hello"; | ||
case THURSDAY, SATURDAY -> "hello"; | ||
case WEDNESDAY -> "hello"; | ||
default -> throw new IllegalStateException("Invalid day: " + day); | ||
}; | ||
|
||
// TODO: This is a false positive. | ||
// :: error: (dereference.of.nullable) | ||
o.toString(); | ||
} | ||
|
||
void method3() { | ||
Day day = Day.WEDNESDAY; | ||
Object o = | ||
switch (day) { | ||
case MONDAY, FRIDAY, SUNDAY -> "hello"; | ||
case TUESDAY -> "hello"; | ||
case THURSDAY, SATURDAY -> { | ||
String s = null; | ||
if (day == Day.THURSDAY) { | ||
s = "hello"; | ||
// TODO: This is a false positive. | ||
// :: error: (dereference.of.nullable) | ||
s.toString(); | ||
} | ||
yield s; | ||
} | ||
case WEDNESDAY -> "hello"; | ||
default -> throw new IllegalStateException("Invalid day: " + day); | ||
}; | ||
|
||
// :: error: (dereference.of.nullable) | ||
o.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.