Skip to content

Commit 1e0e03c

Browse files
klueverError Prone Team
authored and
Error Prone Team
committed
Don't fire CanIgnoreReturnValueSuggester if the function is directly annotated w/ @CheckReturnValue (from any package).
#checkreturnvalue PiperOrigin-RevId: 658409445
1 parent 6ff6f35 commit 1e0e03c

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/CanIgnoreReturnValueSuggester.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,10 @@ public final class CanIgnoreReturnValueSuggester extends BugChecker implements M
7979

8080
private static final String AUTO_VALUE = "com.google.auto.value.AutoValue";
8181
private static final String IMMUTABLE = "com.google.errorprone.annotations.Immutable";
82+
private static final String CRV_SIMPLE_NAME = "CheckReturnValue";
8283
private static final String CIRV_SIMPLE_NAME = "CanIgnoreReturnValue";
8384
private static final ImmutableSet<String> EXEMPTING_METHOD_ANNOTATIONS =
84-
ImmutableSet.of(
85-
"com.google.errorprone.annotations.CheckReturnValue",
86-
"com.google.errorprone.refaster.annotation.AfterTemplate");
85+
ImmutableSet.of("com.google.errorprone.refaster.annotation.AfterTemplate");
8786

8887
private static final ImmutableSet<String> EXEMPTING_CLASS_ANNOTATIONS =
8988
ImmutableSet.of(
@@ -112,14 +111,14 @@ public final class CanIgnoreReturnValueSuggester extends BugChecker implements M
112111
@Override
113112
public Description matchMethod(MethodTree methodTree, VisitorState state) {
114113
MethodSymbol methodSymbol = getSymbol(methodTree);
115-
// Don't fire on overrides of methods within anonymous classes.
116-
if (streamSuperMethods(methodSymbol, state.getTypes()).findFirst().isPresent()
117-
&& methodSymbol.owner.isAnonymous()) {
114+
115+
// If the method is directly annotated w/ any @CanIgnoreReturnValue, then bail out.
116+
if (hasDirectAnnotationWithSimpleName(methodSymbol, CIRV_SIMPLE_NAME)) {
118117
return Description.NO_MATCH;
119118
}
120119

121-
// If the method is @CanIgnoreReturnValue (in any package), then bail out.
122-
if (hasDirectAnnotationWithSimpleName(methodSymbol, CIRV_SIMPLE_NAME)) {
120+
// If the method is directly annotated w/ any @CheckReturnValue, then bail out.
121+
if (hasDirectAnnotationWithSimpleName(methodSymbol, CRV_SIMPLE_NAME)) {
123122
return Description.NO_MATCH;
124123
}
125124

@@ -141,6 +140,12 @@ public Description matchMethod(MethodTree methodTree, VisitorState state) {
141140
return Description.NO_MATCH;
142141
}
143142

143+
// Don't fire on overrides of methods within anonymous classes.
144+
if (streamSuperMethods(methodSymbol, state.getTypes()).findFirst().isPresent()
145+
&& methodSymbol.owner.isAnonymous()) {
146+
return Description.NO_MATCH;
147+
}
148+
144149
// if the method always return a single input param (of the same type), make it CIRV
145150
if (methodAlwaysReturnsInputParam(methodTree, state)) {
146151
return annotateWithCanIgnoreReturnValue(methodTree, state);

0 commit comments

Comments
 (0)