Skip to content
Open
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 @@ -83,6 +83,9 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
return Description.NO_MATCH;
}
VariableTree arg = getOnlyElement(lambdaExpressionTree.getParameters());
if ("_".equals(state.getSourceForNode(arg))) {
return Description.NO_MATCH;
}
SuggestedFix.Builder fix = SuggestedFix.builder();
new TreeScanner<Void, VisitorState>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.errorprone.bugpatterns;

import static com.google.common.truth.TruthJUnit.assume;

import com.google.errorprone.BugCheckerRefactoringTestHelper;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -388,4 +390,24 @@ private void home() {
""")
.doTest();
}

@Test
public void unnamedVariable_doesNotSuggestFix() {
assume().that(Runtime.version().feature()).isAtLeast(22);
refactoringTestHelper
.addInputLines(
"Test.java",
"""
import java.util.Optional;

public class Test {
private void home() {
Optional<String> op = Optional.of("hello");
op.ifPresent(_ -> System.out.println(op.get()));
}
}
""")
.expectUnchanged()
.doTest();
}
}