Skip to content

Commit af4e202

Browse files
klueverError Prone Team
authored andcommitted
Add a unit test for LambdaExpressionTrees to UnnecessaryParenthesesTest.
PiperOrigin-RevId: 897391220
1 parent 30a5a30 commit af4e202

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

core/src/test/java/com/google/errorprone/bugpatterns/UnnecessaryParenthesesTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.errorprone.bugpatterns;
1818

19+
import static com.google.common.truth.TruthJUnit.assume;
20+
1921
import com.google.auto.value.processor.AutoValueProcessor;
2022
import com.google.errorprone.BugCheckerRefactoringTestHelper;
2123
import com.google.errorprone.CompilationTestHelper;
@@ -176,6 +178,37 @@ Function<Void, Void> f() {
176178
.doTest();
177179
}
178180

181+
@Test
182+
public void lambdaExpressionTrees() {
183+
assume().that(Runtime.version().feature()).isAtLeast(22);
184+
testHelper
185+
.addInputLines(
186+
"Test.java",
187+
"""
188+
import java.util.List;
189+
190+
class Test {
191+
void withParens(List<String> list) {
192+
list.forEach((_) -> System.out.println());
193+
list.forEach((unused) -> System.out.println());
194+
list.forEach((String unused) -> System.out.println());
195+
}
196+
197+
void withoutParens(List<String> list) {
198+
list.forEach(_ -> System.out.println());
199+
list.forEach(unused -> System.out.println());
200+
}
201+
}
202+
""")
203+
// TODO(kak): we probably should remove unnecessary parentheses from LambdaExpressionTrees.
204+
// Note that the parentheses are not a ParenthesizedTree node: In the AST, when you have
205+
// (_) -> ..., the (_) is not represented as a ParenthesizedTree node wrapping an
206+
// expression. Instead, the parentheses are just syntactical tokens that are part of the
207+
// LambdaExpressionTree node itself.
208+
.expectUnchanged()
209+
.doTest();
210+
}
211+
179212
@Test
180213
public void unaryPostFixParenthesesNotNeeded() {
181214
testHelper

0 commit comments

Comments
 (0)