Skip to content

Commit

Permalink
Don't fire CanIgnoreReturnValueSuggester if the function is directl…
Browse files Browse the repository at this point in the history
…y annotated w/ `@CheckReturnValue` (from any package).

#checkreturnvalue

PiperOrigin-RevId: 658409445
  • Loading branch information
kluever authored and Error Prone Team committed Aug 1, 2024
1 parent 6ff6f35 commit 1e0e03c
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> EXEMPTING_CLASS_ANNOTATIONS =
ImmutableSet.of(
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down

0 comments on commit 1e0e03c

Please sign in to comment.