Skip to content

Commit

Permalink
Add InlineMe:CheckFixCompiles flag, which allows InlineMe users to …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
kluever authored and Error Prone Team committed Oct 25, 2021
1 parent dd91993 commit 122e512
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> apiPrefixes;
private final boolean skipCallsitesWithComments;
private final boolean checkFixCompiles;

public Inliner(ErrorProneFlags flags) {
this.apiPrefixes =
ImmutableSet.copyOf(flags.getSet(PREFIX_FLAG).orElse(ImmutableSet.<String>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);
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ public void testVarargsWithPrecedingElements() {

@Test
public void testReplaceWithJustParameter() {
refactoringTestHelper
bugCheckerWithCheckFixCompiles()
.allowBreakingChanges()
.addInputLines(
"Client.java",
Expand Down Expand Up @@ -922,7 +922,7 @@ public void testReplaceWithJustParameter() {

@Test
public void testOrderOfOperations() {
refactoringTestHelper
bugCheckerWithCheckFixCompiles()
.allowBreakingChanges()
.addInputLines(
"Client.java",
Expand Down Expand Up @@ -957,7 +957,7 @@ public void testOrderOfOperations() {

@Test
public void testOrderOfOperationsWithParamAddition() {
refactoringTestHelper
bugCheckerWithCheckFixCompiles()
.allowBreakingChanges()
.addInputLines(
"Client.java",
Expand Down Expand Up @@ -992,7 +992,7 @@ public void testOrderOfOperationsWithParamAddition() {

@Test
public void testOrderOfOperationsWithTrailingOperand() {
refactoringTestHelper
bugCheckerWithCheckFixCompiles()
.allowBreakingChanges()
.addInputLines(
"Client.java",
Expand Down Expand Up @@ -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");
}
}

0 comments on commit 122e512

Please sign in to comment.