From c83ba54d47ef10c0fb1a4cdbe675c88233926c69 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Thu, 2 Nov 2023 15:00:47 -0700 Subject: [PATCH] ASTHelpersSuggestions: don't suggest ASTHelpers inside ASTHelpers PiperOrigin-RevId: 578989135 --- .../bugpatterns/ASTHelpersSuggestions.java | 9 +++++++++ .../bugpatterns/ASTHelpersSuggestionsTest.java | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ASTHelpersSuggestions.java b/core/src/main/java/com/google/errorprone/bugpatterns/ASTHelpersSuggestions.java index b75e4e485e4..4ce0b451d5f 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ASTHelpersSuggestions.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ASTHelpersSuggestions.java @@ -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; @@ -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; @@ -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(); diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/ASTHelpersSuggestionsTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/ASTHelpersSuggestionsTest.java index cfc4ad9e499..2da73508f3c 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/ASTHelpersSuggestionsTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/ASTHelpersSuggestionsTest.java @@ -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(); + } }