Skip to content

Commit

Permalink
For NamedLikeContextualKeyword error prone check, don't alert on gene…
Browse files Browse the repository at this point in the history
…rated code from an @autovalue or @AutoOneOf class.

PiperOrigin-RevId: 547601610
  • Loading branch information
java-team-github-bot authored and Error Prone Team committed Jul 19, 2023
1 parent 563f13c commit 74d2d30
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,6 +83,10 @@ public final class NamedLikeContextualKeyword extends BugChecker
"yield");
private static final Matcher<MethodTree> DISALLOWED_METHOD_NAME_MATCHER =
allOf(not(methodIsConstructor()), methodIsNamed("yield"));
private static final ImmutableList<String> AUTO_PROCESSORS =
ImmutableList.of(
"com.google.auto.value.processor.AutoValueProcessor",
"com.google.auto.value.processor.AutoOneOfProcessor");

private final boolean enableMethodNames;
private final boolean enableClassNames;
Expand All @@ -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)) {
Expand Down Expand Up @@ -153,4 +165,8 @@ private static String getQualifier(
}
}
}

private static boolean isInGeneratedAutoCode(VisitorState state) {
return Iterables.any(ASTHelpers.getGeneratedBy(state), AUTO_PROCESSORS::contains);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 74d2d30

Please sign in to comment.