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

Various fixes for generating @SuppressWarnings #271

Merged
merged 4 commits into from Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 16 additions & 6 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Expand Up @@ -1328,7 +1328,6 @@ private Description checkCastToNonNullTakesNullable(
// Initializer block
isInitializer = true;
}
MethodTree enclosingMethod = ASTHelpers.findEnclosingNode(state.getPath(), MethodTree.class);
if (!isInitializer && !mayBeNullExpr(state, actual)) {
String message =
"passing known @NonNull parameter '"
Expand Down Expand Up @@ -1961,8 +1960,8 @@ private Description matchDereference(
*/
private Description createErrorDescription(
MessageTypes errorType, Tree errorLocTree, String message, TreePath path) {
MethodTree enclosingMethod = ASTHelpers.findEnclosingNode(path, MethodTree.class);
return createErrorDescription(errorType, errorLocTree, message, enclosingMethod);
Tree enclosingSuppressTree = findEnclosingSuppressTree(path);
return createErrorDescription(errorType, errorLocTree, message, enclosingSuppressTree);
}

/**
Expand Down Expand Up @@ -2033,10 +2032,21 @@ private Description createErrorDescriptionForNullAssignment(
String message,
@Nullable Tree suggestTreeIfCastToNonNull,
@Nullable TreePath suggestTreePathIfSuppression) {
MethodTree enclosingMethod =
ASTHelpers.findEnclosingNode(suggestTreePathIfSuppression, MethodTree.class);
Tree enclosingSuppressTree = findEnclosingSuppressTree(suggestTreePathIfSuppression);
return createErrorDescriptionForNullAssignment(
errorType, errorLocTree, message, suggestTreeIfCastToNonNull, enclosingMethod);
errorType, errorLocTree, message, suggestTreeIfCastToNonNull, enclosingSuppressTree);
}

private Tree findEnclosingSuppressTree(@Nullable TreePath suggestTreePathIfSuppression) {
Tree enclosingSuppressNode =
ASTHelpers.findEnclosingNode(suggestTreePathIfSuppression, MethodTree.class);
if (enclosingSuppressNode == null) {
// this can happen, e.g., if the deref is on the RHS of a field initialization expression.
// in this case, look for the field declaration
enclosingSuppressNode =
ASTHelpers.findEnclosingNode(suggestTreePathIfSuppression, VariableTree.class);
}
return enclosingSuppressNode;
}

/**
Expand Down
Expand Up @@ -24,7 +24,6 @@

import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.ErrorProneFlags;
import java.io.IOException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -57,7 +56,7 @@ public void setup() {
}

@Test
public void suggestSuppressionWithComment() throws IOException {
public void suggestSuppressionWithComment() {
BugCheckerRefactoringTestHelper bcr =
BugCheckerRefactoringTestHelper.newInstance(new NullAway(flags), getClass());

Expand All @@ -82,7 +81,7 @@ public void suggestSuppressionWithComment() throws IOException {
}

@Test
public void suggestSuppressionWithoutComment() throws IOException {
public void suggestSuppressionWithoutComment() {
BugCheckerRefactoringTestHelper bcr =
BugCheckerRefactoringTestHelper.newInstance(
new NullAway(flagsNoAutoFixSuppressionComment), getClass());
Expand All @@ -106,4 +105,72 @@ public void suggestSuppressionWithoutComment() throws IOException {
"}");
bcr.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
}

@Test
public void suggestSuppressionFieldLambdaDeref() {
BugCheckerRefactoringTestHelper bcr =
BugCheckerRefactoringTestHelper.newInstance(
new NullAway(flagsNoAutoFixSuppressionComment), getClass());

bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath());
bcr.addInputLines(
"Test.java",
"package com.uber;",
"import javax.annotation.Nullable;",
"class Test {",
" @Nullable private Object foo;",
" private final Runnable runnable =",
" () -> {",
" foo.toString();",
" };",
"}")
.addOutputLines(
"out/Test.java",
"package com.uber;",
"import javax.annotation.Nullable;",
"class Test {",
" @Nullable private Object foo;",
" @SuppressWarnings(\"NullAway\")",
" private final Runnable runnable =",
" () -> {",
" foo.toString();",
" };",
"}");
bcr.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
}

@Test
public void suggestSuppressionFieldLambdaUnbox() {
BugCheckerRefactoringTestHelper bcr =
BugCheckerRefactoringTestHelper.newInstance(
new NullAway(flagsNoAutoFixSuppressionComment), getClass());

bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath());
bcr.addInputLines(
"Test.java",
"package com.uber;",
"import javax.annotation.Nullable;",
"class Test {",
" @Nullable private Integer foo;",
" static int id(int x) { return x; }",
" private final Runnable runnable =",
" () -> {",
" id(foo);",
" };",
"}")
.addOutputLines(
"out/Test.java",
"package com.uber;",
"import javax.annotation.Nullable;",
"class Test {",
" @Nullable private Integer foo;",
" static int id(int x) { return x; }",
" @SuppressWarnings(\"NullAway\")",
" private final Runnable runnable =",
" () -> {",
" id(foo);",
" };",
"}");
bcr.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
}
}