Skip to content

Commit 3a5bd67

Browse files
committed
ClassCanBeStatic: Ignore if annotated with @Keep
Replace the check for the `@Nested` annotation with a check for `@Keep`, and consider `@Nested` to be `@Keep`-annotated.
1 parent 60fe183 commit 3a5bd67

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,9 @@ public static boolean shouldKeep(Tree tree) {
10411041
* though they are.
10421042
*/
10431043
private static final ImmutableSet<String> ANNOTATIONS_CONSIDERED_KEEP =
1044-
ImmutableSet.of("org.apache.beam.sdk.transforms.DoFn.ProcessElement");
1044+
ImmutableSet.of(
1045+
"org.apache.beam.sdk.transforms.DoFn.ProcessElement",
1046+
"org.junit.jupiter.api.Nested");
10451047

10461048
private static final String USED_REFLECTIVELY = "UsedReflectively";
10471049

core/src/main/java/com/google/errorprone/bugpatterns/ClassCanBeStatic.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
tags = {StandardTags.STYLE, StandardTags.PERFORMANCE})
4444
public class ClassCanBeStatic extends BugChecker implements ClassTreeMatcher {
4545

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

@@ -78,7 +77,7 @@ public Description matchClass(ClassTree tree, VisitorState state) {
7877
if (CanBeStaticAnalyzer.referencesOuter(tree, currentClass, state)) {
7978
return NO_MATCH;
8079
}
81-
if (hasAnnotation(currentClass, JUNIT_NESTED_ANNOTATION, state)) {
80+
if (ASTHelpers.shouldKeep(tree)) {
8281
return NO_MATCH;
8382
}
8483
if (tree.getMembers().stream().anyMatch(m -> hasAnnotation(m, REFASTER_ANNOTATION, state))) {

0 commit comments

Comments
 (0)