Skip to content

Commit 4ff6e79

Browse files
SONARJAVA-6119 S3078 report on compact source files (#5456)
1 parent 3dea37c commit 4ff6e79

4 files changed

Lines changed: 46 additions & 3 deletions

File tree

java-checks-test-sources/default/src/main/java/checks/VolatileVariablesOperationsCheck.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,25 @@ void method() {
109109
value++; // Noncompliant
110110
}
111111
}
112+
113+
record Vinyl(String singer, String title, int year) {
114+
static volatile int counter = 0;
115+
static int nonVolatile = 0;
116+
117+
void method() {
118+
counter++; // Noncompliant
119+
nonVolatile++;
120+
}
121+
}
122+
123+
class Container {
124+
static volatile int counter = 0;
125+
static int nonVolatile = 0;
126+
}
127+
128+
interface MyInterface {
129+
default void method() {
130+
Container.counter++; // Noncompliant
131+
Container.nonVolatile++;
132+
}
133+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
volatile int count1 = 0;
2+
int nonVolatileCount1 = 0;
3+
volatile boolean boo1 = false;
4+
5+
void main() {
6+
count1++; // Noncompliant {{Use an "AtomicInteger" for this field; its operations are atomic.}}
7+
nonVolatileCount1++;
8+
boo1 = !boo1; // Noncompliant {{Use an "AtomicBoolean" for this field; its operations are atomic.}}
9+
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,17 @@ private void reportIssueIfNotInExcludedContext(Tree tree, String recommendedType
125125
}
126126
break;
127127
case ENUM,
128-
CLASS:
129-
if (((ClassTree) current).simpleName() == null) {
128+
CLASS,
129+
INTERFACE,
130+
RECORD,
131+
IMPLICIT_CLASS:
132+
if (!current.is(Tree.Kind.IMPLICIT_CLASS) && ((ClassTree) current).simpleName() == null) {
130133
return;
131134
}
132-
// we got to a non anonymous class, we can safely raise an issue
135+
// we got to a non-anonymous class or implicit class in compact source file, we can safely raise an issue
133136
foundClass = true;
134137
break;
138+
135139
}
136140
current = current.parent();
137141
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,12 @@ void test() {
3030
.withCheck(new VolatileVariablesOperationsCheck())
3131
.verifyIssues();
3232
}
33+
34+
@Test
35+
void test_compact_source() {
36+
CheckVerifier.newVerifier()
37+
.onFile(mainCodeSourcesPath("checks/VolatileVariablesOperationsCheckCompactSource.java"))
38+
.withCheck(new VolatileVariablesOperationsCheck())
39+
.verifyIssues();
40+
}
3341
}

0 commit comments

Comments
 (0)