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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -57,7 +56,7 @@ public void setup() { | |
} | ||
|
||
@Test | ||
public void suggestSuppressionWithComment() throws IOException { | ||
public void suggestSuppressionWithComment() { | ||
BugCheckerRefactoringTestHelper bcr = | ||
BugCheckerRefactoringTestHelper.newInstance(new NullAway(flags), getClass()); | ||
|
||
|
@@ -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()); | ||
|
@@ -106,4 +105,227 @@ 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); | ||
} | ||
|
||
@Test | ||
public void suggestSuppressionFieldLambdaAssignment() { | ||
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 =", | ||
" () -> {", | ||
" int x = foo + 1;", | ||
" };", | ||
"}") | ||
.addOutputLines( | ||
"out/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 =", | ||
" () -> {", | ||
" @SuppressWarnings(\"NullAway\")", | ||
" int x = foo + 1;", | ||
" };", | ||
"}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you assign a lambda within a method, where does this put the suppression? And, does that suppression work? e.g.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a test case. For the |
||
bcr.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
|
||
@Test | ||
public void suggestLambdaAssignInMethod() { | ||
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;", | ||
" @Nullable private java.util.function.Function<Object, Integer> f;", | ||
" void m1() {", | ||
" f = (x) -> { return foo + 1; };", | ||
" }", | ||
" void m2() {", | ||
" java.util.function.Function<Object,Integer> g = (x) -> { return foo + 1; };", | ||
" }", | ||
"}") | ||
.addOutputLines( | ||
"out/Test.java", | ||
"package com.uber;", | ||
"import javax.annotation.Nullable;", | ||
"class Test {", | ||
" @Nullable private Integer foo;", | ||
" @Nullable private java.util.function.Function<Object, Integer> f;", | ||
" @SuppressWarnings(\"NullAway\")", | ||
" void m1() {", | ||
" f = (x) -> { return foo + 1; };", | ||
" }", | ||
" void m2() {", | ||
" @SuppressWarnings(\"NullAway\")", | ||
" java.util.function.Function<Object,Integer> g = (x) -> { return foo + 1; };", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
" }", | ||
"}"); | ||
bcr.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
|
||
@Test | ||
public void suppressMethodRefOverrideParam() { | ||
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 {", | ||
" static interface I {", | ||
" public void foo(@Nullable Object o);", | ||
" }", | ||
" static void biz(Object p) {}", | ||
" static void callFoo(I i) { i.foo(null); }", | ||
" static void bar() {", | ||
" callFoo(Test::biz);", | ||
" }", | ||
"}") | ||
.addOutputLines( | ||
"out/Test.java", | ||
"package com.uber;", | ||
"import javax.annotation.Nullable;", | ||
"class Test {", | ||
" static interface I {", | ||
" public void foo(@Nullable Object o);", | ||
" }", | ||
" static void biz(Object p) {}", | ||
" static void callFoo(I i) { i.foo(null); }", | ||
" @SuppressWarnings(\"NullAway\")", | ||
" static void bar() {", | ||
" callFoo(Test::biz);", | ||
" }", | ||
"}"); | ||
bcr.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
|
||
@Test | ||
public void suppressMethodRefOverrideReturn() { | ||
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 {", | ||
" static interface I {", | ||
" public Object foo();", | ||
" }", | ||
" @Nullable", | ||
" static Object biz() { return null; }", | ||
" static void callFoo(I i) { i.foo(); }", | ||
" static void bar() {", | ||
" callFoo(Test::biz);", | ||
" }", | ||
"}") | ||
.addOutputLines( | ||
"out/Test.java", | ||
"package com.uber;", | ||
"import javax.annotation.Nullable;", | ||
"class Test {", | ||
" static interface I {", | ||
" public Object foo();", | ||
" }", | ||
" @Nullable", | ||
" static Object biz() { return null; }", | ||
" static void callFoo(I i) { i.foo(); }", | ||
" @SuppressWarnings(\"NullAway\")", | ||
" static void bar() {", | ||
" callFoo(Test::biz);", | ||
" }", | ||
"}"); | ||
bcr.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this iterate over the full path even if the suppressible node is close? Or is it a lazy iterator / generator?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so. From what I can see it bottoms out in an iterator and goes one by one checking the predicate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry by "I think so" I meant that I think it's lazy