Skip to content

Commit

Permalink
Revert "Strict unused variable enforces lambda parameters (#1355)"
Browse files Browse the repository at this point in the history
This reverts commit 620c65b.
  • Loading branch information
dansanduleac committed May 28, 2020
1 parent 26a93df commit b817a1e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 80 deletions.
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

0 comments on commit b817a1e

Please sign in to comment.