Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Strict unused variable enforces lambda parameters (#1355)" #1357

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -221,15 +220,7 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s
Symbol.VarSymbol symbol = (Symbol.VarSymbol) unusedSymbol;
ImmutableList<SuggestedFix> 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);
}
Expand Down Expand Up @@ -481,34 +472,11 @@ private static ImmutableList<SuggestedFix> buildUnusedVarFixes(
return ImmutableList.of(fix.build());
}

private static ImmutableList<SuggestedFix> buildUnusedLambdaParameterFix(
Symbol.VarSymbol _symbol, Collection<UnusedSpec> 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<SuggestedFix> buildUnusedParameterFixes(
Symbol varSymbol, Symbol.MethodSymbol methodSymbol, List<TreePath> usagePaths, VisitorState state) {
Symbol varSymbol, List<TreePath> 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());
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String, Integer> doStuff() {",
" // BUG: Diagnostic contains: Unused",
" BiFunction<String, String, Integer> first = (String value1, String value2) -> 1;",
" // BUG: Diagnostic contains: Unused",
" return first.andThen(value3 -> 2);",
" }",
"}")
.doTest();
}

@Test
public void renames_previous_suppression() {
refactoringTestHelper
Expand Down Expand Up @@ -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<String, String, Integer> doStuff() {",
" BiFunction<String, String, Integer> first = (String value1, String value2) -> 1;",
" return first.andThen(value3 -> 2);",
" }",
"}")
.addOutputLines(
"Test.java",
"import java.util.function.BiFunction;",
"class Test {",
" private static BiFunction<String, String, Integer> doStuff() {",
" BiFunction<String, String, Integer> first = (String _value1, String _value2) -> 1;",
" return first.andThen(_value3 -> 2);",
" }",
"}")
.doTest();
}

@Test
public void fails_suppressed_but_used_variables() {
compilationHelper
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1357.v2.yml
Original file line number Diff line number Diff line change
@@ -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