Product/version: SonarQube Community Build 26.9.0.129388 (sonar-java 8.41.0.47177); also 26.7.0.124771 (sonar-java 8.34.0.44906), sonar-scanner 8.1.0.6389
Language: Java
Rule: java:S1185
Issue:
Example.getId() in seed/Example.java just returns super.getId() and S1185 reports it. In variant/Example.java the same call has parentheses around it, return (super.getId());, and nothing is reported.
MethodOnlyCallsSuperCheck.isUselessSuperCall takes the return expression, and isCallToSuper needs it to be a METHOD_INVOCATION (MethodOnlyCallsSuperCheck.java:114 at 8.41.0.47177). The parenthesised call is a PARENTHESIZED_EXPRESSION, so the override isn’t seen as useless. Calling ExpressionUtils.skipParentheses on the return expression would fix it, and on each argument in callsWithSameParameters too, which only accepts bare identifiers.
Reproducer:
seed/Example.java:
class PersistentObject {
public Long getId() {
return 1L;
}
}
public class Example extends PersistentObject {
@Override
public Long getId() {
return super.getId(); // S1185 reported
}
}
variant/Example.java:
class PersistentObject {
public Long getId() {
return 1L;
}
}
public class Example extends PersistentObject {
@Override
public Long getId() {
return (super.getId()); // the same forwarding call; nothing reported
}
}
Expected behavior:
S1185 on getId in variant/Example.java (line 9), as on the one in seed/Example.java.
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: S1185
--- SEED -- expected: reported (seed/)
java:S1220 line - Move this file to a named package.
java:S1185 line 9 Remove this method to simply inherit it. <== S1185
[S1185: 1]
--- VARIANT -- expected: reported; actual: not reported (variant/)
java:S1220 line - Move this file to a named package.
[S1185: 0]