Skip to content

SONARJAVA-5517 Modify S1481: add information about the java unnamed pattern #4975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion rules/S1481/java/metadata.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"quickfix": "partial"
"quickfix": "partial",
"tags": [
"java22", "unused"
]
}
97 changes: 95 additions & 2 deletions rules/S1481/java/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
include::../rationale.adoc[]

include::../how-to-fix-it.adoc[]
== How to fix it

The fix for this issue is straightforward. Once you ensure the unused variable is not part of an incomplete implementation, you just need to remove it.

Java 22 introduces the unnamed variable pattern ``++_++``. When a variable declared within a pattern match, an enhanced for loop, or a try with resource is unused, you should replace its name with the unnamed variable pattern to clearly indicate the intent not to use the variable.

=== Code examples

Expand All @@ -9,10 +13,55 @@ include::../how-to-fix-it.adoc[]
[source,java,diff-id=1,diff-type=noncompliant]
----
public int numberOfMinutes(int hours) {
int seconds = 0; // Noncompliant - seconds is unused
int seconds = 0; // Noncompliant: "seconds" is unused
return hours * 60;
}
----
[source,java,diff-id=2,diff-type=noncompliant]
----
public String name(Person p) {
return switch(p){
case User(String name, int age) -> name; // Noncompliant: "age" is unused replace it with the unnamed variable pattern (starting from Java 22)
default -> throw new IllegalArgumentException();
};
}
----
[source,java,diff-id=3,diff-type=noncompliant]
----
public String type(Person p) {
return switch (p) {
case User user -> "user"; // Noncompliant: "user" is unused replace it with the unnamed variable pattern (starting from Java 22)
default -> throw new IllegalArgumentException();
};
}
----
[source,java,diff-id=4,diff-type=noncompliant]
----
public int age(Person p) {
if (p instanceof User(String name, int age)) { // Noncompliant: "name" is unused replace it with the unnamed variable pattern (starting from Java 22)
return age;
}
}
----
[source,java,diff-id=5,diff-type=noncompliant]
----
public static int count(int[] elements) {
int count = 0;
for (var el : elements) { // Noncompliant: "el" is unused replace it with the unnamed variable pattern (starting from Java 22)
count++;
}
return count;
}
----
[source,java,diff-id=6,diff-type=noncompliant]
----
public void foo() {
try (var file = Files.createTempFile(directory, "temp", ".txt")) { // Noncompliant: "file" is unused replace it with the unnamed variable pattern (starting from Java 22)
System.out.println("file created");
}
}
----


==== Compliant solution

Expand All @@ -22,6 +71,50 @@ public int numberOfMinutes(int hours) {
return hours * 60;
}
----
[source,java,diff-id=2,diff-type=compliant]
----
public String name(Person p) {
return switch (p) {
case User(String name, _) -> name; // Compliant
default -> throw new IllegalArgumentException();
};
}
----
[source,java,diff-id=3,diff-type=compliant]
----
public String type(Person p) {
return switch (p) {
case User _ -> "user"; // Compliant
default -> throw new IllegalArgumentException();
};
}
----
[source,java,diff-id=4,diff-type=compliant]
----
public int age(Person p) {
if(p instanceof User(String _, int age)) { // Compliant
return age;
}
}
----
[source,java,diff-id=5,diff-type=compliant]
----
public static int count(int[] elements) {
int count = 0;
for (var _ : elements) { // Compliant
count++;
}
return count;
}
----
[source,java,diff-id=6,diff-type=compliant]
----
public void foo() {
try (var _ = Files.createTempFile(directory, "temp", ".txt")) { // Compliant
System.out.println("file created");
}
}
----

ifdef::env-github,rspecator-view[]

Expand Down