diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/BuilderReturnThis.java b/core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/BuilderReturnThis.java index 36d177867b5..a6aaa7cc0cc 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/BuilderReturnThis.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/BuilderReturnThis.java @@ -21,6 +21,7 @@ import static com.google.errorprone.fixes.SuggestedFixes.qualifyType; import static com.google.errorprone.matchers.Description.NO_MATCH; import static com.google.errorprone.util.ASTHelpers.getSymbol; +import static com.google.errorprone.util.ASTHelpers.hasAnnotation; import static com.google.errorprone.util.ASTHelpers.isSameType; import static com.google.errorprone.util.ASTHelpers.isSubtype; import static java.lang.Boolean.TRUE; @@ -57,6 +58,9 @@ public Description matchMethod(MethodTree tree, VisitorState state) { if (tree.getBody() == null) { return NO_MATCH; } + if (hasAnnotation(tree, CRV, state)) { + return Description.NO_MATCH; + } if (!instanceReturnsBuilder(sym, state)) { return NO_MATCH; } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/checkreturnvalue/BuilderReturnThisTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/checkreturnvalue/BuilderReturnThisTest.java index f22784e678b..08549f14ded 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/checkreturnvalue/BuilderReturnThisTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/checkreturnvalue/BuilderReturnThisTest.java @@ -32,6 +32,8 @@ public void negative() { testHelper .addInputLines( "Test.java", + "import com.google.errorprone.annotations.CheckReturnValue;", + "", "class Test {", " static class TestBuilder {", " static TestBuilder builder() {", @@ -58,6 +60,10 @@ public void negative() { " TestBuilder setParens(String bar) {", " return (this);", " }", + " @CheckReturnValue", + " TestBuilder dontFlag(String flag) {", + " return new TestBuilder();", + " }", " }", "}") .expectUnchanged()