Skip to content

Commit 7542d36

Browse files
klueverError Prone Team
authored and
Error Prone Team
committed
Don't fire CanIgnoreReturnValueSuggester for simple return param; implementations.
#checkreturnvalue PiperOrigin-RevId: 667633531
1 parent 0a5a5b8 commit 7542d36

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/CanIgnoreReturnValueSuggester.java

+10
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ public Description matchMethod(MethodTree methodTree, VisitorState state) {
153153

154154
// if the method always return a single input param (of the same type), make it CIRV
155155
if (methodAlwaysReturnsInputParam(methodTree, state)) {
156+
// if the method _only_ returns an input param, bail out
157+
if (methodTree.getBody() != null && methodTree.getBody().getStatements().size() == 1) {
158+
StatementTree onlyStatement = methodTree.getBody().getStatements().get(0);
159+
if (onlyStatement instanceof ReturnTree) {
160+
ReturnTree returnTree = (ReturnTree) onlyStatement;
161+
if (returnTree.getExpression() instanceof IdentifierTree) {
162+
return Description.NO_MATCH;
163+
}
164+
}
165+
}
156166
return annotateWithCanIgnoreReturnValue(methodTree, state);
157167
}
158168

core/src/test/java/com/google/errorprone/bugpatterns/checkreturnvalue/CanIgnoreReturnValueSuggesterTest.java

+34
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,36 @@ public void simpleCase() {
5656
.doTest();
5757
}
5858

59+
@Test
60+
public void b362106953_returnThis() {
61+
helper
62+
.addInputLines(
63+
"Client.java",
64+
"package com.google.frobber;",
65+
"public final class Client {",
66+
" public Client setName(String name) {",
67+
" return this;",
68+
" }",
69+
"}")
70+
.expectUnchanged()
71+
.doTest();
72+
}
73+
74+
@Test
75+
public void b362106953_returnParam() {
76+
helper
77+
.addInputLines(
78+
"Client.java",
79+
"package com.google.frobber;",
80+
"public final class Client {",
81+
" public String setName(String name) {",
82+
" return name;",
83+
" }",
84+
"}")
85+
.expectUnchanged()
86+
.doTest();
87+
}
88+
5989
@Test
6090
public void parenthesizedCastThis() {
6191
helper
@@ -92,6 +122,7 @@ public void returnsInputParam() {
92122
"package com.google.frobber;",
93123
"public final class Client {",
94124
" public String method(String name) {",
125+
" System.out.println(name);",
95126
" return name;",
96127
" }",
97128
"}")
@@ -102,6 +133,7 @@ public void returnsInputParam() {
102133
"public final class Client {",
103134
" @CanIgnoreReturnValue",
104135
" public String method(String name) {",
136+
" System.out.println(name);",
105137
" return name;",
106138
" }",
107139
"}")
@@ -157,6 +189,7 @@ public void returnInputParams_multipleParams() {
157189
"package com.google.frobber;",
158190
"public final class ReturnInputParam {",
159191
" public static StringBuilder append(StringBuilder input, String name) {",
192+
" input.append(name);",
160193
" return input;",
161194
" }",
162195
"}")
@@ -167,6 +200,7 @@ public void returnInputParams_multipleParams() {
167200
"public final class ReturnInputParam {",
168201
" @CanIgnoreReturnValue",
169202
" public static StringBuilder append(StringBuilder input, String name) {",
203+
" input.append(name);",
170204
" return input;",
171205
" }",
172206
"}")

0 commit comments

Comments
 (0)