Skip to content

JAVA-7382 Fix S1128 FPs when --release option shadows non-public members of JDK - #6558

Merged
leonardo-pilastri-sonarsource merged 5 commits into
masterfrom
lp/fix-S1128-fp-with-release-option
Nov 25, 2026
Merged

leonardo-pilastri-sonarsource merged 5 commits into
masterfrom
lp/fix-S1128-fp-with-release-option

Conversation

@leonardo-pilastri-sonarsource

@leonardo-pilastri-sonarsource leonardo-pilastri-sonarsource commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary by Gitar

  • Bug fix:
    • Avoided S1128 false positives when an import's type is still referenced by simple name under --release API shadowing
  • Unused import check:
    • Added identifier-based collector to skip reporting imports whose types are referenced in the file
    • Reports imports made redundant by member types inherited from enclosing or supertype classes
  • Tests:
    • Added HiddenMemberTypes.java test source and a test covering Java 17 and default API behavior

This will update automatically on new commits.

@hashicorp-vault-sonar-prod hashicorp-vault-sonar-prod Bot changed the title Fix S1128 FPs when --release option shadows non-public members of JDK JAVA-7382 Fix S1128 FPs when --release option shadows non-public members of JDK Nov 23, 2026
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

JAVA-7382

@datadog-sonarsource

This comment has been minimized.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Ruling Diff Summary

Detected changes in 1 rule files: 4 issues removed, 0 issues added.

S1128 (java) on guava - 4 issues removed, 0 issues added

Removed src/com/google/common/cache/LocalCache.java (line 67, column 8, end column 27)

Remove this unused import 'java.util.Map.Entry'.
        62 | import java.util.AbstractQueue;
        63 | import java.util.AbstractSet;
        64 | import java.util.Collection;
        65 | import java.util.Iterator;
        66 | import java.util.Map;
>>>     67 | import java.util.Map.Entry;
                    ^^^^^^^^^^^^^^^^^^^
        68 | import java.util.NoSuchElementException;
        69 | import java.util.Queue;
        70 | import java.util.Set;
        71 | import java.util.concurrent.Callable;
        72 | import java.util.concurrent.ConcurrentLinkedQueue;

Removed src/com/google/common/collect/ArrayTable.java (line 36, column 8, end column 27)

Remove this unused import 'java.util.Map.Entry'.
        31 | import java.lang.reflect.Array;
        32 | import java.util.Arrays;
        33 | import java.util.Collection;
        34 | import java.util.Iterator;
        35 | import java.util.Map;
>>>     36 | import java.util.Map.Entry;
                    ^^^^^^^^^^^^^^^^^^^
        37 | import java.util.Set;
        38 | 
        39 | import javax.annotation.Nullable;
        40 | 
        41 | /**

Removed src/com/google/common/collect/DenseImmutableTable.java (line 24, column 8, end column 27)

Remove this unused import 'java.util.Map.Entry'.
        19 | import com.google.common.annotations.GwtCompatible;
        20 | import com.google.common.collect.ImmutableMap.IteratorBasedImmutableMap;
        21 | import com.google.j2objc.annotations.WeakOuter;
        22 | 
        23 | import java.util.Map;
>>>     24 | import java.util.Map.Entry;
                    ^^^^^^^^^^^^^^^^^^^
        25 | 
        26 | import javax.annotation.Nullable;
        27 | import javax.annotation.concurrent.Immutable;
        28 | 
        29 | /**

Removed src/com/google/common/collect/RegularImmutableTable.java (line 20, column 8, end column 44)

Remove this unused import 'com.google.common.collect.Table.Cell'.
        15 | package com.google.common.collect;
        16 | 
        17 | import static com.google.common.base.Preconditions.checkNotNull;
        18 | 
        19 | import com.google.common.annotations.GwtCompatible;
>>>     20 | import com.google.common.collect.Table.Cell;
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        21 | import com.google.j2objc.annotations.WeakOuter;
        22 | 
        23 | import java.util.Collections;
        24 | import java.util.Comparator;
        25 | import java.util.LinkedHashSet;

PR author and reviewer: how much active time did you each spend checking the ruling changes?
Click one reaction below. Count reading the diff/report and checking source; exclude waits and fixes.

👍 ≤5 min · 😄 >5–15 min · 🎉 >15–30 min · 😕 Didn’t check · 👀 >30 min

This survey is for validation purposes. It will be removed by the end of December 2026.

@david-schroeter david-schroeter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR looks good to me!

Just one very small nit.

Also the rulling are failing after the last modif, other than that it's ready to be merged.

@leonardo-pilastri-sonarsource
leonardo-pilastri-sonarsource force-pushed the lp/fix-S1128-fp-with-release-option branch from 52eab90 to c74b54f Compare November 25, 2026 11:08
@gitar-bot

gitar-bot Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Code Review ✅ Approved 🟡 Medium risk 1 closed / 1 findings

The S1128 checker filters unused-import warnings through identifier and member-scope resolution, which could suppress valid findings or flag imports incorrectly.

Fixes S1128 false positives when the --release option shadows non-public JDK members by adjusting how unused-import checks resolve those members. Resolved an issue where the suppression also hid imports that are truly redundant through member scope, so those are reported again. No open issues found.

✅ 1 closed
✅ Edge Case: Suppression also hides imports that are really redundant via member scope

📄 java-checks/src/main/java/org/sonar/java/checks/UselessImportCheck.java:138-140 📄 java-checks/src/main/java/org/sonar/java/checks/UselessImportCheck.java:150-160 📄 java-checks/src/test/java/org/sonar/java/checks/UselessImportCheckTest.java:97-109
The new guard at line 138 drops an UNUSED_IMPORT warning whenever any simple-name identifier in the file resolves to the imported type. It never checks whether that identifier was actually resolved through the import. In Java, member types (declared or inherited) shadow single-type imports. So a correct compiler warning is now hidden in cases like these:

  • import java.util.Map.Entry; class M<K,V> implements Map<K,V> { Entry<K,V> e; ... }: Entry resolves to the inherited Map.Entry.
  • import p.Outer.Inner; inside p/Outer.java, where Inner is referenced in Outer: Inner resolves through the enclosing class's member scope.

In both cases the compiler correctly reports the import as unused, and the symbol is the same java.util.Map.Entry / p.Outer.Inner. The collector adds it, and the issue is suppressed. This introduces false negatives that S1128 did not have before, and no test covers it. A narrower fix would only skip the warning when the type is not already reachable as a member type of an enclosing or super type at the use site. Another option is to limit the suppression to the known case where the release API hides a non-public inherited member type, e.g. by checking that the enclosing class's supertypes do not declare an accessible member type with that simple name.

Verification Rules 3/3 met Repos 1 inspected

Functional validation No results

Rules 3 passed 2 not applicable

✅ Internal artifact registries
✅ Release secrets usage
✅ Repository license compliance

Broader impact Repos 1 inspected
Cross-repo 1 repository inspected 0 open impacts

SonarSource/sonar-java-symbolic-execution Inspected

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

Copy link
Copy Markdown
Contributor

@leonardo-pilastri-sonarsource
leonardo-pilastri-sonarsource merged commit 06980a8 into master Nov 25, 2026
31 of 32 checks passed
@leonardo-pilastri-sonarsource
leonardo-pilastri-sonarsource deleted the lp/fix-S1128-fp-with-release-option branch November 25, 2026 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants