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

Allow successful inlining of methods whose body just returns a single input parameter #4389

Merged
merged 1 commit into from
May 7, 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 @@ -234,7 +234,17 @@ && stringContainsComments(state.getSourceForNode(tree), state.context)) {
"\\b" + Pattern.quote(typeName.getKey()) + "\\b",
Matcher.quoteReplacement(typeName.getValue()));
}

for (int i = 0; i < varNames.size(); i++) {
// The replacement logic below assumes the existence of another token after the parameter
// in the replacement string (ex: a trailing parens, comma, dot, etc.). However, in the case
// where the replacement is _just_ one parameter, there isn't a trailing token. We just make
// the direct replacement here.
if (replacement.equals(varNames.get(i))) {
replacement = callingVars.get(i);
break;
}

// Ex: foo(int a, int... others) -> this.bar(a, others)
// If caller passes 0 args in the varargs position, we want to remove the preceding comma to
// make this.bar(a) (as opposed to "this.bar(a, )"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
/** Tests for the {@link Inliner}. */
@RunWith(JUnit4.class)
public class InlinerTest {
/* We expect that all @InlineMe annotations we try to use as inlineable targets are valid,
so we run both checkers here. If the Validator trips on a method, we'll suggest some
replacement which should trip up the checker.
/*
We expect that all @InlineMe annotations we try to use as inlineable targets are valid,
so we run both checkers here. If the Validator trips on a method, we'll suggest some
replacement which should trip up the checker.
*/
private final BugCheckerRefactoringTestHelper refactoringTestHelper =
BugCheckerRefactoringTestHelper.newInstance(
Expand Down Expand Up @@ -912,9 +913,7 @@ public void replaceWithJustParameter() {
"public final class Caller {",
" public void doTest() {",
" Client client = new Client();",
// TODO(b/180976346): replacements of the form that terminate in a parameter by itself
// don't work with the new replacement tool, but this is uncommon enough
" int x = client.identity(42);",
" int x = 42;",
" }",
"}")
.doTest();
Expand Down
Loading