Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable switch expression support #300

Merged
merged 1 commit into from Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions nullaway/src/main/java/com/uber/nullaway/ErrorBuilder.java
Expand Up @@ -100,6 +100,7 @@ public Description createErrorDescription(
case RETURN_NULLABLE:
case PASS_NULLABLE:
case ASSIGN_FIELD_NULLABLE:
case SWITCH_EXPRESSION_NULLABLE:
if (config.getCastToNonNullMethod() != null) {
builder = addCastToNonNullFix(suggestTree, builder);
} else {
Expand Down
3 changes: 2 additions & 1 deletion nullaway/src/main/java/com/uber/nullaway/ErrorMessage.java
Expand Up @@ -51,6 +51,7 @@ public enum MessageTypes {
NULLABLE_VARARGS_UNSUPPORTED,
ANNOTATION_VALUE_INVALID,
CAST_TO_NONNULL_ARG_NONNULL,
GET_ON_EMPTY_OPTIONAL;
GET_ON_EMPTY_OPTIONAL,
SWITCH_EXPRESSION_NULLABLE
}
}
27 changes: 26 additions & 1 deletion nullaway/src/main/java/com/uber/nullaway/NullAway.java
Expand Up @@ -72,6 +72,7 @@
import com.sun.source.tree.ParenthesizedTree;
import com.sun.source.tree.ReturnTree;
import com.sun.source.tree.StatementTree;
import com.sun.source.tree.SwitchTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.TryTree;
import com.sun.source.tree.TypeCastTree;
Expand Down Expand Up @@ -167,7 +168,8 @@ public class NullAway extends BugChecker
BugChecker.LambdaExpressionTreeMatcher,
BugChecker.IdentifierTreeMatcher,
BugChecker.MemberReferenceTreeMatcher,
BugChecker.CompoundAssignmentTreeMatcher {
BugChecker.CompoundAssignmentTreeMatcher,
BugChecker.SwitchTreeMatcher {

static final String INITIALIZATION_CHECK_NAME = "NullAway.Init";

Expand Down Expand Up @@ -495,6 +497,29 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
return Description.NO_MATCH;
}

@Override
public Description matchSwitch(SwitchTree tree, VisitorState state) {
if (!matchWithinClass) {
return Description.NO_MATCH;
}

ExpressionTree switchExpression = tree.getExpression();
if (switchExpression instanceof ParenthesizedTree) {
switchExpression = ((ParenthesizedTree) switchExpression).getExpression();
}

if (mayBeNullExpr(state, switchExpression)) {
final String message = "switch expression " + switchExpression.toString() + " is @Nullable";
ErrorMessage errorMessage =
new ErrorMessage(MessageTypes.SWITCH_EXPRESSION_NULLABLE, message);

return errorBuilder.createErrorDescription(
errorMessage, switchExpression, buildDescription(switchExpression));
}

return Description.NO_MATCH;
}

/**
* checks that an overriding method does not override a {@code @Nullable} parameter with a
* {@code @NonNull} parameter
Expand Down
51 changes: 51 additions & 0 deletions nullaway/src/test/java/com/uber/nullaway/NullAwayTest.java
Expand Up @@ -966,6 +966,57 @@ public void supportObjectsIsNull() {
.doTest();
}

@Test
public void supportSwitchExpression() {
compilationHelper
.addSourceLines(
"TestPositive.java",
"package com.uber;",
"import javax.annotation.Nullable;",
"enum Level {",
" HIGH, MEDIUM, LOW }",
"class TestPositive {",
" void foo(@Nullable Integer s) {",
" // BUG: Diagnostic contains: switch expression s is @Nullable",
" switch(s) {",
" case 5: break;",
" }",
" String x = null;",
" // BUG: Diagnostic contains: switch expression x is @Nullable",
" switch(x) {",
" default: break;",
" }",
" Level level = null;",
" // BUG: Diagnostic contains: switch expression level is @Nullable",
" switch (level) {",
" default: break; }",
" }",
"}")
.addSourceLines(
"TestNegative.java",
"package com.uber;",
"import javax.annotation.Nullable;",
"class TestNegative {",
" void foo(Integer s, short y) {",
" switch(s) {",
" case 5: break;",
" }",
" String x = \"irrelevant\";",
" switch(x) {",
" default: break;",
" }",
" switch(y) {",
" default: break;",
" }",
" Level level = Level.HIGH;",
" switch (level) {",
" default: break;",
" }",
" }",
"}")
.doTest();
}

@Test
public void defaultPermissiveOnUnannotated() {
compilationHelper
Expand Down