Skip to content

Commit

Permalink
Make MemoizeConstantVisitorStateLookups check suppressible
Browse files Browse the repository at this point in the history
It's nice to be able to suppress this check, e.g., while you're initially prototyping a checker.  I based my fix on the fix for #1650; hope it's the right approach.

Fixes #3690

COPYBARA_INTEGRATE_REVIEW=#3690 from msridhar:make-memoizeconstantvisitorstatelookups-suppressible 0adb67b
PiperOrigin-RevId: 500002923
  • Loading branch information
msridhar authored and Error Prone Team committed Jan 6, 2023
1 parent bb2563e commit f36a502
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
Expand Down Expand Up @@ -157,9 +156,9 @@ private static final class CallSite {
}
}

private static ImmutableList<CallSite> findConstantLookups(ClassTree tree, VisitorState state) {
private ImmutableList<CallSite> findConstantLookups(ClassTree tree, VisitorState state) {
ImmutableList.Builder<CallSite> result = ImmutableList.builder();
new TreeScanner<Void, Void>() {
new SuppressibleTreePathScanner<Void, Void>(state) {
@Override
public Void visitMethodInvocation(MethodInvocationTree tree, Void unused) {
if (CONSTANT_LOOKUP.matches(tree, state)) {
Expand All @@ -186,7 +185,7 @@ private void handleConstantLookup(MethodInvocationTree tree) {
}
}
}
}.scan(tree, null);
}.scan(state.getPath(), null);
return result.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,31 @@ public void negative_doesntMemoizeTwice() {
.expectUnchanged()
.doTest();
}

@Test
public void testSuppressWarnings() {
compilationTestHelper
.addSourceLines(
"Test.java",
"import com.google.errorprone.VisitorState;",
"import com.sun.tools.javac.code.Type;",
"import com.sun.tools.javac.util.Name;",
"class Test {",
" @SuppressWarnings(\"MemoizeConstantVisitorStateLookups\")",
" public Test(VisitorState state) {",
" Name className = state.getName(\"java.lang.Class\");",
" }",
" @SuppressWarnings(\"MemoizeConstantVisitorStateLookups\")",
" public void testMethod(VisitorState state) {",
" Name className = state.getName(\"java.lang.Class\");",
" }",
" @SuppressWarnings(\"MemoizeConstantVisitorStateLookups\")",
" class InnerClass {",
" void innerMethod(VisitorState state) {",
" Name className = state.getName(\"java.lang.Class\");",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit f36a502

Please sign in to comment.