Skip to content

Commit

Permalink
Make ConstantPatternCompile suppressible.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 487181889
  • Loading branch information
graememorgan authored and Error Prone Team committed Nov 9, 2022
1 parent 4e12b54 commit 7d5048c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import com.sun.source.tree.VariableTree;
import com.sun.source.tree.WhileLoopTree;
import com.sun.source.tree.WildcardTree;
import com.sun.source.util.TreePath;
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.code.Symbol;
Expand Down Expand Up @@ -561,15 +562,18 @@ public SuppressibleTreePathScanner(VisitorState state) {

@Override
public A scan(Tree tree, B b) {
return suppressed(tree) ? null : super.scan(tree, b);
}

@Override
public A scan(TreePath treePath, B b) {
return suppressed(treePath.getLeaf()) ? null : super.scan(treePath, b);
}

private boolean suppressed(Tree tree) {
boolean isSuppressible =
tree instanceof ClassTree || tree instanceof MethodTree || tree instanceof VariableTree;
if (isSuppressible) {
boolean suppressed = state != null ? isSuppressed(tree, state) : isSuppressed(tree);
if (suppressed) {
return null;
}
}
return super.scan(tree, b);
return isSuppressible && isSuppressed(tree, state);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Description matchClass(ClassTree classTree, VisitorState state) {
SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
Tree[] firstHit = new Tree[1];
for (Tree member : classTree.getMembers()) {
new TreePathScanner<Void, Void>() {
new SuppressibleTreePathScanner<Void, Void>(state) {
@Override
public Void visitClass(ClassTree node, Void unused) {
// Don't descend into nested classes - we'll visit them later
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,21 @@ public void withinList_noFinding() {
"}")
.doTest();
}

@Test
public void suppressible() {
testHelper
.addInputLines(
"in/Test.java",
"import java.util.regex.Matcher;",
"import java.util.regex.Pattern;",
"class Test {",
" @SuppressWarnings(\"ConstantPatternCompile\")",
" boolean isCar(String input) {",
" return Pattern.compile(\"car\").matcher(input).matches();",
" }",
"}")
.expectUnchanged()
.doTest();
}
}

0 comments on commit 7d5048c

Please sign in to comment.