Skip to content

Commit c02aff7

Browse files
SONARJAVA-6086 S1172 should raise on main in Java 25 (#5450)
1 parent a0a1b50 commit c02aff7

5 files changed

Lines changed: 46 additions & 5 deletions

File tree

java-checks-test-sources/default/src/main/files/non-compiling/checks/unused/UnusedMethodParameterCheck.java renamed to java-checks-test-sources/default/src/main/files/non-compiling/checks/unused/UnusedMethodParameterCheckSample.java

File renamed without changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package checks.unused;
2+
3+
public class UnusedMethodParameterCheckMainSample {
4+
public static void main(String[] args) { // Noncompliant {{Remove this unused method parameter "args".}}
5+
System.out.println("Hello World!");
6+
}
7+
8+
void main() { // Compliant
9+
System.out.println("Hello World!");
10+
}
11+
}

java-checks-test-sources/default/src/main/java/checks/unused/UnusedMethodParameterCheck.java renamed to java-checks-test-sources/default/src/main/java/checks/unused/UnusedMethodParameterCheckSample.java

File renamed without changes.

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.sonar.java.reporting.JavaTextEdit;
3434
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
3535
import org.sonar.plugins.java.api.JavaFileScannerContext;
36+
import org.sonar.plugins.java.api.JavaVersion;
3637
import org.sonar.plugins.java.api.semantic.MethodMatchers;
3738
import org.sonar.plugins.java.api.semantic.Symbol;
3839
import org.sonar.plugins.java.api.semantic.SymbolMetadata;
@@ -159,14 +160,23 @@ private static List<JavaQuickFix> createQuickFixes(MethodTree methodTree, List<I
159160
}
160161

161162
private boolean isExcluded(MethodTree tree) {
162-
return MethodTreeUtils.isMainMethod(tree, context.getJavaVersion())
163+
return isMainWithRequiredArgs(tree)
163164
|| isAnnotated(tree)
164165
|| isOverriding(tree)
165166
|| isSerializableMethod(tree)
166167
|| isDesignedForExtension(tree)
167168
|| isUsedAsMethodReference(tree);
168169
}
169170

171+
/**
172+
* True if the argument is the main method and the argument cannot be removed,
173+
* which is the case before Java 25.
174+
*/
175+
private boolean isMainWithRequiredArgs(MethodTree tree) {
176+
JavaVersion javaVersion = context.getJavaVersion();
177+
return !javaVersion.isJava25Compatible() && MethodTreeUtils.isMainMethod(tree, javaVersion);
178+
}
179+
170180
private static boolean isAnnotated(MethodTree tree) {
171181
// If any annotation doesn't match the @SuppressWarning then mark the method as annotated.
172182
return tree.modifiers().annotations().stream().anyMatch(annotation -> !isExcludedLiteral(annotation));

java-checks/src/test/java/org/sonar/java/checks/unused/UnusedMethodParameterCheckTest.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,45 @@
1818

1919
import org.junit.jupiter.api.Test;
2020
import org.sonar.java.checks.verifier.CheckVerifier;
21+
import org.sonar.plugins.java.api.JavaFileScanner;
2122

2223
import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPath;
2324
import static org.sonar.java.checks.verifier.TestUtils.nonCompilingTestSourcesPath;
2425

2526
class UnusedMethodParameterCheckTest {
27+
private static final JavaFileScanner CHECK = new UnusedMethodParameterCheck();
2628

2729
@Test
2830
void test() {
2931
CheckVerifier.newVerifier()
30-
.onFile(mainCodeSourcesPath("checks/unused/UnusedMethodParameterCheck.java"))
31-
.withCheck(new UnusedMethodParameterCheck())
32+
.onFile(mainCodeSourcesPath("checks/unused/UnusedMethodParameterCheckSample.java"))
33+
.withCheck(CHECK)
34+
.verifyIssues();
35+
}
36+
37+
@Test
38+
void test_main_method_java21() {
39+
CheckVerifier.newVerifier()
40+
.onFile(mainCodeSourcesPath("checks/unused/UnusedMethodParameterCheckMainSample.java"))
41+
.withCheck(CHECK)
42+
.withJavaVersion(21)
43+
.verifyNoIssues();
44+
}
45+
46+
@Test
47+
void test_main_method_java25() {
48+
CheckVerifier.newVerifier()
49+
.onFile(mainCodeSourcesPath("checks/unused/UnusedMethodParameterCheckMainSample.java"))
50+
.withCheck(CHECK)
51+
.withJavaVersion(25)
3252
.verifyIssues();
3353
}
3454

3555
@Test
3656
void test_non_compiling() {
3757
CheckVerifier.newVerifier()
38-
.onFile(nonCompilingTestSourcesPath("checks/unused/UnusedMethodParameterCheck.java"))
39-
.withCheck(new UnusedMethodParameterCheck())
58+
.onFile(nonCompilingTestSourcesPath("checks/unused/UnusedMethodParameterCheckSample.java"))
59+
.withCheck(CHECK)
4060
.verifyIssues();
4161
}
4262
}

0 commit comments

Comments
 (0)