Skip to content

Commit

Permalink
Allow successful inlining of methods whose body just returns a single…
Browse files Browse the repository at this point in the history
… input parameter

PiperOrigin-RevId: 631558441
  • Loading branch information
nick-someone authored and Error Prone Team committed May 7, 2024
1 parent 3d065de commit 93861b8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
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
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

0 comments on commit 93861b8

Please sign in to comment.