From 1569624678bc274a3539d5f1d6dce02cdddd7686 Mon Sep 17 00:00:00 2001 From: Kurt Alfred Kluever Date: Mon, 26 Aug 2024 09:59:20 -0700 Subject: [PATCH] Don't fire `CanIgnoreReturnValueSuggester` for simple `return param;` implementations. #checkreturnvalue PiperOrigin-RevId: 667620072 --- .../CanIgnoreReturnValueSuggester.java | 10 ++++++ .../CanIgnoreReturnValueSuggesterTest.java | 34 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/CanIgnoreReturnValueSuggester.java b/core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/CanIgnoreReturnValueSuggester.java index 72d0a4ec697b..7a7cf8836bad 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/CanIgnoreReturnValueSuggester.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/CanIgnoreReturnValueSuggester.java @@ -153,6 +153,16 @@ public Description matchMethod(MethodTree methodTree, VisitorState state) { // if the method always return a single input param (of the same type), make it CIRV if (methodAlwaysReturnsInputParam(methodTree, state)) { + // if the method _only_ returns an input param, bail out + if (methodTree.getBody() != null && methodTree.getBody().getStatements().size() == 1) { + StatementTree onlyStatement = methodTree.getBody().getStatements().get(0); + if (onlyStatement instanceof ReturnTree) { + ReturnTree returnTree = (ReturnTree) onlyStatement; + if (returnTree.getExpression() instanceof IdentifierTree) { + return Description.NO_MATCH; + } + } + } return annotateWithCanIgnoreReturnValue(methodTree, state); } 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 a5f1606036d8..a0b3349327f4 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 @@ -56,6 +56,36 @@ public void simpleCase() { .doTest(); } + @Test + public void b362106953_returnThis() { + helper + .addInputLines( + "Client.java", + "package com.google.frobber;", + "public final class Client {", + " public Client setName(String name) {", + " return this;", + " }", + "}") + .expectUnchanged() + .doTest(); + } + + @Test + public void b362106953_returnParam() { + helper + .addInputLines( + "Client.java", + "package com.google.frobber;", + "public final class Client {", + " public String setName(String name) {", + " return name;", + " }", + "}") + .expectUnchanged() + .doTest(); + } + @Test public void parenthesizedCastThis() { helper @@ -92,6 +122,7 @@ public void returnsInputParam() { "package com.google.frobber;", "public final class Client {", " public String method(String name) {", + " System.out.println(name);", " return name;", " }", "}") @@ -102,6 +133,7 @@ public void returnsInputParam() { "public final class Client {", " @CanIgnoreReturnValue", " public String method(String name) {", + " System.out.println(name);", " return name;", " }", "}") @@ -157,6 +189,7 @@ public void returnInputParams_multipleParams() { "package com.google.frobber;", "public final class ReturnInputParam {", " public static StringBuilder append(StringBuilder input, String name) {", + " input.append(name);", " return input;", " }", "}") @@ -167,6 +200,7 @@ public void returnInputParams_multipleParams() { "public final class ReturnInputParam {", " @CanIgnoreReturnValue", " public static StringBuilder append(StringBuilder input, String name) {", + " input.append(name);", " return input;", " }", "}")