Skip to content

JAVA-7403 Derive unused import and no-effect assignment warnings - #6580

Open
rombirli wants to merge 2 commits into
rombirli/semsitter-annotation-metadatafrom
rombirli/semsitter-unused-import-warnings
Open

rombirli wants to merge 2 commits into
rombirli/semsitter-annotation-metadatafrom
rombirli/semsitter-unused-import-warnings

Conversation

@rombirli

@rombirli rombirli commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Part 13 of 17 of JAVA-7392. Ticket: JAVA-7403.

Intent

Checks such as S1128 read the compiler warnings of the compilation unit. ECJ computes them, SemSitter does not, so CompilerWarnings derives them from the translated tree: unused imports and assignments without effect.

flowchart LR
  U[CompilationUnitTree] --> W[CompilerWarnings]
  W --> I[UNUSED_IMPORT: no simple name or javadoc reference uses it]
  W --> A[ASSIGNMENT_HAS_NO_EFFECT: x = x]
  W --> U2[unit.addWarnings]
Loading

From Warnings.expected.txt, identical to what ECJ reports:

+WARNING UNUSED_IMPORT IMPORT @8:1 The import java.util.Set is never used
+WARNING ASSIGNMENT_HAS_NO_EFFECT ASSIGNMENT @37:5 The assignment to variable local has no effect

ReplayTest.same_warnings_as_ecj compares the derived warnings with ECJ on every compiling sample. Cast and catch warnings come in the next PR.

Test plan

  • same_warnings_as_ecj on the replay samples, JWarningTest, SemSitterParityTest

@hashicorp-vault-sonar-prod

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

Copy link
Copy Markdown
Contributor

JAVA-7403

@rombirli
rombirli added this pull request to stack #6570 November 25, 2026 11:48
@rombirli
rombirli force-pushed the rombirli/semsitter-unused-import-warnings branch from 63f3fd4 to 6522b29 Compare November 25, 2026 12:00
@rombirli
rombirli force-pushed the rombirli/semsitter-unused-import-warnings branch from 6522b29 to 678b2ec Compare November 25, 2026 12:03
@rombirli
rombirli force-pushed the rombirli/semsitter-unused-import-warnings branch from 678b2ec to 8da4fa9 Compare November 25, 2026 13:08
@rombirli
rombirli force-pushed the rombirli/semsitter-unused-import-warnings branch from 8da4fa9 to 8298f68 Compare November 25, 2026 13:41
@rombirli
rombirli force-pushed the rombirli/semsitter-unused-import-warnings branch from 8298f68 to 1f1295b Compare November 25, 2026 14:14
@rombirli
rombirli force-pushed the rombirli/semsitter-unused-import-warnings branch from 1f1295b to 06b06d8 Compare November 25, 2026 14:55
@rombirli
rombirli force-pushed the rombirli/semsitter-unused-import-warnings branch from 06b06d8 to a16cbb0 Compare November 25, 2026 15:32
@rombirli
rombirli force-pushed the rombirli/semsitter-unused-import-warnings branch 2 times, most recently from 8a0fee0 to fe690b4 Compare November 25, 2026 17:16
@rombirli
rombirli force-pushed the rombirli/semsitter-unused-import-warnings branch from fe690b4 to c4192fa Compare November 25, 2026 18:03
@rombirli
rombirli force-pushed the rombirli/semsitter-unused-import-warnings branch from c4192fa to be84f82 Compare November 25, 2026 18:39
@rombirli
rombirli force-pushed the rombirli/semsitter-unused-import-warnings branch from be84f82 to 25d3060 Compare November 25, 2026 20:45
@rombirli
rombirli removed this pull request from stack #6570 November 25, 2026 20:47
@rombirli
rombirli added this pull request to stack #6589 November 25, 2026 20:48
@rombirli
rombirli force-pushed the rombirli/semsitter-unused-import-warnings branch from 25d3060 to a28a9c7 Compare November 25, 2026 20:57
@rombirli
rombirli force-pushed the rombirli/semsitter-unused-import-warnings branch 2 times, most recently from 83c98cb to c0a380b Compare November 25, 2026 21:28
@rombirli
rombirli force-pushed the rombirli/semsitter-unused-import-warnings branch from c0a380b to 788213a Compare November 27, 2026 07:55
@rombirli
rombirli force-pushed the rombirli/semsitter-unused-import-warnings branch from 788213a to cb0bfe7 Compare November 27, 2026 08:29
@datadog-sonarsource

This comment has been minimized.

@rombirli
rombirli force-pushed the rombirli/semsitter-unused-import-warnings branch from cb0bfe7 to 261bd6b Compare November 27, 2026 09:24
@gitar-bot

gitar-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown
CI failed: The Ruling Update and Notify job failed because ruling QA was cancelled upstream, so no actual_* artifacts were downloaded. The log does not explain the cancellation, and the finding is not attributed to this PR (related_to_pr=unclear).

Overview

The Ruling Update and Notify job failed because ruling QA was cancelled upstream, so no actual_* artifacts were downloaded. The log does not explain the cancellation, and the finding is not attributed to this PR (related_to_pr=unclear).

Failures

Ruling Update and Notify failed: no actual_* artifacts downloaded after upstream ruling QA was cancelled (cause unclear, not attributed to PR)

  • Type: infrastructure
  • Affected jobs: 140420199677
  • Related to change: unclear
  • Confidence: high
  • Root cause: This is a cascading workflow failure: the log marks ruling QA as cancelled, then the notification action fails during artifact sync with “No artifacts were downloaded” and exits with code 1. The log does not explain why ruling QA was cancelled. The PR adds derived compiler-warning behavior and replay expectations, but the supplied evidence does not show that any specific change caused the cancellation.
  • Suggested fix: Investigate why the ruling QA job was cancelled and rerun it so it can upload the expected actual_* artifacts. Consider making the notification workflow handle missing artifacts gracefully when the upstream job is cancelled.

