Skip to content

SONARJAVA-5517 Modify rule S1481: Format code examples #4979

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 3 commits into from
May 1, 2025
Merged
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
72 changes: 36 additions & 36 deletions rules/S1481/java/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public int numberOfMinutes(int hours) {
[source,java,diff-id=2,diff-type=noncompliant]
----
public String name(Person p) {
return switch(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();
};
Expand All @@ -29,36 +29,36 @@ public String name(Person p) {
[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();
};
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;
}
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;
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");
}
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");
}
}
----

Expand All @@ -74,45 +74,45 @@ public int numberOfMinutes(int hours) {
[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();
};
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();
};
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;
}
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;
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");
}
public void foo() {
try (var _ = Files.createTempFile(directory, "temp", ".txt")) { // Compliant
System.out.println("file created");
}
}
----

Expand Down