diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/NullableVoid.java b/core/src/main/java/com/google/errorprone/bugpatterns/NullableVoid.java index b65ba237c70..d8ca3cd0b96 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/NullableVoid.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/NullableVoid.java @@ -17,6 +17,7 @@ package com.google.errorprone.bugpatterns; import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; +import static com.google.errorprone.dataflow.nullnesspropagation.NullnessAnnotations.annotationsRelevantToNullness; import static com.google.errorprone.matchers.Description.NO_MATCH; import com.google.errorprone.BugPattern; @@ -26,7 +27,6 @@ import com.google.errorprone.fixes.SuggestedFix; import com.google.errorprone.matchers.Description; import com.google.errorprone.util.ASTHelpers; -import com.sun.source.tree.AnnotationTree; import com.sun.source.tree.MethodTree; import com.sun.tools.javac.code.Symbol.MethodSymbol; import javax.lang.model.type.TypeKind; @@ -34,7 +34,7 @@ /** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */ @BugPattern( summary = - "void-returning methods should not be annotated with @Nullable," + "void-returning methods should not be annotated with nullness annotations," + " since they cannot return null", severity = WARNING, tags = StandardTags.STYLE) @@ -46,11 +46,12 @@ public Description matchMethod(MethodTree tree, VisitorState state) { if (sym.getReturnType().getKind() != TypeKind.VOID) { return NO_MATCH; } - AnnotationTree annotation = - ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), "Nullable"); - if (annotation == null) { + var relevantAnnos = annotationsRelevantToNullness(tree.getModifiers().getAnnotations()); + if (relevantAnnos.isEmpty()) { return NO_MATCH; } - return describeMatch(annotation, SuggestedFix.delete(annotation)); + var fix = SuggestedFix.builder(); + relevantAnnos.forEach(fix::delete); + return describeMatch(relevantAnnos.get(0), fix.build()); } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/NullableVoidTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/NullableVoidTest.java index dff29e41b41..c51805a5a8e 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/NullableVoidTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/NullableVoidTest.java @@ -36,12 +36,24 @@ public void positive() { "import javax.annotation.Nullable;", "class Test {", " // BUG: Diagnostic contains:", - " // void-returning methods should not be annotated with @Nullable", " @Nullable void f() {}", "}") .doTest(); } + @Test + public void positiveCheckForNull() { + compilationHelper + .addSourceLines( + "Test.java", + "import javax.annotation.CheckForNull;", + "class Test {", + " // BUG: Diagnostic contains:", + " @CheckForNull void f() {}", + "}") + .doTest(); + } + @Test public void negativeNotAnnotated() { compilationHelper