Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For NamedLikeContextualKeyword error prone check, don't alert on generated code from an @AutoValue or @AutoOneOf class. #4010

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -81,6 +82,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 ImmutableSet<String> AUTO_PROCESSORS =
ImmutableSet.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 +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)) {
Expand Down Expand Up @@ -153,4 +164,8 @@ private static String getQualifier(
}
}
}

private static boolean isInGeneratedAutoCode(VisitorState state) {
return !Collections.disjoint(ASTHelpers.getGeneratedBy(state), AUTO_PROCESSORS);
}
}
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("-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
Expand Down
Loading