Skip to content

Commit

Permalink
Don't complain about unused variables named _
Browse files Browse the repository at this point in the history
See https://openjdk.org/jeps/456

Fixes #4451

PiperOrigin-RevId: 652904059
  • Loading branch information
cushon authored and Error Prone Team committed Jul 16, 2024
1 parent 7f90df7 commit 6bd568d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public static boolean isAtLeast21() {
return FEATURE >= 21;
}

/** Returns true if the current runtime is JDK 22 or newer. */
public static boolean isAtLeast22() {
return FEATURE >= 22;
}

/**
* Returns the latest {@code --release} version.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ public final class UnusedVariable extends BugChecker implements CompilationUnitT
this.exemptNames =
ImmutableSet.<String>builder()
.add("ignored")
.add("") // `var _ = ...` is handled as an empty variable name
.addAll(flags.getListOrEmpty("Unused:exemptNames"))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1679,4 +1679,19 @@ public void parameterUsedInOverride() {
.expectUnchanged()
.doTest();
}

@Test
public void underscoreVariable() {
assumeTrue(RuntimeVersion.isAtLeast22());
refactoringHelper
.addInputLines(
"Test.java",
"class Test {",
" public static void main(String[] args) {",
" var _ = new Object();",
" }",
"}")
.expectUnchanged()
.doTest();
}
}

0 comments on commit 6bd568d

Please sign in to comment.