Skip to content

Commit

Permalink
fix #1431: Avoid rewriting lambdas into ambiguous references (#1432)
Browse files Browse the repository at this point in the history
Avoid rewriting lambdas into ambiguous references
  • Loading branch information
carterkozak authored Jul 1, 2020
1 parent a5989da commit 5be0d9c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@
@SuppressWarnings("checkstyle:CyclomaticComplexity")
public final class LambdaMethodReference extends BugChecker implements BugChecker.LambdaExpressionTreeMatcher {

private static final String MESSAGE = "Lambda should be a method reference";

@Override
public Description matchLambdaExpression(LambdaExpressionTree tree, VisitorState state) {
LambdaExpressionTree.BodyKind bodyKind = tree.getBodyKind();
Expand Down Expand Up @@ -150,8 +148,7 @@ private Description convertVariableInstanceMethods(
return Description.NO_MATCH;
}
return buildFix(methodSymbol, methodInvocation, root, state, isLocal(methodInvocation))
.map(fix ->
buildDescription(root).setMessage(MESSAGE).addFix(fix).build())
.map(fix -> buildDescription(root).addFix(fix).build())
.orElse(Description.NO_MATCH);
}

Expand All @@ -170,8 +167,8 @@ private Description convertMethodInvocations(
}

return buildFix(methodSymbol, methodInvocation, root, state, isLocal(methodInvocation))
.map(fix ->
buildDescription(root).setMessage(MESSAGE).addFix(fix).build())
.filter(fix -> SuggestedFixes.compilesWithFix(fix, state))
.map(fix -> buildDescription(root).addFix(fix).build())
.orElse(Description.NO_MATCH);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,4 +727,29 @@ public void testGuavaToJavaUtilOptional() {
.expectUnchanged()
.doTest();
}

@Test
public void testSuperAmbiguous() {
refactor()
.addInputLines(
"Test.java",
"import java.util.function.BiConsumer;",
"import java.util.function.Consumer;",
"class Test {",
" interface One {",
" void call(Consumer<String> a);",
" void call(BiConsumer<String, String> a);",
" }",
" interface Two {",
" void apply(String a, String b);",
" void apply(String a);",
" }",
" void f(One one, Two two) {",
// Lambda conversion requires a cast which wouldn't necessarily be cleaner.
" one.call(value -> two.apply(value));",
" }",
"}")
.expectUnchanged()
.doTest();
}
}
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1432.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Avoid rewriting lambdas into ambiguous references
links:
- https://github.com/palantir/gradle-baseline/pull/1432

0 comments on commit 5be0d9c

Please sign in to comment.