Skip to content

Commit 187a94f

Browse files
SONARJAVA-5083 Add unit test samples for Java 22 unnamed variables and patterns (#4907)
1 parent 353e76f commit 187a94f

4 files changed

Lines changed: 30 additions & 13 deletions

File tree

java-checks-test-sources/default/src/main/java/checks/unused/UnusedLocalVariableCheck.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,18 +315,25 @@ record ColoredPoint(Point p, String color) { }
315315

316316
void unnamedVariablesUseCases(Queue<Ball> queue, BallHolder<? extends Ball> ballHolder, ColoredPoint coloredPoint) {
317317
int total = 0;
318+
int _ = 1 + 1;
319+
java.util.function.IntUnaryOperator _ = (int _) -> 0;
320+
java.util.function.IntUnaryOperator _ = _ -> 0;
321+
java.util.function.IntBinaryOperator _ = (_,_) -> 0;
322+
java.util.function.IntBinaryOperator _ = (int _, int _) -> 0;
318323
for(Object _ : queue) { // Compliant
319324
total++;
320325
}
321326
System.out.println(total);
322-
327+
for (int i = 0, _ = 1 + 1; i < 2; i++) {
328+
System.out.println(i);
329+
}
323330
while(queue.size() > 2) {
324331
var a = queue.remove();
325332
var _ = queue.remove(); // Compliant
326333
System.out.println(a);
327334
}
328335

329-
try {
336+
try (var _ = new java.io.FileInputStream("foo.txt")) {
330337
queue.remove();
331338
} catch (Exception _) { // Compliant
332339
System.out.println("Exception");

java-checks/src/main/java/org/sonar/java/checks/naming/BadLocalVariableNameCheck.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.sonar.plugins.java.api.tree.ClassTree;
3030
import org.sonar.plugins.java.api.tree.ForEachStatement;
3131
import org.sonar.plugins.java.api.tree.ForStatementTree;
32+
import org.sonar.plugins.java.api.tree.IdentifierTree;
3233
import org.sonar.plugins.java.api.tree.Tree;
3334
import org.sonar.plugins.java.api.tree.VariableTree;
3435

@@ -92,8 +93,9 @@ public void visitCatch(CatchTree tree) {
9293

9394
@Override
9495
public void visitVariable(VariableTree tree) {
95-
if (!pattern.matcher(tree.simpleName().name()).matches() && !isLocalConstant(tree)) {
96-
context.reportIssue(this, tree.simpleName(), "Rename this local variable to match the regular expression '" + format + "'.");
96+
IdentifierTree simpleName = tree.simpleName();
97+
if (!simpleName.isUnnamedVariable() && !pattern.matcher(simpleName.name()).matches() && !isLocalConstant(tree)) {
98+
context.reportIssue(this, simpleName, "Rename this local variable to match the regular expression '" + format + "'.");
9799
}
98100
super.visitVariable(tree);
99101
}

java-checks/src/main/java/org/sonar/java/checks/unused/UnusedLocalVariableCheck.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,17 @@ public void visitNode(Tree tree) {
7272
public void leaveNode(Tree tree) {
7373
if (tree.is(Tree.Kind.VARIABLE)) {
7474
VariableTree variable = (VariableTree) tree;
75-
String name = variable.simpleName().name();
76-
boolean unresolved = UNRESOLVED_IDENTIFIERS_AND_SWITCH_CASE_VISITOR.isUnresolved(name);
77-
if (!unresolved && isProperLocalVariable(variable) && isUnused(variable.symbol())) {
78-
QuickFixHelper.newIssue(context)
79-
.forRule(this)
80-
.onTree(variable.simpleName())
81-
.withMessage(String.format(MESSAGE, name))
82-
.withQuickFixes(() -> computeQuickFix(variable))
83-
.report();
75+
IdentifierTree simpleName = variable.simpleName();
76+
if (!simpleName.isUnnamedVariable()) {
77+
boolean unresolved = UNRESOLVED_IDENTIFIERS_AND_SWITCH_CASE_VISITOR.isUnresolved(simpleName.name());
78+
if (!unresolved && isProperLocalVariable(variable) && isUnused(variable.symbol())) {
79+
QuickFixHelper.newIssue(context)
80+
.forRule(this)
81+
.onTree(simpleName)
82+
.withMessage(String.format(MESSAGE, simpleName.name()))
83+
.withQuickFixes(() -> computeQuickFix(variable))
84+
.report();
85+
}
8486
}
8587
}
8688

java-checks/src/test/files/checks/naming/BadLocalVariableNameNoncompliant.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import java.util.function.IntUnaryOperator;
2+
13
class BadLocalVariableName {
24
void method(
35
int BAD_FORMAL_PARAMETER // Noncompliant {{Rename this local variable to match the regular expression '^[a-z][a-zA-Z0-9]*$'.}}
@@ -36,4 +38,8 @@ void forEachMethod() {
3638
}
3739
}
3840

41+
void foo() {
42+
IntUnaryOperator f1 = (int _) -> 0; // Compliant, unnamed variable
43+
IntUnaryOperator f2 = _ -> 0; // Compliant, unnamed variable
44+
}
3945
}

0 commit comments

Comments
 (0)