Skip to content

Commit 0d7703c

Browse files
SONARJAVA-4238 S2924 should not report on non-private rules declared inside of abstract classes (#4886)
1 parent b3741f0 commit 0d7703c

9 files changed

Lines changed: 92 additions & 13 deletions

File tree

its/autoscan/src/test/resources/autoscan/autoscan-diff-by-rules.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@
10461046
{
10471047
"ruleKey": "S2187",
10481048
"hasTruePositives": true,
1049-
"falseNegatives": 11,
1049+
"falseNegatives": 12,
10501050
"falsePositives": 1
10511051
},
10521052
{
@@ -1400,7 +1400,7 @@
14001400
{
14011401
"ruleKey": "S2699",
14021402
"hasTruePositives": true,
1403-
"falseNegatives": 143,
1403+
"falseNegatives": 151,
14041404
"falsePositives": 1
14051405
},
14061406
{
@@ -1466,7 +1466,7 @@
14661466
{
14671467
"ruleKey": "S2924",
14681468
"hasTruePositives": false,
1469-
"falseNegatives": 8,
1469+
"falseNegatives": 11,
14701470
"falsePositives": 0
14711471
},
14721472
{
@@ -1682,7 +1682,7 @@
16821682
{
16831683
"ruleKey": "S3577",
16841684
"hasTruePositives": true,
1685-
"falseNegatives": 44,
1685+
"falseNegatives": 46,
16861686
"falsePositives": 0
16871687
},
16881688
{
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S2187",
33
"hasTruePositives": true,
4-
"falseNegatives": 11,
4+
"falseNegatives": 12,
55
"falsePositives": 1
6-
}
6+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S2699",
33
"hasTruePositives": true,
4-
"falseNegatives": 150,
4+
"falseNegatives": 151,
55
"falsePositives": 1
6-
}
6+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S2924",
33
"hasTruePositives": false,
4-
"falseNegatives": 8,
4+
"falseNegatives": 11,
55
"falsePositives": 0
6-
}
6+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S3577",
33
"hasTruePositives": true,
4-
"falseNegatives": 45,
4+
"falseNegatives": 46,
55
"falsePositives": 0
6-
}
6+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package checks.tests;
2+
3+
import java.nio.file.Path;
4+
import org.junit.jupiter.api.io.TempDir;
5+
6+
// https://community.sonarsource.com/t/possible-fp-for-java-tempdir-declared-in-super-class/60836
7+
// https://sonarsource.atlassian.net/browse/SONARJAVA-4238
8+
public abstract class UnusedTestRuleCheck_Protected {
9+
@TempDir
10+
protected Path tempDirOldFP; // Compliant FP as used in a subclass - compliant because not private
11+
}
12+
13+
// Test abstract private
14+
abstract class AbstractTestCase {
15+
16+
@TempDir
17+
private Path tempDir; // Noncompliant {{Remove this unused "TempDir".}}
18+
// increases AutoScan FNs
19+
20+
void test() {
21+
}
22+
23+
}
24+
25+
// Test non-abstract private
26+
class ClassTestCase { // increases AutoScan FNs
27+
28+
@TempDir
29+
private Path tempDir; // Noncompliant {{Remove this unused "TempDir".}}
30+
// increases AutoScan FNs
31+
32+
void test() {
33+
}
34+
35+
}
36+
37+
// Test non-abstract protected
38+
class ClassTestCase2 {
39+
40+
@TempDir
41+
protected Path tempDir; // Noncompliant {{Remove this unused "TempDir".}}
42+
// increases AutoScan FNs
43+
44+
void test() {
45+
}
46+
47+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package checks.tests;
2+
3+
import java.nio.file.Files;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
// AutoScan S3577 Increases FN to 46
8+
public class UnusedTestRuleCheck_UseProtected extends UnusedTestRuleCheck_Protected {
9+
@BeforeEach
10+
void setup() throws Exception {
11+
Files.createTempFile(tempDirOldFP, "test", "");
12+
}
13+
@Test
14+
void test() {
15+
// AutoScan S2699 Increases FN to 151
16+
}
17+
}

java-checks/src/main/java/org/sonar/java/checks/unused/UnusedTestRuleCheck.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
import java.util.List;
2424
import java.util.Set;
2525
import org.sonar.check.Rule;
26-
import org.sonarsource.analyzer.commons.collections.SetUtils;
26+
import org.sonar.java.model.ModifiersUtils;
2727
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
2828
import org.sonar.plugins.java.api.semantic.Symbol;
2929
import org.sonar.plugins.java.api.tree.ClassTree;
3030
import org.sonar.plugins.java.api.tree.MethodTree;
31+
import org.sonar.plugins.java.api.tree.Modifier;
3132
import org.sonar.plugins.java.api.tree.Tree;
3233
import org.sonar.plugins.java.api.tree.VariableTree;
34+
import org.sonarsource.analyzer.commons.collections.SetUtils;
3335

3436
@Rule(key = "S2924")
3537
public class UnusedTestRuleCheck extends IssuableSubscriptionVisitor {
@@ -47,11 +49,16 @@ public List<Tree.Kind> nodesToVisit() {
4749
@Override
4850
public void visitNode(Tree tree) {
4951
ClassTree classTree = (ClassTree) tree;
52+
boolean isAbstract = ModifiersUtils.hasModifier(classTree.modifiers(), Modifier.ABSTRACT);
5053
for (Tree member : classTree.members()) {
5154
if (member.is(Tree.Kind.VARIABLE)) {
5255
VariableTree variableTree = (VariableTree) member;
5356
Symbol symbol = variableTree.symbol();
5457
if ((isTestNameOrTemporaryFolderRule(symbol) || hasTempDirAnnotation(symbol)) && symbol.usages().isEmpty()) {
58+
// if class is abstract, then we need to check modifier - if not private, then it's okay
59+
if (isAbstract && !ModifiersUtils.hasModifier(variableTree.modifiers(), Modifier.PRIVATE)) {
60+
continue;
61+
}
5562
reportIssue(variableTree.simpleName(), "Remove this unused \"" + getSymbolType(symbol) + "\".");
5663
}
5764
} else if (member.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR)) {

java-checks/src/test/java/org/sonar/java/checks/unused/UnusedTestRuleCheckTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ void test_JUnit5() {
4242
.verifyIssues();
4343
}
4444

45+
@Test
46+
void test_UseProtected() {
47+
CheckVerifier.newVerifier()
48+
.onFile(testCodeSourcesPath("checks/tests/UnusedTestRuleCheck_Protected.java"))
49+
.withCheck(new UnusedTestRuleCheck())
50+
.verifyIssues();
51+
}
52+
4553
@Test
4654
void test_no_issues_without_semantic() {
4755
CheckVerifier.newVerifier()

0 commit comments

Comments
 (0)