Skip to content

Commit

Permalink
ClassCanBeStatic: Ignore if annotated with @Keep
Browse files Browse the repository at this point in the history
Replace the check for the `@Nested` annotation with a check for `@Keep`,
and consider `@Nested` to be `@Keep`-annotated.
  • Loading branch information
ljrmorgan committed Jan 10, 2023
1 parent 60fe183 commit 3a5bd67
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,9 @@ public static boolean shouldKeep(Tree tree) {
* though they are.
*/
private static final ImmutableSet<String> ANNOTATIONS_CONSIDERED_KEEP =
ImmutableSet.of("org.apache.beam.sdk.transforms.DoFn.ProcessElement");
ImmutableSet.of(
"org.apache.beam.sdk.transforms.DoFn.ProcessElement",
"org.junit.jupiter.api.Nested");

private static final String USED_REFLECTIVELY = "UsedReflectively";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
tags = {StandardTags.STYLE, StandardTags.PERFORMANCE})
public class ClassCanBeStatic extends BugChecker implements ClassTreeMatcher {

private static final String JUNIT_NESTED_ANNOTATION = "org.junit.jupiter.api.Nested";
private static final String REFASTER_ANNOTATION =
"com.google.errorprone.refaster.annotation.BeforeTemplate";

Expand Down Expand Up @@ -78,7 +77,7 @@ public Description matchClass(ClassTree tree, VisitorState state) {
if (CanBeStaticAnalyzer.referencesOuter(tree, currentClass, state)) {
return NO_MATCH;
}
if (hasAnnotation(currentClass, JUNIT_NESTED_ANNOTATION, state)) {
if (ASTHelpers.shouldKeep(tree)) {
return NO_MATCH;
}
if (tree.getMembers().stream().anyMatch(m -> hasAnnotation(m, REFASTER_ANNOTATION, state))) {
Expand Down

0 comments on commit 3a5bd67

Please sign in to comment.