From 187a94fc5300b86e098198e6bb0da82bffc867c0 Mon Sep 17 00:00:00 2001 From: Alban Auzeill Date: Tue, 15 Oct 2024 10:19:04 +0200 Subject: [PATCH] SONARJAVA-5083 Add unit test samples for Java 22 unnamed variables and patterns (#4907) --- .../unused/UnusedLocalVariableCheck.java | 11 ++++++++-- .../naming/BadLocalVariableNameCheck.java | 6 ++++-- .../unused/UnusedLocalVariableCheck.java | 20 ++++++++++--------- .../BadLocalVariableNameNoncompliant.java | 6 ++++++ 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/java-checks-test-sources/default/src/main/java/checks/unused/UnusedLocalVariableCheck.java b/java-checks-test-sources/default/src/main/java/checks/unused/UnusedLocalVariableCheck.java index 9cc276a9903..9e7dfc2b051 100644 --- a/java-checks-test-sources/default/src/main/java/checks/unused/UnusedLocalVariableCheck.java +++ b/java-checks-test-sources/default/src/main/java/checks/unused/UnusedLocalVariableCheck.java @@ -315,18 +315,25 @@ record ColoredPoint(Point p, String color) { } void unnamedVariablesUseCases(Queue queue, BallHolder ballHolder, ColoredPoint coloredPoint) { int total = 0; + int _ = 1 + 1; + java.util.function.IntUnaryOperator _ = (int _) -> 0; + java.util.function.IntUnaryOperator _ = _ -> 0; + java.util.function.IntBinaryOperator _ = (_,_) -> 0; + java.util.function.IntBinaryOperator _ = (int _, int _) -> 0; for(Object _ : queue) { // Compliant total++; } System.out.println(total); - + for (int i = 0, _ = 1 + 1; i < 2; i++) { + System.out.println(i); + } while(queue.size() > 2) { var a = queue.remove(); var _ = queue.remove(); // Compliant System.out.println(a); } - try { + try (var _ = new java.io.FileInputStream("foo.txt")) { queue.remove(); } catch (Exception _) { // Compliant System.out.println("Exception"); diff --git a/java-checks/src/main/java/org/sonar/java/checks/naming/BadLocalVariableNameCheck.java b/java-checks/src/main/java/org/sonar/java/checks/naming/BadLocalVariableNameCheck.java index 0eb587161c6..2ba45a12288 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/naming/BadLocalVariableNameCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/naming/BadLocalVariableNameCheck.java @@ -29,6 +29,7 @@ import org.sonar.plugins.java.api.tree.ClassTree; import org.sonar.plugins.java.api.tree.ForEachStatement; import org.sonar.plugins.java.api.tree.ForStatementTree; +import org.sonar.plugins.java.api.tree.IdentifierTree; import org.sonar.plugins.java.api.tree.Tree; import org.sonar.plugins.java.api.tree.VariableTree; @@ -92,8 +93,9 @@ public void visitCatch(CatchTree tree) { @Override public void visitVariable(VariableTree tree) { - if (!pattern.matcher(tree.simpleName().name()).matches() && !isLocalConstant(tree)) { - context.reportIssue(this, tree.simpleName(), "Rename this local variable to match the regular expression '" + format + "'."); + IdentifierTree simpleName = tree.simpleName(); + if (!simpleName.isUnnamedVariable() && !pattern.matcher(simpleName.name()).matches() && !isLocalConstant(tree)) { + context.reportIssue(this, simpleName, "Rename this local variable to match the regular expression '" + format + "'."); } super.visitVariable(tree); } diff --git a/java-checks/src/main/java/org/sonar/java/checks/unused/UnusedLocalVariableCheck.java b/java-checks/src/main/java/org/sonar/java/checks/unused/UnusedLocalVariableCheck.java index cce7297e432..99015b15e70 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/unused/UnusedLocalVariableCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/unused/UnusedLocalVariableCheck.java @@ -72,15 +72,17 @@ public void visitNode(Tree tree) { public void leaveNode(Tree tree) { if (tree.is(Tree.Kind.VARIABLE)) { VariableTree variable = (VariableTree) tree; - String name = variable.simpleName().name(); - boolean unresolved = UNRESOLVED_IDENTIFIERS_AND_SWITCH_CASE_VISITOR.isUnresolved(name); - if (!unresolved && isProperLocalVariable(variable) && isUnused(variable.symbol())) { - QuickFixHelper.newIssue(context) - .forRule(this) - .onTree(variable.simpleName()) - .withMessage(String.format(MESSAGE, name)) - .withQuickFixes(() -> computeQuickFix(variable)) - .report(); + IdentifierTree simpleName = variable.simpleName(); + if (!simpleName.isUnnamedVariable()) { + boolean unresolved = UNRESOLVED_IDENTIFIERS_AND_SWITCH_CASE_VISITOR.isUnresolved(simpleName.name()); + if (!unresolved && isProperLocalVariable(variable) && isUnused(variable.symbol())) { + QuickFixHelper.newIssue(context) + .forRule(this) + .onTree(simpleName) + .withMessage(String.format(MESSAGE, simpleName.name())) + .withQuickFixes(() -> computeQuickFix(variable)) + .report(); + } } } diff --git a/java-checks/src/test/files/checks/naming/BadLocalVariableNameNoncompliant.java b/java-checks/src/test/files/checks/naming/BadLocalVariableNameNoncompliant.java index 4c341c7ea49..1f88c4cc4bc 100644 --- a/java-checks/src/test/files/checks/naming/BadLocalVariableNameNoncompliant.java +++ b/java-checks/src/test/files/checks/naming/BadLocalVariableNameNoncompliant.java @@ -1,3 +1,5 @@ +import java.util.function.IntUnaryOperator; + class BadLocalVariableName { void method( 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() { } } + void foo() { + IntUnaryOperator f1 = (int _) -> 0; // Compliant, unnamed variable + IntUnaryOperator f2 = _ -> 0; // Compliant, unnamed variable + } }