Skip to content

Commit

Permalink
SONARJAVA-5083 Add unit test samples for Java 22 unnamed variables an…
Browse files Browse the repository at this point in the history
…d patterns (#4907)
  • Loading branch information
alban-auzeill authored Oct 15, 2024
1 parent 353e76f commit 187a94f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,18 +315,25 @@ record ColoredPoint(Point p, String color) { }

void unnamedVariablesUseCases(Queue<Ball> queue, BallHolder<? extends Ball> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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]*$'.}}
Expand Down Expand Up @@ -36,4 +38,8 @@ void forEachMethod() {
}
}

void foo() {
IntUnaryOperator f1 = (int _) -> 0; // Compliant, unnamed variable
IntUnaryOperator f2 = _ -> 0; // Compliant, unnamed variable
}
}

0 comments on commit 187a94f

Please sign in to comment.