Skip to content

Commit

Permalink
Don't fire CanIgnoreReturnValueSuggester for simple return param;
Browse files Browse the repository at this point in the history
… implementations.

#checkreturnvalue

PiperOrigin-RevId: 667633531
  • Loading branch information
kluever authored and Error Prone Team committed Aug 26, 2024
1 parent 0a5a5b8 commit 7542d36
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;",
" }",
"}")
Expand All @@ -102,6 +133,7 @@ public void returnsInputParam() {
"public final class Client {",
" @CanIgnoreReturnValue",
" public String method(String name) {",
" System.out.println(name);",
" return name;",
" }",
"}")
Expand Down Expand Up @@ -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;",
" }",
"}")
Expand All @@ -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;",
" }",
"}")
Expand Down

0 comments on commit 7542d36

Please sign in to comment.