diff --git a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StrictUnusedVariable.java b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StrictUnusedVariable.java index ec7974383..b09be6bc6 100644 --- a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StrictUnusedVariable.java +++ b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/StrictUnusedVariable.java @@ -51,7 +51,6 @@ import com.google.auto.service.AutoService; import com.google.common.base.Ascii; import com.google.common.base.CaseFormat; -import com.google.common.base.Preconditions; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableListMultimap; @@ -221,15 +220,7 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s Symbol.VarSymbol symbol = (Symbol.VarSymbol) unusedSymbol; ImmutableList fixes; if (symbol.getKind() == ElementKind.PARAMETER && !isEverUsed.contains(unusedSymbol)) { - Symbol.MethodSymbol methodSymbol = (Symbol.MethodSymbol) symbol.owner; - int index = methodSymbol.params.indexOf(symbol); - // If we can not find the parameter in the owning method, then it must be a parameter to a lambda - // defined within the method - if (index == -1) { - fixes = buildUnusedLambdaParameterFix(symbol, entry.getValue(), state); - } else { - fixes = buildUnusedParameterFixes(symbol, methodSymbol, allUsageSites, state); - } + fixes = buildUnusedParameterFixes(symbol, allUsageSites, state); } else { fixes = buildUnusedVarFixes(symbol, allUsageSites, state); } @@ -481,34 +472,11 @@ private static ImmutableList buildUnusedVarFixes( return ImmutableList.of(fix.build()); } - private static ImmutableList buildUnusedLambdaParameterFix( - Symbol.VarSymbol _symbol, Collection values, VisitorState state) { - SuggestedFix.Builder fix = SuggestedFix.builder(); - - for (UnusedSpec unusedSpec : values) { - Tree leaf = unusedSpec.variableTree().getLeaf(); - if (!(leaf instanceof VariableTree)) { - continue; - } - - VariableTree tree = (VariableTree) leaf; - if (state.getEndPosition(tree.getType()) == -1) { - fix.replace(tree, "_" + tree.getName()); - } else { - int startPos = state.getEndPosition(tree.getType()) + 1; - int endPos = state.getEndPosition(tree); - fix.replace(startPos, endPos, "_" + tree.getName()); - } - } - - return ImmutableList.of(fix.build()); - } - private static ImmutableList buildUnusedParameterFixes( - Symbol varSymbol, Symbol.MethodSymbol methodSymbol, List usagePaths, VisitorState state) { + Symbol varSymbol, List usagePaths, VisitorState state) { + Symbol.MethodSymbol methodSymbol = (Symbol.MethodSymbol) varSymbol.owner; boolean isPrivateMethod = methodSymbol.getModifiers().contains(Modifier.PRIVATE); int index = methodSymbol.params.indexOf(varSymbol); - Preconditions.checkState(index != -1, "symbol %s must be a parameter to the owning method", varSymbol); SuggestedFix.Builder fix = SuggestedFix.builder(); for (TreePath path : usagePaths) { fix.delete(path.getLeaf()); @@ -748,9 +716,8 @@ public Void visitClass(ClassTree tree, Void unused) { @Override public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) { - scan(node.getBody(), null); - scan(node.getParameters(), null); - return null; + // skip lambda parameters + return scan(node.getBody(), null); } @Override diff --git a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/StrictUnusedVariableTest.java b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/StrictUnusedVariableTest.java index a2ca58b70..2c31910a1 100644 --- a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/StrictUnusedVariableTest.java +++ b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/StrictUnusedVariableTest.java @@ -95,24 +95,6 @@ public void handles_enums() { .doTest(); } - @Test - void handles_lambdas() { - compilationHelper - .addSourceLines( - "Test.java", - "import java.util.function.BiFunction;", - "import java.util.Optional;", - "class Test {", - " private static BiFunction doStuff() {", - " // BUG: Diagnostic contains: Unused", - " BiFunction first = (String value1, String value2) -> 1;", - " // BUG: Diagnostic contains: Unused", - " return first.andThen(value3 -> 2);", - " }", - "}") - .doTest(); - } - @Test public void renames_previous_suppression() { refactoringTestHelper @@ -151,30 +133,6 @@ public void renames_unused_param() { .doTest(TestMode.TEXT_MATCH); } - @Test - void renames_unused_lambda_params() { - refactoringTestHelper - .addInputLines( - "Test.java", - "import java.util.function.BiFunction;", - "class Test {", - " private static BiFunction doStuff() {", - " BiFunction first = (String value1, String value2) -> 1;", - " return first.andThen(value3 -> 2);", - " }", - "}") - .addOutputLines( - "Test.java", - "import java.util.function.BiFunction;", - "class Test {", - " private static BiFunction doStuff() {", - " BiFunction first = (String _value1, String _value2) -> 1;", - " return first.andThen(_value3 -> 2);", - " }", - "}") - .doTest(); - } - @Test public void fails_suppressed_but_used_variables() { compilationHelper diff --git a/changelog/@unreleased/pr-1357.v2.yml b/changelog/@unreleased/pr-1357.v2.yml new file mode 100644 index 000000000..5fa45882f --- /dev/null +++ b/changelog/@unreleased/pr-1357.v2.yml @@ -0,0 +1,5 @@ +type: fix +fix: + description: 'Revert recent regression #1355 from 5.18.0 which broke some consumers.' + links: + - https://github.com/palantir/gradle-baseline/pull/1357