From 5be0d9ca72c19723f895603850e5a16c3a84fdc6 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Wed, 1 Jul 2020 09:02:28 -0400 Subject: [PATCH] fix #1431: Avoid rewriting lambdas into ambiguous references (#1432) Avoid rewriting lambdas into ambiguous references --- .../errorprone/LambdaMethodReference.java | 9 +++---- .../errorprone/LambdaMethodReferenceTest.java | 25 +++++++++++++++++++ changelog/@unreleased/pr-1432.v2.yml | 5 ++++ 3 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 changelog/@unreleased/pr-1432.v2.yml diff --git a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/LambdaMethodReference.java b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/LambdaMethodReference.java index 188a51a00..3fad79386 100644 --- a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/LambdaMethodReference.java +++ b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/LambdaMethodReference.java @@ -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(); @@ -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); } @@ -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); } diff --git a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/LambdaMethodReferenceTest.java b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/LambdaMethodReferenceTest.java index b2bd0c435..33b1d7169 100644 --- a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/LambdaMethodReferenceTest.java +++ b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/LambdaMethodReferenceTest.java @@ -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 a);", + " void call(BiConsumer 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(); + } } diff --git a/changelog/@unreleased/pr-1432.v2.yml b/changelog/@unreleased/pr-1432.v2.yml new file mode 100644 index 000000000..9df9b3ac5 --- /dev/null +++ b/changelog/@unreleased/pr-1432.v2.yml @@ -0,0 +1,5 @@ +type: fix +fix: + description: Avoid rewriting lambdas into ambiguous references + links: + - https://github.com/palantir/gradle-baseline/pull/1432