Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't fire CanIgnoreReturnValueSuggester for simple return param; implementations. #4553

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading