Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't fire CanIgnoreReturnValueSuggester on Dagger @Component.Builder or @Subcomponent.Builder methods (they will soon be treated as implicitly ignorable via a custom rule). #4236

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ public final class CanIgnoreReturnValueSuggester extends BugChecker implements M
"com.google.errorprone.annotations.CheckReturnValue",
"com.google.errorprone.refaster.annotation.AfterTemplate");

private static final ImmutableSet<String> EXEMPTING_CLASS_ANNOTATIONS =
ImmutableSet.of(
"com.google.auto.value.AutoValue.Builder",
"com.google.auto.value.AutoBuilder",
"dagger.Component.Builder",
"dagger.Subcomponent.Builder");

private static final Supplier<Type> PROTO_BUILDER =
VisitorState.memoize(s -> s.getTypeFromString("com.google.protobuf.MessageLite.Builder"));

Expand All @@ -93,7 +100,7 @@ public final class CanIgnoreReturnValueSuggester extends BugChecker implements M
private final ImmutableSet<String> exemptingMethodAnnotations;

@Inject
public CanIgnoreReturnValueSuggester(ErrorProneFlags errorProneFlags) {
CanIgnoreReturnValueSuggester(ErrorProneFlags errorProneFlags) {
this.exemptingMethodAnnotations =
Sets.union(
EXEMPTING_METHOD_ANNOTATIONS,
Expand All @@ -107,7 +114,7 @@ public Description matchMethod(MethodTree methodTree, VisitorState state) {

// If the method has an exempting annotation, then bail out.
if (exemptingMethodAnnotations.stream()
.anyMatch(annotation -> ASTHelpers.hasAnnotation(methodSymbol, annotation, state))) {
.anyMatch(annotation -> hasAnnotation(methodSymbol, annotation, state))) {
return Description.NO_MATCH;
}

Expand Down Expand Up @@ -148,8 +155,8 @@ public Description matchMethod(MethodTree methodTree, VisitorState state) {
return Description.NO_MATCH;
}

// skip @AutoValue and @AutoBuilder methods
if (isAbstractAutoValueOrAutoBuilderMethod(methodSymbol, state)) {
// skip builder setter methods defined on "known safe" types
if (isKnownSafeBuilderInterface(methodSymbol, state)) {
return Description.NO_MATCH;
}

Expand Down Expand Up @@ -179,14 +186,17 @@ private Description annotateWithCanIgnoreReturnValue(MethodTree methodTree, Visi
return describeMatch(methodTree, fix.build());
}

private static boolean isAbstractAutoValueOrAutoBuilderMethod(
private static boolean isKnownSafeBuilderInterface(
MethodSymbol methodSymbol, VisitorState state) {
Symbol owner = methodSymbol.owner;
// TODO(kak): use ResultEvaluator instead of duplicating _some_ of the logic (right now we only
// exclude @AutoValue.Builder's and @AutoBuilder's)
return isAbstract(methodSymbol)
&& (hasAnnotation(owner, AUTO_VALUE + ".Builder", state)
|| hasAnnotation(owner, "com.google.auto.value.AutoBuilder", state));
// TODO(kak): use ResultEvaluator instead of duplicating logic
if (isAbstract(methodSymbol)) {
for (String annotation : EXEMPTING_CLASS_ANNOTATIONS) {
if (hasAnnotation(methodSymbol.owner, annotation, state)) {
return true;
}
}
}
return false;
}

private static boolean classLooksLikeBuilder(Symbol owner, VisitorState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -885,18 +885,7 @@ public void daggerComponentBuilder_b318407972() {
" Builder setName(String name);",
" String build();",
"}")
.addOutputLines(
"Builder.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.CanIgnoreReturnValue;",
"import dagger.Component;",
"@Component.Builder",
"interface Builder {",
// TODO(b/318407972): we shouldn't suggest @CIRV on Dagger Component.Builder setters
" @CanIgnoreReturnValue",
" Builder setName(String name);",
" String build();",
"}")
.expectUnchanged()
.doTest();
}

Expand All @@ -912,18 +901,7 @@ public void daggerSubcomponentBuilder_b318407972() {
" Builder setName(String name);",
" String build();",
"}")
.addOutputLines(
"Builder.java",
"package com.google.frobber;",
"import com.google.errorprone.annotations.CanIgnoreReturnValue;",
"import dagger.Subcomponent;",
"@Subcomponent.Builder",
"interface Builder {",
// TODO(b/318407972): we shouldn't suggest @CIRV on Dagger Subcomponent.Builder setters
" @CanIgnoreReturnValue",
" Builder setName(String name);",
" String build();",
"}")
.expectUnchanged()
.doTest();
}
}
Loading