Skip to content

Commit 9444b20

Browse files
SONARJAVA-6123 Issues for rule S8433 (#5463)
1 parent 3dab09a commit 9444b20

4 files changed

Lines changed: 5 additions & 30 deletions

File tree

its/autoscan/src/test/java/org/sonar/java/it/AutoScanTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public void javaCheckTestSources() throws Exception {
199199
softly.assertThat(newDiffs).containsExactlyInAnyOrderElementsOf(knownDiffs.values());
200200
softly.assertThat(newTotal).isEqualTo(knownTotal);
201201
softly.assertThat(rulesCausingFPs).hasSize(10);
202-
softly.assertThat(rulesNotReporting).hasSize(16);
202+
softly.assertThat(rulesNotReporting).hasSize(17);
203203

204204
/**
205205
* 4. Check total number of differences (FPs + FNs)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S8433",
3-
"hasTruePositives": true,
3+
"hasTruePositives": false,
44
"falseNegatives": 0,
55
"falsePositives": 0
66
}

java-checks-test-sources/default/src/main/files/non-compiling/checks/FlexibleConstructorBodyValidationCheckSample.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public SpringCoffee(String name) {
133133
static class ImplicitSuperCoffee extends Coffee {
134134
public ImplicitSuperCoffee(int water) {
135135
// Implicit super() call here
136-
if (water < 0) { // Noncompliant
136+
if (water < 0) {
137137
throw new IllegalArgumentException();
138138
}
139139
}
@@ -167,23 +167,4 @@ public NestedIfCoffee(int water, int milk) {
167167
}
168168
}
169169

170-
// Test when there is no superclass
171-
static class NoSuperclassCoffee {
172-
private int water;
173-
private int milk;
174-
private static final int MAX_SIZE = 500;
175-
176-
public NoSuperclassCoffee(int water, int milk) {
177-
if (water + milk > MAX_SIZE) { // Compliant: no superclass and no this(...) call
178-
throw new IllegalArgumentException();
179-
}
180-
}
181-
182-
public NoSuperclassCoffee(int water) {
183-
this(water, 0);
184-
if (water <= 0) { // Noncompliant {{Move this validation logic before the super() or this() call.}}
185-
throw new IllegalArgumentException();
186-
}
187-
}
188-
}
189170
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.sonar.check.Rule;
2424
import org.sonar.plugins.java.api.semantic.MethodMatchers;
2525
import org.sonar.plugins.java.api.semantic.Symbol;
26-
import org.sonar.plugins.java.api.semantic.Type;
2726
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
2827
import org.sonar.plugins.java.api.tree.ExpressionStatementTree;
2928
import org.sonar.plugins.java.api.tree.ExpressionTree;
@@ -65,8 +64,8 @@ public class FlexibleConstructorBodyValidationCheck extends FlexibleConstructorV
6564
@Override
6665
void validateConstructor(MethodTree constructor, List<StatementTree> body, int constructorCallIndex) {
6766
if (constructorCallIndex == body.size() - 1
68-
|| (constructorCallIndex == -1 && hasNoExplicitSuperClass(constructor))) {
69-
// No statements after constructor call or no superclass and no constructor call
67+
|| (constructorCallIndex == -1)) {
68+
// No statements after constructor call or no constructor call
7069
return;
7170
}
7271
// Collect constructor parameters for analysis
@@ -82,11 +81,6 @@ void validateConstructor(MethodTree constructor, List<StatementTree> body, int c
8281
}
8382
}
8483

85-
private static boolean hasNoExplicitSuperClass(MethodTree constructor) {
86-
Type superClass = constructor.symbol().enclosingClass().superClass();
87-
return (superClass == null || superClass.is("java.lang.Object"));
88-
}
89-
9084
/**
9185
* Check if a statement is a validation statement (conditionally throws exception or calls validation method).
9286
*

0 commit comments

Comments
 (0)