From 56f214bac611220f540d9dc796ddba00f0784e3f Mon Sep 17 00:00:00 2001 From: Kurt Alfred Kluever Date: Thu, 1 Aug 2024 07:20:05 -0700 Subject: [PATCH] Don't fire `CanIgnoreReturnValueSuggester` if the function is directly annotated w/ `@CheckReturnValue` (from any package). #checkreturnvalue PiperOrigin-RevId: 658392303 --- .../CanIgnoreReturnValueSuggester.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/CanIgnoreReturnValueSuggester.java b/core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/CanIgnoreReturnValueSuggester.java index 404f4ec37c51..15fd049c4cd2 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/CanIgnoreReturnValueSuggester.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/CanIgnoreReturnValueSuggester.java @@ -79,11 +79,10 @@ public final class CanIgnoreReturnValueSuggester extends BugChecker implements M private static final String AUTO_VALUE = "com.google.auto.value.AutoValue"; private static final String IMMUTABLE = "com.google.errorprone.annotations.Immutable"; + private static final String CRV_SIMPLE_NAME = "CheckReturnValue"; private static final String CIRV_SIMPLE_NAME = "CanIgnoreReturnValue"; private static final ImmutableSet EXEMPTING_METHOD_ANNOTATIONS = - ImmutableSet.of( - "com.google.errorprone.annotations.CheckReturnValue", - "com.google.errorprone.refaster.annotation.AfterTemplate"); + ImmutableSet.of("com.google.errorprone.refaster.annotation.AfterTemplate"); private static final ImmutableSet EXEMPTING_CLASS_ANNOTATIONS = ImmutableSet.of( @@ -112,14 +111,14 @@ public final class CanIgnoreReturnValueSuggester extends BugChecker implements M @Override public Description matchMethod(MethodTree methodTree, VisitorState state) { MethodSymbol methodSymbol = getSymbol(methodTree); - // Don't fire on overrides of methods within anonymous classes. - if (streamSuperMethods(methodSymbol, state.getTypes()).findFirst().isPresent() - && methodSymbol.owner.isAnonymous()) { + + // If the method is directly annotated w/ any @CanIgnoreReturnValue, then bail out. + if (hasDirectAnnotationWithSimpleName(methodSymbol, CIRV_SIMPLE_NAME)) { return Description.NO_MATCH; } - // If the method is @CanIgnoreReturnValue (in any package), then bail out. - if (hasDirectAnnotationWithSimpleName(methodSymbol, CIRV_SIMPLE_NAME)) { + // If the method is directly annotated w/ any @CheckReturnValue, then bail out. + if (hasDirectAnnotationWithSimpleName(methodSymbol, CRV_SIMPLE_NAME)) { return Description.NO_MATCH; } @@ -141,6 +140,12 @@ public Description matchMethod(MethodTree methodTree, VisitorState state) { return Description.NO_MATCH; } + // Don't fire on overrides of methods within anonymous classes. + if (streamSuperMethods(methodSymbol, state.getTypes()).findFirst().isPresent() + && methodSymbol.owner.isAnonymous()) { + return Description.NO_MATCH; + } + // if the method always return a single input param (of the same type), make it CIRV if (methodAlwaysReturnsInputParam(methodTree, state)) { return annotateWithCanIgnoreReturnValue(methodTree, state);