From 4f060e1ff140874e1d7424a08c6d23e9c9d50d13 Mon Sep 17 00:00:00 2001 From: Kurt Alfred Kluever Date: Wed, 14 Feb 2024 13:32:20 -0800 Subject: [PATCH] Add a test case demonstrating a `CanIgnoreReturnValueSuggester` bug. PiperOrigin-RevId: 607088772 --- .../CanIgnoreReturnValueSuggesterTest.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/checkreturnvalue/CanIgnoreReturnValueSuggesterTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/checkreturnvalue/CanIgnoreReturnValueSuggesterTest.java index 7b1e6f27c0a..6062ed46b27 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/checkreturnvalue/CanIgnoreReturnValueSuggesterTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/checkreturnvalue/CanIgnoreReturnValueSuggesterTest.java @@ -924,4 +924,49 @@ public void anonymous() { .expectUnchanged() .doTest(); } + + @Test + public void returningANewInstance() { + helper + .addInputLines( + "MyBuilder.java", + "public final class MyBuilder {", + " MyBuilder() { }", + " MyBuilder(String name) { }", + " MyBuilder(MyBuilder builder) { }", + " public MyBuilder passingParam(String name) {", + " return new MyBuilder(name);", + " }", + " public MyBuilder passingThis() {", + " return new MyBuilder(this);", + " }", + " public MyBuilder notPassing() {", + " return new MyBuilder();", + " }", + "}") + .addOutputLines( + "MyBuilder.java", + "import com.google.errorprone.annotations.CanIgnoreReturnValue;", + "public final class MyBuilder {", + " MyBuilder() { }", + " MyBuilder(String name) { }", + " MyBuilder(MyBuilder builder) { }", + // TODO(b/325282579): this is a bug! we should not recommend @CIRV here! + " @CanIgnoreReturnValue", + " public MyBuilder passingParam(String name) {", + " return new MyBuilder(name);", + " }", + // TODO(b/325282579): this is a bug! we should not recommend @CIRV here! + " @CanIgnoreReturnValue", + " public MyBuilder passingThis() {", + " return new MyBuilder(this);", + " }", + // TODO(b/325282579): this is a bug! we should not recommend @CIRV here! + " @CanIgnoreReturnValue", + " public MyBuilder notPassing() {", + " return new MyBuilder();", + " }", + "}") + .doTest(); + } }