Skip to content

Commit 6bd568d

Browse files
cushonError Prone Team
authored and
Error Prone Team
committed
Don't complain about unused variables named _
See https://openjdk.org/jeps/456 Fixes #4451 PiperOrigin-RevId: 652904059
1 parent 7f90df7 commit 6bd568d

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

check_api/src/main/java/com/google/errorprone/util/RuntimeVersion.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public static boolean isAtLeast21() {
7979
return FEATURE >= 21;
8080
}
8181

82+
/** Returns true if the current runtime is JDK 22 or newer. */
83+
public static boolean isAtLeast22() {
84+
return FEATURE >= 22;
85+
}
86+
8287
/**
8388
* Returns the latest {@code --release} version.
8489
*

core/src/main/java/com/google/errorprone/bugpatterns/UnusedVariable.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ public final class UnusedVariable extends BugChecker implements CompilationUnitT
198198
this.exemptNames =
199199
ImmutableSet.<String>builder()
200200
.add("ignored")
201+
.add("") // `var _ = ...` is handled as an empty variable name
201202
.addAll(flags.getListOrEmpty("Unused:exemptNames"))
202203
.build();
203204

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,4 +1679,19 @@ public void parameterUsedInOverride() {
16791679
.expectUnchanged()
16801680
.doTest();
16811681
}
1682+
1683+
@Test
1684+
public void underscoreVariable() {
1685+
assumeTrue(RuntimeVersion.isAtLeast22());
1686+
refactoringHelper
1687+
.addInputLines(
1688+
"Test.java",
1689+
"class Test {",
1690+
" public static void main(String[] args) {",
1691+
" var _ = new Object();",
1692+
" }",
1693+
"}")
1694+
.expectUnchanged()
1695+
.doTest();
1696+
}
16821697
}

0 commit comments

Comments
 (0)