Skip to content

DO NOT MERGE SemSitter front-end: align unused imports, enum constant annotations and generic cast operands with ECJ - #6620

Draft
antonioaversa wants to merge 15 commits into
rombirli/semsitter-pvffrom
antonio/semsitter-pvf-client-fixes
Draft

antonioaversa wants to merge 15 commits into
rombirli/semsitter-pvffrom
antonio/semsitter-pvf-client-fixes

Conversation

@antonioaversa

@antonioaversa antonioaversa commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

DO NOT MERGE

Stacks on #6584 (rombirli/semsitter-pvf, which sits on top of the SemSitter front-end stack). Client-side fixes in java-frontend-semsitter for divergences between the ECJ and SemSitter front-ends found in the PVF report of #6584. No change to the analyzer server or to the bundled binaries.

Each fix is its own commit. "Before/after" is the number of differing results (ECJ vs SemSitter) on a minimal repro, measured with the same bundled server pair as #6584.

Fix Rule Before After
@SuppressWarnings("unused"/"all") on the first type suppresses unused imports S1128 4 0
no unused import is reported for a single-type import of a type the unit declares S1128 1 0
keep every owner of a statically imported name and bind a call to the owner it applies to S1128 2 0
scan the annotations of a type parameter (<@NonNull T>) S1128 1 0
a statically imported nested type is used by new Nested<>(...) S1128 1 0
read the annotations written on an enum constant (@Deprecated constant) S1874 / S5738 8 0
open type variables of a generic call with no target type (cast operand) default to Object S1905 6 0

New replay samples compare the warnings against ECJ (they fail without these commits); a unit test covers the enum constant annotation.

Not fixed here: a unit that the ECJ run cannot compile (missing test jars, preview syntax) reports no unused import with ECJ, while the SemSitter front-end resolves it. A gate on unresolved names was tried and removed: it silenced about 1350 imports ECJ does report.