Summary

  • Change-related failures: 0
  • Infrastructure/flaky failures: 1
  • Recommended action: Investigate why the ruling QA job was cancelled and rerun it so it can upload the expected actual_* artifacts. Consider making the notification workflow handle missing artifacts gracefully when the upstream job is cancelled.
Code Review ✅ Approved 🟡 Medium risk 13 closed / 13 findings

Deriving unused-import and self-assignment warnings changes SemSitter analysis diagnostics, which could alter rule findings for Java files.

Derives UNUSED_IMPORT and ASSIGNMENT_HAS_NO_EFFECT compiler warnings from the translated tree so checks like S1128 see the same warnings ECJ reports. Seven issues were resolved this session, including static on-demand imports now accepting inherited members, which matches ECJ on the InheritedStaticImports sample.

  • Javadoc references, fully qualified Javadoc parameter types, and annotations on qualified types are now handled so imports are no longer wrongly marked unused
  • On-demand imports are no longer counted as used by types from subpackages
  • The import scope check no longer recomputes the parent walk for every import and identifier pair
  • Supported warning kinds are reported even when no warning was found
✅ 13 closed
✅ Bug: Supported warning kinds only show up when a warning was found

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:62 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:64 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:72-74 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:83 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:86-88
add only creates a map entry through computeIfAbsent when a warning is actually reported. addTo then hands that map to unit.addWarnings, and CompilationUnitTreeImpl.supportedWarningKinds() returns warnings.keySet().

So for a file with no unused import and no self-assignment, supportedWarningKinds() returns an empty set. The CompilationUnitTree contract says that means "no compiler warnings are available", not "supported, none found". A file with only an unused import also reports ASSIGNMENT_HAS_NO_EFFECT as unsupported.

The JDT path (JDTWarning.Mapper) pre-seeds every kind, and the constant SUPPORTED_KINDS declared here is never used. That suggests the seeding was intended and left out. Checks relying on the documented API to tell "unsupported" from "clean" will treat SemSitter files as having no warnings data.

✅ Performance: isInScope parent walk is recomputed for every import × identifier

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:200-210 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:250-264
reportUnusedImports calls isUsed once per import. isUsed loops over every identifier in the file and calls ImportScopes.isInScope(symbol, identifier) on each one. That call walks the identifier's whole parent chain and runs isSubtypeOf on each enclosing class.

The result depends only on the identifier, not on the import. So the cost is O(imports × identifiers × depth) for every analyzed file, and large files with many imports pay it in full.

Fix: compute the out-of-scope identifiers once, before the import loop (for example in a list or a map from simple name to identifiers), and reuse it for every import.

✅ Bug: On-demand import is counted as used by types from its subpackages

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:319-323
uses handles an on-demand import by calling declaredIn(symbol, qualifier, false). For non-static imports that helper only checks dottedType(type).startsWith(qualifier + "."), so any type in a subpackage also matches. Example: import java.util.*; import java.util.concurrent.*; where the file only uses ConcurrentHashMap. java.util.concurrent.ConcurrentHashMap starts with java.util., so java.util.* is counted as used and no UNUSED_IMPORT is reported, but ECJ reports The import java.util is never used, because an on-demand import does not bring in subpackage types. That gives S1128 a false negative and breaks the parity with ECJ this PR claims. The fix is to require that the type's package (or its directly enclosing type, for an on-demand import of a type's members) equals the qualifier, not just shares the prefix.

✅ Edge Case: Javadoc reference wrapped onto the next line is read as *

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:63 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:185-188 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:206-216
Both alternatives of JAVADOC_REFERENCE start with \s++, which also consumes a line break. In `{@link

  • Foo}or@see
  • Foo, the captured text is therefore * Foo. referenceEndstops at the space after, so the reference is just andJAVA_NAMEfinds no name.Foois never added tojavadocNames, and an import used only in that Javadoc is reported as unused, while ECJ strips the leading asterisks and counts it as used. The fix is to strip the leading *` (and whitespace) from continuation lines before matching, or to allow only horizontal whitespace after the tag.
✅ Edge Case: Fully qualified parameter type in a Javadoc link marks import used

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:206-216 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:311
referenceNames adds every Java name after the ( of a method reference. For {@link #m(java.util.List)} it adds java, util and List. A single-type import java.util.List; that is otherwise unused is then counted as used through javadocNames, so no UNUSED_IMPORT is reported, even though the qualified parameter type does not go through the import and ECJ does report it. The fix is to keep only the first name of each comma-separated parameter, the same way the head of the reference is handled.

...and 8 more closed from earlier reviews

Verification Rules 3/3 met Repos 3 inspected · PRs 1 inspected

Functional validation No results

Rules 3 passed 2 not applicable

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

Broader impact Repos 3 inspected PRs 1 inspected
Cross-repo 3 repositories inspected 0 open impacts

SonarSource/sonar-iac Inspected
SonarSource/sonarlint-intellij Inspected
SonarSource/sonarlint-eclipse Inspected

Cross-PR 1 PR inspected 0 open impacts

PR 6553: SONARJAVA-7339 Parse with the SemSitter translator behind the feature flag Inspected

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Tip

Comment Gitar fix CI or enable auto-apply: gitar auto-apply:on

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

This branch has not been deployed

No deployments
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.

1 participant