Skip to content

Commit e9e93bd

Browse files
SONARJAVA-6121 : Fix FP for rule S2694 raising on compact source files (#5465)
Co-authored-by: Tomasz Tylenda <tomasz.tylenda@sonarsource.com>
1 parent 3113445 commit e9e93bd

4 files changed

Lines changed: 37 additions & 13 deletions

File tree

java-checks/src/test/files/checks/InnerStaticClassesCheckSample.java renamed to java-checks-test-sources/default/src/main/java/checks/InnerStaticClassesCheck/InnerStaticClassesCheckSample.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package foo;
1+
package checks.InnerStaticClassesCheck;
22

33
class Fruit {
44
private int value;
@@ -113,7 +113,7 @@ class A {
113113

114114
class B { // Compliant inner class refers to field.
115115
Bar foo() {
116-
return new String() {
116+
return new Bar() {
117117
public String toString() { return ""+fielda;}
118118
};
119119
}
@@ -125,14 +125,14 @@ Object foo() {
125125
}
126126

127127
class SubB extends B { // Compliant since super class is not static
128-
Object foo() {
129-
return new Object();
128+
Bar foo() {
129+
return new Bar();
130130
}
131131
}
132132

133133
class SubSubB extends SubB { // Compliant since super class is not static
134-
Object foo() {
135-
return new Object();
134+
Bar foo() {
135+
return new Bar();
136136
}
137137
}
138138

@@ -146,19 +146,19 @@ class InnerArrayList extends java.util.ArrayList<String> { // Noncompliant
146146

147147
}
148148

149-
public class Extendable {
149+
class Extendable {
150150
protected int field;
151151
}
152152

153-
public class Foo extends Extendable {
153+
class Foo extends Extendable {
154154

155155
class Inner { // Compliant field is refered.
156156
void plop(){
157157
System.out.println(field);
158158
}
159159
}
160160
}
161-
public class A1 {
161+
class A1 {
162162
private int field;
163163

164164
class B { // Noncompliant
@@ -177,7 +177,7 @@ public String toString() {
177177
void usedInAnonymousClass() {
178178
final String myString = "";
179179
method(new toImplement() { // Compliant my String is used.
180-
void fun(String s) {
180+
public void fun(String s) {
181181
myString.length();
182182
}
183183
});
@@ -200,10 +200,10 @@ public Inner() {
200200
}
201201
}
202202
}
203-
public class A4 {
203+
class A4 {
204204
public void m(int a) {
205205
class B { // compliant cannot be made static
206-
void foo() {
206+
int foo() {
207207
return a;
208208
}
209209
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
void main() { // just to make the file is a compact source file
3+
}
4+
5+
class A { // Compliant, FP raised here
6+
}
7+
8+
class C {
9+
class D { // Noncompliant
10+
}
11+
}

java-checks/src/main/java/org/sonar/java/checks/InnerStaticClassesCheck.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public void scanFile(final JavaFileScannerContext context) {
4949

5050
@Override
5151
public void visitClass(ClassTree tree) {
52+
if (tree.is(Tree.Kind.IMPLICIT_CLASS)) {
53+
// this rule doesn't apply to implicit classes (compact source files)
54+
return;
55+
}
5256
Symbol.TypeSymbol symbol = tree.symbol();
5357
outerClasses.push(symbol);
5458
atLeastOneReference.push(Boolean.FALSE);

java-checks/src/test/java/org/sonar/java/checks/InnerStaticClassesCheckTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,23 @@
1919
import org.junit.jupiter.api.Test;
2020
import org.sonar.java.checks.verifier.CheckVerifier;
2121

22+
import static org.sonar.java.checks.verifier.TestUtils.mainCodeSourcesPath;
23+
2224
class InnerStaticClassesCheckTest {
2325

2426
@Test
2527
void test() {
2628
CheckVerifier.newVerifier()
27-
.onFile("src/test/files/checks/InnerStaticClassesCheckSample.java")
29+
.onFile(mainCodeSourcesPath("checks/InnerStaticClassesCheck/InnerStaticClassesCheckSample.java"))
2830
.withCheck(new InnerStaticClassesCheck())
2931
.verifyIssues();
3032
}
3133

34+
@Test
35+
void testCompactSourceFiles() {
36+
CheckVerifier.newVerifier()
37+
.onFile(mainCodeSourcesPath("checks/InnerStaticClassesCheck/InnerStaticClassesCompactSourceFileSample.java"))
38+
.withCheck(new InnerStaticClassesCheck())
39+
.verifyNoIssues();
40+
}
3241
}

0 commit comments

Comments
 (0)