PVF (same bundled server as #6584), run 47415770056 against DEV 9.1.0.60630, versus the #6584 run of 2 Dec 2026:

Rule #6584 (SemSitter only / ECJ only) This PR (SemSitter only / ECJ only)
S1128 5659 / 47 212 / 47
S1905 2125 / 368 2091 / 368
S1874 321 / 1603 310 / 1603
S5738 188 / 964 188 / 953
all rules 13580 / 6397 8050 / 6243

The S1905 and S5738 gains are small on the real projects: the jOOQ casts and the @Support({ ..., DERBY, ... }) uses of a deprecated enum constant are not covered by the repros of this PR.


Summary by Gitar

  • Deprecation handling:
    • Ignores JDK @Deprecated(since) entries newer than the configured language level
  • Type inference:
    • Keeps supertype type arguments written in class headers for calls in the class body
    • Checks wildcard bounds when a cast argument is a bounded wildcard
  • Name resolution:
    • Resolves member types inherited from compiled supertypes
  • Method metadata:
    • Marks generic methods read from compiled classes as parametrized

This will update automatically on new commits.

@antonioaversa

Copy link
Copy Markdown
Contributor Author

/pvf

@hashicorp-vault-sonar-prod

Copy link
Copy Markdown
Contributor

📊 Performance Validation complete
🆕 7918 new · 🗑️ 7593 lost issues
View dashboard

…ng on unresolved names

The unresolved-name gate silenced imports the compiler reports whenever the semantic model leaves a name unknown. Only a single-type import of a type the unit itself declares is left out.
@antonioaversa

Copy link
Copy Markdown
Contributor Author

/pvf

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

@antonioaversa ❌ Your /pvf command was not dispatched. See the failed run.

No candidate-version artifact for 41874c5; wait for Build to finish

@antonioaversa

Copy link
Copy Markdown
Contributor Author

/pvf

Comment on lines 384 to 398
@@ -378,12 +396,38 @@ private void reportUnusedImports(JavaTree.CompilationUnitTreeImpl unit) {
String name = dotted(importTree.qualifiedIdentifier());
String key = (importTree.isStatic() ? "static " : "") + name;
boolean duplicate = !seen.add(key);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Quality: PR description still claims unresolved-name units report no unused import

This commit removes the hasUnresolvedNames(unit) check from reportUnusedImports, and adds the ownType check in its place. A unit with an unresolved name now gets unused-import warnings again. The PR description table still has the row "a unit with an unresolved name reports no unused import" (S1128, 2 → 0). The row "a used single-type import of the unit's own package counts as used" is also still there, even though commit f21dae2 reverted that behaviour: isOwnPackage applies to single-type imports again. Please update both rows to say that imports of a type the unit itself declares are skipped, and give the before/after numbers measured for that case.

Was this helpful? React with 👍 / 👎

@hashicorp-vault-sonar-prod

Copy link
Copy Markdown
Contributor

📊 Performance Validation complete
🆕 8050 new · 🗑️ 6243 lost issues
View dashboard

…the calls in its body

The supertypes are now bound before the members are translated, a member type
inherited from a compiled supertype is found by name, and a supertype fact
without arguments no longer hides the ones the header spells out. An unqualified
call to a method of a generic parent then returns the substituted type instead of
the type variable.
…ss the bound holds

A Class<?> cast to Class<? super T> narrows, so the cast is not redundant.
Like the compiler, a level above 8 sees the API of that release, where a member
deprecated in a later release is not deprecated yet.
An anonymous class of an interface whose only abstract method is generic
is no longer reported as convertible to a lambda.
Comment on lines 322 to 334
/** Without any library, a method of a type nothing declares is unknown instead of named after its written owner. */
void release(int release) {
caches.release = release;
}

/** The configured language level, or 0 when the project sets none. */
int release() {
return caches.release;
}

void noLibraries(boolean noLibraries) {
caches.noLibraries = noLibraries;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Quality: JSema release(): javadoc moved off noLibraries and wrong default

The new release(int) setter went in between the existing noLibraries javadoc and noLibraries(boolean). That comment ("Without any library, a method of a type nothing declares is unknown…") now documents release(int), and noLibraries has no doc. The getter's doc is also wrong. It says the level is "0 when the project sets none", but SemSitterParser passes Integer.MAX_VALUE when javaVersion.isNotSet(). Behaviour is the same either way: neither 0 nor Integer.MAX_VALUE drops a deprecation. Still, a reader who trusts the doc will misread the release <= 8 guard in SemaAnnotations.deprecatedAfterRelease. The fix is to move the comment back onto noLibraries and describe the unset case correctly.

Move the noLibraries javadoc back and document the unset value accurately:

/** Sets the configured language level. */
void release(int release) {
  caches.release = release;
}

/** The configured language level, or Integer.MAX_VALUE when the project sets none. */
int release() {
  return caches.release;
}

/** Without any library, a method of a type nothing declares is unknown instead of named after its written owner. */
void noLibraries(boolean noLibraries) {
  caches.noLibraries = noLibraries;
}
  • Apply fix

Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎

@gitar-bot

gitar-bot Bot commented Dec 5, 2026

Copy link
Copy Markdown
Code Review 👍 Approved with suggestions 🟡 Medium risk 1 closed / 3 findings

SemSitter front-end changes to name resolution, type inference, and warning metadata could alter Java analyzer findings across analyzed projects.

Stacks client-side fixes in java-frontend-semsitter that align unused-import, deprecated enum constant, and generic cast handling with ECJ, and adds JDK deprecation and class-header type argument handling. Approved with suggestions: the PR description still lists an unresolved-name unused-import row and a reverted own-package row that no longer match the code, and the release(int) javadoc in JSema.java was moved off noLibraries and misstates the unset default. Also resolved: labeled statements no longer silence every unused-import warning.

💡 Quality: PR description still claims unresolved-name units report no unused import

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:384-398

This commit removes the hasUnresolvedNames(unit) check from reportUnusedImports, and adds the ownType check in its place. A unit with an unresolved name now gets unused-import warnings again. The PR description table still has the row "a unit with an unresolved name reports no unused import" (S1128, 2 → 0). The row "a used single-type import of the unit's own package counts as used" is also still there, even though commit f21dae2 reverted that behaviour: isOwnPackage applies to single-type imports again. Please update both rows to say that imports of a type the unit itself declares are skipped, and give the before/after numbers measured for that case.

💡 Quality: JSema release(): javadoc moved off noLibraries and wrong default

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/JSema.java:322-334

The new release(int) setter went in between the existing noLibraries javadoc and noLibraries(boolean). That comment ("Without any library, a method of a type nothing declares is unknown…") now documents release(int), and noLibraries has no doc. The getter's doc is also wrong. It says the level is "0 when the project sets none", but SemSitterParser passes Integer.MAX_VALUE when javaVersion.isNotSet(). Behaviour is the same either way: neither 0 nor Integer.MAX_VALUE drops a deprecation. Still, a reader who trusts the doc will misread the release <= 8 guard in SemaAnnotations.deprecatedAfterRelease. The fix is to move the comment back onto noLibraries and describe the unset case correctly.

Move the noLibraries javadoc back and document the unset value accurately
/** Sets the configured language level. */
void release(int release) {
  caches.release = release;
}

/** The configured language level, or Integer.MAX_VALUE when the project sets none. */
int release() {
  return caches.release;
}

/** Without any library, a method of a type nothing declares is unknown instead of named after its written owner. */
void noLibraries(boolean noLibraries) {
  caches.noLibraries = noLibraries;
}
✅ 1 closed
✅ Bug: Any labeled statement now silences every unused-import warning

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:224-228 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:383-385
reportUnusedImports now returns early when hasUnresolvedNames(unit) is true. That check treats any visited identifier whose symbol().isUnknown() as an unresolved name. The SemSitter translator leaves the declaring identifier of a labeled statement unbound: the approved Statements.expected.txt shows IDENTIFIER @35:5 id outer unknown. BaseTreeVisitor.visitLabeledStatement scans tree.label(), so CompilerWarnings.visitIdentifier adds that label to identifiers. As a result, a compilation unit that compiles cleanly and contains a label such as outer: gets no S1128 unused-import warnings, while ECJ still reports them because a label is not an error.

The existing samples miss this because every import in Statements.java is used. Before this PR the same predicate only gated MASKED_CATCH. Fix: exclude label identifiers from the unresolved check, either by skipping identifiers whose parent is a LabeledStatementTree or by binding the declaring identifier to its JLabelSymbol. Then add an unused import to a sample that contains a label.

🤖 Prompt for agents
Code Review: Stacks client-side fixes in `java-frontend-semsitter` that align unused-import, deprecated enum constant, and generic cast handling with ECJ, and adds JDK deprecation and class-header type argument handling. Approved with suggestions: the PR description still lists an unresolved-name unused-import row and a reverted own-package row that no longer match the code, and the `release(int)` javadoc in `JSema.java` was moved off `noLibraries` and misstates the unset default. Also resolved: labeled statements no longer silence every unused-import warning.

1. 💡 Quality: PR description still claims unresolved-name units report no unused import
   Files: java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/CompilerWarnings.java:384-398

   This commit removes the `hasUnresolvedNames(unit)` check from `reportUnusedImports`, and adds the `ownType` check in its place. A unit with an unresolved name now gets unused-import warnings again. The PR description table still has the row "a unit with an unresolved name reports no unused import" (S1128, 2 → 0). The row "a used single-type import of the unit's own package counts as used" is also still there, even though commit f21dae2 reverted that behaviour: `isOwnPackage` applies to single-type imports again. Please update both rows to say that imports of a type the unit itself declares are skipped, and give the before/after numbers measured for that case.

2. 💡 Quality: JSema release(): javadoc moved off noLibraries and wrong default
   Files: java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/JSema.java:322-334

   The new `release(int)` setter went in between the existing `noLibraries` javadoc and `noLibraries(boolean)`. That comment ("Without any library, a method of a type nothing declares is unknown…") now documents `release(int)`, and `noLibraries` has no doc. The getter's doc is also wrong. It says the level is "0 when the project sets none", but `SemSitterParser` passes `Integer.MAX_VALUE` when `javaVersion.isNotSet()`. Behaviour is the same either way: neither 0 nor `Integer.MAX_VALUE` drops a deprecation. Still, a reader who trusts the doc will misread the `release <= 8` guard in `SemaAnnotations.deprecatedAfterRelease`. The fix is to move the comment back onto `noLibraries` and describe the unset case correctly.

   Fix (Move the noLibraries javadoc back and document the unset value accurately):
   /** Sets the configured language level. */
   void release(int release) {
     caches.release = release;
   }
   
   /** The configured language level, or Integer.MAX_VALUE when the project sets none. */
   int release() {
     return caches.release;
   }
   
   /** Without any library, a method of a type nothing declares is unknown instead of named after its written owner. */
   void noLibraries(boolean noLibraries) {
     caches.noLibraries = noLibraries;
   }

Verification Rules 2/3 met Repos 3 inspected · PRs 3 inspected
Rules 1 failed 2 passed 3 not applicable

❌ Repository license compliance - LICENSE.txt contains SONAR Source-Available License v1.0 terms, but the central exception for sonar-java requires LGPL-3.0 in LICENSE.txt; confirm the license text (the repo remote could not be read, so identity is inferred from the PR URL).
✅ Internal artifact registries
✅ Release secrets usage

Functional validation No results

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

SonarSource/sonar-java-symbolic-execution Inspected
SonarSource/sonarlint-core Inspected
SonarSource/sonar-lits Inspected

Cross-PR 3 PRs inspected 0 open impacts

PR 6588: JAVA-7420 Bind names SemSitter leaves unresolved to statically imported fields Inspected
PR 6580: JAVA-7403 Derive unused import and no-effect assignment warnings Inspected
PR 6576: JAVA-7399 Infer invocation return types, thrown types and diamonds 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

sonarqube-next Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Quality Gate failed Quality Gate failed

Failed conditions
8 New issues
82.8% Coverage on New Code (required ≥ 90%)

See analysis details on SonarQube

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE SonarQube for IDE

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