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:S1170
Issue:
S1170 reports public final int max = 10; in a class (“Make this final field static too”). The same field in an enum isn’t reported, even though every enum constant then carries its own copy of the value.
ConstantsShouldBeStaticFinalCheck.nodesToVisit only subscribes to Tree.Kind.CLASS (ConstantsShouldBeStaticFinalCheck.java:42 at 8.34.0.44906, unchanged on master at e2ff24d5b, 2026-10-08), so enum bodies are never visited. Adding Tree.Kind.ENUM should fix it.
Reproducer:
seed/Limits.java:
public class Limits {
public final int max = 10; // Noncompliant: S1170 reported
}
variant/Limits.java:
public enum Limits {
ONE;
public final int max = 10; // same field, no S1170
}
Expected behavior:
S1170 on the field in variant/Limits.java, as on the one in seed/Limits.java: the field is final and initialised with a constant.
How to reproduce:
# one project per file
sonar-scanner -Dsonar.projectKey=<key> -Dsonar.sources=. \
-Dsonar.java.binaries=. -Dsonar.scm.disabled=true
See also False negative java:S1068: an unused private field declared in an enum is not reported.
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: S1170
--- SEED -- expected: reported (seed/)
java:S1220 line - Move this file to a named package.
java:S1170 line 2 Make this final field static too. <== S1170
[S1170: 1]
--- VARIANT -- expected: reported; actual: not reported (variant/)
java:S1220 line - Move this file to a named package.
[S1170: 0]