From 43900088013e62f675119105d694d67e3a6b2607 Mon Sep 17 00:00:00 2001 From: Kurt Alfred Kluever Date: Mon, 9 Sep 2024 08:57:53 -0700 Subject: [PATCH] Don't perform the inlining if parameter names are stripped. #inlineme PiperOrigin-RevId: 672561572 --- .../bugpatterns/inlineme/Inliner.java | 11 +++++++++-- .../bugpatterns/inlineme/InlinerTest.java | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/inlineme/Inliner.java b/core/src/main/java/com/google/errorprone/bugpatterns/inlineme/Inliner.java index 5b234814c50..197b041e34c 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/inlineme/Inliner.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/inlineme/Inliner.java @@ -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; } @@ -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); diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/inlineme/InlinerTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/inlineme/InlinerTest.java index ff1523dc8a4..62d2dd929d1 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/inlineme/InlinerTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/inlineme/InlinerTest.java @@ -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);