From 122e51294d1851ab22ff405d5f48a08911b1e309 Mon Sep 17 00:00:00 2001 From: Kurt Alfred Kluever Date: Mon, 25 Oct 2021 12:46:21 -0700 Subject: [PATCH] Add `InlineMe:CheckFixCompiles` flag, which allows InlineMe users to optionally check that the generated fixes compile (only if there are no new imports). This flag is disabled by default, due to (external) performance issues. PiperOrigin-RevId: 405468093 --- .../errorprone/bugpatterns/inlineme/Inliner.java | 12 +++++++----- .../bugpatterns/inlineme/InlinerTest.java | 15 ++++++++++----- 2 files changed, 17 insertions(+), 10 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 cbbb8165a32..5cc673d52a1 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 @@ -83,20 +83,22 @@ public final class Inliner extends BugChecker private static final Splitter PACKAGE_SPLITTER = Splitter.on('.'); + private static final String CHECK_FIX_COMPILES = "InlineMe:CheckFixCompiles"; + private static final String INLINE_ME = "InlineMe"; private static final String VALIDATION_DISABLED = "InlineMeValidationDisabled"; private final ImmutableSet apiPrefixes; private final boolean skipCallsitesWithComments; + private final boolean checkFixCompiles; public Inliner(ErrorProneFlags flags) { this.apiPrefixes = ImmutableSet.copyOf(flags.getSet(PREFIX_FLAG).orElse(ImmutableSet.of())); this.skipCallsitesWithComments = flags.getBoolean(SKIP_COMMENTS_FLAG).orElse(true); + this.checkFixCompiles = flags.getBoolean(CHECK_FIX_COMPILES).orElse(false); } - // TODO(b/163596864): Add support for inlining fields - @Override public Description matchNewClass(NewClassTree tree, VisitorState state) { MethodSymbol symbol = getSymbol(tree); @@ -257,9 +259,9 @@ private Description match( SuggestedFix fix = builder.build(); - // If there are no imports to add, then there's no new dependencies, so we can verify that it - // compilesWithFix(); if there are new imports to add, then we can't validate that it compiles. - if (fix.getImportsToAdd().isEmpty()) { + if (checkFixCompiles && fix.getImportsToAdd().isEmpty()) { + // If there are no new imports being added (then there are no new dependencies). Therefore, we + // can verify that the fix compiles (if CHECK_FIX_COMPILES is enabled). return SuggestedFixes.compilesWithFix(fix, state) ? describe(tree, fix, api) : Description.NO_MATCH; 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 542cbda8a1e..03008ad422e 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 @@ -885,7 +885,7 @@ public void testVarargsWithPrecedingElements() { @Test public void testReplaceWithJustParameter() { - refactoringTestHelper + bugCheckerWithCheckFixCompiles() .allowBreakingChanges() .addInputLines( "Client.java", @@ -922,7 +922,7 @@ public void testReplaceWithJustParameter() { @Test public void testOrderOfOperations() { - refactoringTestHelper + bugCheckerWithCheckFixCompiles() .allowBreakingChanges() .addInputLines( "Client.java", @@ -957,7 +957,7 @@ public void testOrderOfOperations() { @Test public void testOrderOfOperationsWithParamAddition() { - refactoringTestHelper + bugCheckerWithCheckFixCompiles() .allowBreakingChanges() .addInputLines( "Client.java", @@ -992,7 +992,7 @@ public void testOrderOfOperationsWithParamAddition() { @Test public void testOrderOfOperationsWithTrailingOperand() { - refactoringTestHelper + bugCheckerWithCheckFixCompiles() .allowBreakingChanges() .addInputLines( "Client.java", @@ -1141,8 +1141,13 @@ public void testCustomInlineMe() { .doTest(); } - private BugCheckerRefactoringTestHelper buildBugCheckerWithPrefixFlag(String prefix) { + private BugCheckerRefactoringTestHelper bugCheckerWithPrefixFlag(String prefix) { return BugCheckerRefactoringTestHelper.newInstance(Inliner.class, getClass()) .setArgs("-XepOpt:" + PREFIX_FLAG + "=" + prefix); } + + private BugCheckerRefactoringTestHelper bugCheckerWithCheckFixCompiles() { + return BugCheckerRefactoringTestHelper.newInstance(Inliner.class, getClass()) + .setArgs("-XepOpt:InlineMe:CheckFixCompiles=true"); + } }