Skip to content

Commit

Permalink
Don't perform the inlining if parameter names are stripped.
Browse files Browse the repository at this point in the history
#inlineme

PiperOrigin-RevId: 672561572
  • Loading branch information
kluever authored and Error Prone Team committed Sep 9, 2024
1 parent dab4089 commit 4390008
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,18 @@ && stringContainsComments(state.getSourceForNode(tree), state.context)) {
}

for (int i = 0; i < varNames.size(); i++) {
String varName = varNames.get(i);

// If the parameter names are missing (b/365094947), don't perform the inlining.
if (varName.matches("arg[0-9]+")) {
return Description.NO_MATCH;
}

// 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))) {
if (replacement.equals(varName)) {
replacement = callingVars.get(i);
break;
}
Expand All @@ -252,7 +259,7 @@ && stringContainsComments(state.getSourceForNode(tree), state.context)) {
String capturePrefixForVarargs = terminalVarargsReplacement ? "(?:,\\s*)?" : "\\b";
// We want to avoid replacing a method invocation with the same name as the method.
var extractArgAndNextToken =
Pattern.compile(capturePrefixForVarargs + Pattern.quote(varNames.get(i)) + "\\b([^(])");
Pattern.compile(capturePrefixForVarargs + Pattern.quote(varName) + "\\b([^(])");
String replacementResult =
Matcher.quoteReplacement(terminalVarargsReplacement ? "" : callingVars.get(i)) + "$1";
Matcher matcher = extractArgAndNextToken.matcher(replacement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,23 @@ public void paramCast_b308614050() {
.doTest();
}

@Test
public void inlinerSubstitutionsNotHappening_b365094947() {
refactoringTestHelper
.addInputLines(
"Caller.java",
"import static java.nio.charset.StandardCharsets.UTF_8;",
"import com.google.common.io.Files;",
"import java.io.File;",
"public final class Caller {",
" public void doTest(File file, String text) throws Exception {",
" Files.write(text, file, UTF_8);",
" }",
"}")
.expectUnchanged()
.doTest();
}

private BugCheckerRefactoringTestHelper bugCheckerWithPrefixFlag(String prefix) {
return BugCheckerRefactoringTestHelper.newInstance(Inliner.class, getClass())
.setArgs("-XepOpt:" + PREFIX_FLAG + "=" + prefix);
Expand Down

0 comments on commit 4390008

Please sign in to comment.