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 c8f705aff43c..715380c343f2 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/NamedLikeContextualKeyword.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/NamedLikeContextualKeyword.java @@ -27,7 +27,9 @@ import static com.google.errorprone.util.ASTHelpers.getSymbol; import static com.google.errorprone.util.ASTHelpers.streamSuperMethods; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; import com.google.errorprone.BugPattern; import com.google.errorprone.ErrorProneFlags; import com.google.errorprone.VisitorState; @@ -81,6 +83,10 @@ public final class NamedLikeContextualKeyword extends BugChecker "yield"); private static final Matcher DISALLOWED_METHOD_NAME_MATCHER = allOf(not(methodIsConstructor()), methodIsNamed("yield")); + private static final ImmutableList AUTO_PROCESSORS = + ImmutableList.of( + "com.google.auto.value.processor.AutoValueProcessor", + "com.google.auto.value.processor.AutoOneOfProcessor"); private final boolean enableMethodNames; private final boolean enableClassNames; @@ -100,6 +106,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 +165,8 @@ private static String getQualifier( } } } + + private static boolean isInGeneratedAutoCode(VisitorState state) { + return Iterables.any(ASTHelpers.getGeneratedBy(state), AUTO_PROCESSORS::contains); + } } 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 6eb1ebbb6d54..6ab95c19d38f 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(ImmutableList.of("-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(ImmutableList.of("-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(ImmutableList.of("-XepOpt:NamedLikeContextualKeyword:EnableMethodNames")) + .doTest(); + } + @Test public void className_error() { helper