Skip to content

Commit f36a502

Browse files
msridharError Prone Team
authored andcommitted
Make MemoizeConstantVisitorStateLookups check suppressible
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
1 parent bb2563e commit f36a502

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

core/src/main/java/com/google/errorprone/bugpatterns/MemoizeConstantVisitorStateLookups.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import com.sun.source.tree.CompilationUnitTree;
4343
import com.sun.source.tree.ExpressionTree;
4444
import com.sun.source.tree.MethodInvocationTree;
45-
import com.sun.source.util.TreeScanner;
4645
import com.sun.tools.javac.code.Symbol.MethodSymbol;
4746
import com.sun.tools.javac.code.Symbol.TypeSymbol;
4847
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
@@ -157,9 +156,9 @@ private static final class CallSite {
157156
}
158157
}
159158

160-
private static ImmutableList<CallSite> findConstantLookups(ClassTree tree, VisitorState state) {
159+
private ImmutableList<CallSite> findConstantLookups(ClassTree tree, VisitorState state) {
161160
ImmutableList.Builder<CallSite> result = ImmutableList.builder();
162-
new TreeScanner<Void, Void>() {
161+
new SuppressibleTreePathScanner<Void, Void>(state) {
163162
@Override
164163
public Void visitMethodInvocation(MethodInvocationTree tree, Void unused) {
165164
if (CONSTANT_LOOKUP.matches(tree, state)) {
@@ -186,7 +185,7 @@ private void handleConstantLookup(MethodInvocationTree tree) {
186185
}
187186
}
188187
}
189-
}.scan(tree, null);
188+
}.scan(state.getPath(), null);
190189
return result.build();
191190
}
192191

core/src/test/java/com/google/errorprone/bugpatterns/MemoizeConstantVisitorStateLookupsTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,31 @@ public void negative_doesntMemoizeTwice() {
162162
.expectUnchanged()
163163
.doTest();
164164
}
165+
166+
@Test
167+
public void testSuppressWarnings() {
168+
compilationTestHelper
169+
.addSourceLines(
170+
"Test.java",
171+
"import com.google.errorprone.VisitorState;",
172+
"import com.sun.tools.javac.code.Type;",
173+
"import com.sun.tools.javac.util.Name;",
174+
"class Test {",
175+
" @SuppressWarnings(\"MemoizeConstantVisitorStateLookups\")",
176+
" public Test(VisitorState state) {",
177+
" Name className = state.getName(\"java.lang.Class\");",
178+
" }",
179+
" @SuppressWarnings(\"MemoizeConstantVisitorStateLookups\")",
180+
" public void testMethod(VisitorState state) {",
181+
" Name className = state.getName(\"java.lang.Class\");",
182+
" }",
183+
" @SuppressWarnings(\"MemoizeConstantVisitorStateLookups\")",
184+
" class InnerClass {",
185+
" void innerMethod(VisitorState state) {",
186+
" Name className = state.getName(\"java.lang.Class\");",
187+
" }",
188+
" }",
189+
"}")
190+
.doTest();
191+
}
165192
}

0 commit comments

Comments
 (0)