From dad717432442c27f9168fa485ef15d3ebd64c71b Mon Sep 17 00:00:00 2001 From: Error Prone Team Date: Thu, 20 Jul 2023 12:48:42 -0700 Subject: [PATCH] For NamedLikeContextualKeyword error prone check, don't alert on generated code from an @AutoValue or @AutoOneOf class. PiperOrigin-RevId: 549712144 --- .../NamedLikeContextualKeyword.java | 15 +++++ .../NamedLikeContextualKeywordTest.java | 61 +++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/NamedLikeContextualKeyword.java b/core/src/main/java/com/google/errorprone/bugpatterns/NamedLikeContextualKeyword.java index c8f705aff43..18b3249bf30 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/NamedLikeContextualKeyword.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/NamedLikeContextualKeyword.java @@ -46,6 +46,7 @@ import com.sun.source.util.TreePath; import com.sun.tools.javac.code.Symbol.ClassSymbol; import com.sun.tools.javac.code.Symbol.MethodSymbol; +import java.util.Collections; import javax.inject.Inject; /** @@ -81,6 +82,10 @@ public final class NamedLikeContextualKeyword extends BugChecker "yield"); private static final Matcher DISALLOWED_METHOD_NAME_MATCHER = allOf(not(methodIsConstructor()), methodIsNamed("yield")); + private static final ImmutableSet AUTO_PROCESSORS = + ImmutableSet.of( + "com.google.auto.value.processor.AutoValueProcessor", + "com.google.auto.value.processor.AutoOneOfProcessor"); private final boolean enableMethodNames; private final boolean enableClassNames; @@ -100,6 +105,12 @@ public Description matchMethod(MethodTree tree, VisitorState state) { } MethodSymbol methodSymbol = ASTHelpers.getSymbol(tree); + + // Don't alert if an @Auto... class (safe since reference always qualified). + if (isInGeneratedAutoCode(state)) { + return NO_MATCH; + } + // Don't alert if method is an override (this includes interfaces) if (!streamSuperMethods(methodSymbol, state.getTypes()).findAny().isPresent() && DISALLOWED_METHOD_NAME_MATCHER.matches(tree, state)) { @@ -153,4 +164,8 @@ private static String getQualifier( } } } + + private static boolean isInGeneratedAutoCode(VisitorState state) { + return !Collections.disjoint(ASTHelpers.getGeneratedBy(state), AUTO_PROCESSORS); + } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/NamedLikeContextualKeywordTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/NamedLikeContextualKeywordTest.java index 6eb1ebbb6d5..da56f24f412 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/NamedLikeContextualKeywordTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/NamedLikeContextualKeywordTest.java @@ -69,6 +69,67 @@ public void staticMethodName_error() { .doTest(); } + @Test + public void autoOneOfMethodName_noError() { + helper + .addSourceLines( + "Test.java", + "import javax.annotation.processing.Generated;", + "@Generated(\"com.google.auto.value.processor.AutoOneOfProcessor\")", + "class Test {", + " static Throwable foo;", + " public Test() {", + " }", + " ", + " public static void yield() { ", + " foo = new NullPointerException(\"uh oh\");", + " }", + "}") + .setArgs("-XepOpt:NamedLikeContextualKeyword:EnableMethodNames") + .doTest(); + } + + @Test + public void autoValueMethodName_noError() { + helper + .addSourceLines( + "Test.java", + "import javax.annotation.processing.Generated;", + "@Generated(\"com.google.auto.value.processor.AutoValueProcessor\")", + "class Test {", + " static Throwable foo;", + " public Test() {", + " }", + " ", + " public static void yield() { ", + " foo = new NullPointerException(\"uh oh\");", + " }", + "}") + .setArgs("-XepOpt:NamedLikeContextualKeyword:EnableMethodNames") + .doTest(); + } + + @Test + public void generatedButNotAuto_error() { + helper + .addSourceLines( + "Test.java", + "import javax.annotation.processing.Generated;", + "@Generated(\"com.google.foo.Bar\")", + "class Test {", + " static Throwable foo;", + " public Test() {", + " }", + " ", + " // BUG: Diagnostic contains: [NamedLikeContextualKeyword]", + " public static void yield() { ", + " foo = new NullPointerException(\"uh oh\");", + " }", + "}") + .setArgs("-XepOpt:NamedLikeContextualKeyword:EnableMethodNames") + .doTest(); + } + @Test public void className_error() { helper