From a1468213ce009061845dd9e03dffee857fb30f8a Mon Sep 17 00:00:00 2001 From: "romain.birling" Date: Tue, 24 Feb 2026 11:49:22 +0100 Subject: [PATCH 1/3] Relax rule : do not assume all fields are used in case any unknown method is crossed --- .../InitializeSubclassFieldsBeforeSuperSample.java | 13 +------------ .../InitializeSubclassFieldsBeforeSuperCheck.java | 4 ++-- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/java-checks-test-sources/default/src/main/files/non-compiling/checks/InitializeSubclassFieldsBeforeSuperSample.java b/java-checks-test-sources/default/src/main/files/non-compiling/checks/InitializeSubclassFieldsBeforeSuperSample.java index 3bc61de381..e606543ec5 100644 --- a/java-checks-test-sources/default/src/main/files/non-compiling/checks/InitializeSubclassFieldsBeforeSuperSample.java +++ b/java-checks-test-sources/default/src/main/files/non-compiling/checks/InitializeSubclassFieldsBeforeSuperSample.java @@ -173,22 +173,11 @@ abstract class Middle extends Base { Middle() { super(); - this.label = "value"; // Noncompliant + this.label = "value"; // Compliant : cannot resolve describe() implementation, conservative approach is to not flag } abstract void describe(); } - - abstract class Leaf extends Middle { - Leaf() { - super(); - } - - @Override - void describe() { - IO.println(label); - } - } } // Parameter with same name as field: bare name resolves to parameter, this.field resolves to field diff --git a/java-checks/src/main/java/org/sonar/java/checks/InitializeSubclassFieldsBeforeSuperCheck.java b/java-checks/src/main/java/org/sonar/java/checks/InitializeSubclassFieldsBeforeSuperCheck.java index fbe36f9a52..04523b9341 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/InitializeSubclassFieldsBeforeSuperCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/InitializeSubclassFieldsBeforeSuperCheck.java @@ -79,8 +79,8 @@ private static boolean isFieldUsedInMethod( ) { BlockTree methodBlock; if (method == null || (methodBlock = method.block()) == null) { - // Can't resolve body, conservatively assume field may be used. - return true; + // Can't resolve body, assume field is not used to avoid false positives + return false; } SymbolUsedVisitor symbolUsedVisitor = new SymbolUsedVisitor(symbol, childClass, visitedMethods); methodBlock.body().forEach(statement -> statement.accept(symbolUsedVisitor)); From d3295d861ed5b3619788e3fe0576b7920b3acc77 Mon Sep 17 00:00:00 2001 From: "romain.birling" Date: Tue, 24 Feb 2026 12:42:20 +0100 Subject: [PATCH 2/3] Fix autoscan tests --- .../src/test/resources/autoscan/autoscan-diff-by-rules.json | 2 +- its/autoscan/src/test/resources/autoscan/diffs/diff_S8447.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/its/autoscan/src/test/resources/autoscan/autoscan-diff-by-rules.json b/its/autoscan/src/test/resources/autoscan/autoscan-diff-by-rules.json index 13e4ae799f..1c996af0ac 100644 --- a/its/autoscan/src/test/resources/autoscan/autoscan-diff-by-rules.json +++ b/its/autoscan/src/test/resources/autoscan/autoscan-diff-by-rules.json @@ -2881,7 +2881,7 @@ }, { "ruleKey": "8447", - "hasTruePositives": true, + "hasTruePositives": false, "falseNegatives": 0, "falsePositives": 0 }, diff --git a/its/autoscan/src/test/resources/autoscan/diffs/diff_S8447.json b/its/autoscan/src/test/resources/autoscan/diffs/diff_S8447.json index 70ea2fce98..5b315c1dc0 100644 --- a/its/autoscan/src/test/resources/autoscan/diffs/diff_S8447.json +++ b/its/autoscan/src/test/resources/autoscan/diffs/diff_S8447.json @@ -1,6 +1,6 @@ { "ruleKey": "S8447", - "hasTruePositives": true, + "hasTruePositives": false, "falseNegatives": 0, "falsePositives": 0 } From 3fa225d994f9bf63f7f577b31add43bcd9c8368a Mon Sep 17 00:00:00 2001 From: "romain.birling" Date: Tue, 24 Feb 2026 13:29:08 +0100 Subject: [PATCH 3/3] Fix autoscan tests | 2 --- its/autoscan/src/test/java/org/sonar/java/it/AutoScanTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/its/autoscan/src/test/java/org/sonar/java/it/AutoScanTest.java b/its/autoscan/src/test/java/org/sonar/java/it/AutoScanTest.java index 2684fdb898..230f0cc399 100644 --- a/its/autoscan/src/test/java/org/sonar/java/it/AutoScanTest.java +++ b/its/autoscan/src/test/java/org/sonar/java/it/AutoScanTest.java @@ -199,7 +199,7 @@ public void javaCheckTestSources() throws Exception { softly.assertThat(newDiffs).containsExactlyInAnyOrderElementsOf(knownDiffs.values()); softly.assertThat(newTotal).isEqualTo(knownTotal); softly.assertThat(rulesCausingFPs).hasSize(10); - softly.assertThat(rulesNotReporting).hasSize(18); + softly.assertThat(rulesNotReporting).hasSize(19); /** * 4. Check total number of differences (FPs + FNs)