Skip to content

Commit

Permalink
ASTHelpersSuggestions: don't suggest ASTHelpers inside ASTHelpers
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 578989135
  • Loading branch information
cushon authored and Error Prone Team committed Nov 2, 2023
1 parent bd43f40 commit c83ba54
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod;
import static com.google.errorprone.util.ASTHelpers.findEnclosingNode;
import static com.google.errorprone.util.ASTHelpers.getReceiver;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.isSameType;
import static com.google.errorprone.util.ASTHelpers.isSubtype;
import static com.google.errorprone.util.ASTHelpers.outermostClass;

import com.google.common.collect.ImmutableMap;
import com.google.errorprone.BugPattern;
Expand All @@ -34,8 +36,10 @@
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.suppliers.Supplier;
import com.google.errorprone.suppliers.Suppliers;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Type;

Expand Down Expand Up @@ -70,6 +74,11 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
if (receiver == null) {
return NO_MATCH;
}
ClassSymbol outermost =
outermostClass(getSymbol(findEnclosingNode(state.getPath(), ClassTree.class)));
if (outermost.getQualifiedName().contentEquals("com.google.errorprone.util.ASTHelpers")) {
return NO_MATCH;
}
if (SYMBOL.matches(tree, state)) {
MethodSymbol sym = getSymbol(tree);
String name = sym.getSimpleName().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,22 @@ public void symbolGetEnclosedElements() {
"jdk.compiler/com.sun.tools.javac.code", "jdk.compiler/com.sun.tools.javac.util")
.doTest();
}

@Test
public void selfMatch() {
testHelper
.addInputLines(
"ASTHelpers.java",
"package com.google.errorprone.util;",
"import com.sun.tools.javac.code.Symbol;",
"public final class ASTHelpers {",
" public static boolean isStatic(Symbol symbol) {",
" return symbol.isStatic();",
" }",
"}")
.expectUnchanged()
.addModules(
"jdk.compiler/com.sun.tools.javac.code", "jdk.compiler/com.sun.tools.javac.util")
.doTest();
}
}

0 comments on commit c83ba54

Please sign in to comment.