Product/version: SonarQube Community Build 26.9.0.129388 (sonar-java 8.41.0.47177), sonar-scanner 8.1.0.6389
Language: Java
Rule: java:S1172
Issue:
The rule description exempts unused parameters “in non-private methods that only throw or that have empty bodies”, with nothing about the class. public class WAUnusedParamFinal { public void foo(String s) {} } isn’t reported, as expected. Make the class final and S1172 reports s.
UnusedMethodParameterCheck.isDesignedForExtension drops the exemption when enclosingClass().isFinal() (“methods of final class can not be overridden”). The description only ties final classes to the Javadoc exemption (“in overridable methods (non-final, or not member of a final class, …)”), not to the empty-body one, and its example calls the empty method “designed to be extended”. So either the description should mention final classes for empty bodies too, or the check shouldn’t apply it there.
control/WAUnusedParamUsed.java shows the rule reporting as usual on a non-empty method of the same final class.
Reproducer:
seed/WAUnusedParamFinal.java:
public class WAUnusedParamFinal {
public void foo(String s) {
}
}
variant/WAUnusedParamFinal.java:
public final class WAUnusedParamFinal {
public void foo(String s) {
}
}
control/WAUnusedParamUsed.java:
public final class WAUnusedParamUsed {
public void foo(String s) {
System.out.println("x");
}
}
Expected behavior:
No S1172 on foo(String s) in the final WAUnusedParamFinal (empty body, not private), as for the version without final. Or, if the behavior is intended, the description updated to say so.
How to reproduce:
# one project per file
sonar-scanner -Dsonar.projectKey=<key> -Dsonar.sources=. \
-Dsonar.java.binaries=. -Dsonar.scm.disabled=true
Output of the commands above:
### SonarQube Community Build 26.9.0.129388 (sonar-java 8.41.0.47177), server reports 26.9.0.129388, default Sonar way profile rule: S1172
--- SEED -- expected: not reported (seed/)
java:S1220 line - Move this file to a named package.
java:S1186 line 2 Add a nested comment explaining why this method is empty, th
[S1172: 0]
--- VARIANT -- expected: not reported; actual: reported (variant/)
java:S1220 line - Move this file to a named package.
java:S1186 line 2 Add a nested comment explaining why this method is empty, th
java:S1172 line 2 Remove this unused method parameter "s". <== S1172
[S1172: 1]
--- CONTROL -- the same rule on the control file, expected: reported (control/)
java:S1220 line - Move this file to a named package.
java:S1172 line 2 Remove this unused method parameter "s". <== S1172
java:S106 line 3 Replace this use of System.out by a logger.
[S1172: 1]