Skip to content

[java] Fix #7215: Handle qualified types from unresolved on-demand imports - #7217

Merged
adangel merged 4 commits into
pmd:mainfrom
Eymenonar:issue-7215-unnecessary-import
Nov 13, 2026
Merged

adangel merged 4 commits into
pmd:mainfrom
Eymenonar:issue-7215-unnecessary-import

Conversation

@Eymenonar

Copy link
Copy Markdown
Contributor

Fixes #7215

Problem

UnnecessaryImport reports an on-demand import as unused when an unresolved qualified field access may refer to a type supplied by that import.

Changes

  • Treat ambiguous field-access qualifiers as possible types from on-demand imports.
  • Add a regression test for a qualified member access through a wildcard import.

Tests

  • ./mvnw -pl pmd-java -Dtest=UnnecessaryImportTest -Dsurefire.failIfNoSpecifiedTests=false test
  • ./mvnw -q -pl pmd-java -am test
  • ./mvnw -pl pmd-java -am -DskipTests verify

Treat an unresolved field-access qualifier as a possible type from an on-demand import. Add a regression test for a wildcard import used through a qualified member access.

Signed-off-by: Eymen <[email protected]>
@pmd-actions-helper

pmd-actions-helper Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 16 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-11-13 08:41:06+00:00 for 371119e)

@UncleOwen

UncleOwen commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

I'm not sure the tests are testing what they are supposed to test. There is no C anywhere. I think this is testing the incomplete classpath case.

Could you please add a C.java to a new subdirectory of pmd-java/src/test/java-resources/net/sourceforge/pmd/lang/java/rule/codestyle/unnecessaryimport and adjust your imports?

@Eymenonar

Copy link
Copy Markdown
Contributor Author

You’re right, these tests exercise incomplete classpath recovery. I tried adding C.java under java-resources and updating both imports; both tests then pass with the production change reverted (UnnecessaryImportTest: 95/95). So the patch only changes the unresolved-type path. Should UnnecessaryImport preserve the on-demand import when C cannot be resolved? If so, I’ll make that scope explicit in the test descriptions; otherwise the code change isn’t needed.

@UncleOwen

Copy link
Copy Markdown
Member

Maybe I've misunderstood the issue? I thought this was also happens when the classpath is set up correctly?

@UncleOwen

Copy link
Copy Markdown
Member

Yes, I was wrong. The issue disappears when I set up a maven project.

@adangel adangel changed the title [java] Fix #7215: Handle qualified types from on-demand imports [java] Fix #7215: Handle qualified types from unresolved on-demand imports Nov 13, 2026

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for working on this! I reviewed the change and found a few issues:

1. Regression: unused-import check now wrongly flags static on-demand imports

recordFailedTypeResWithName(..., onlyStatics=false) clears only typeImportsOnDemand. Before this PR, an unresolved qualifier in an expression (e.g. X in X.foo()) cleared staticImportsOnDemand. Now it clears typeImportsOnDemand instead, which trades one false positive for another:

import static something.Foo.*;   // now reported as unused (wasn't before)
public class Example {
    void m() { CONST.length(); }   // CONST may be a static field from Foo.*
}

An unresolved name in this position may be a type from a type-import-on-demand, or a static field / static member type from a static-import-on-demand. So both sets need to be cleared. No existing test covers this, which is why CI and the regression tester didn't catch it.

Comment: Well, the regression tester didn't catch it, because there doesn't seem to be an example of this case in any project we analyze.

2. The new condition is always false

As @oowekyala already noted: AstDisambiguationPass only allows ASTClassType or ASTExpression as the parent of an ASTAmbiguousName (anything else throws an AssertionError). So onlyStatic is now always false, and both the condition and the ASTExpression import can be removed.

3. Tests only cover the incomplete auxclasspath case

As discussed above, the issue only occurs when C can't be resolved, so the two new tests only exercise the incomplete auxclasspath case. Their descriptions should say so. There should also be tests for:

  • the static on-demand case from point 1,
  • resolved types through a type-import-on-demand (e.g. import java.util.*; with Locale.ROOT / Calendar.YEAR),
  • resolved members through a static-import-on-demand (e.g. import static java.util.Collections.*; / import static java.util.Map.*; with EMPTY_LIST.size() / Entry.comparingByKey()).

I'll take care of these points myself and push the changes to this PR, so no further changes are needed from your side. Thanks again for the contribution!


🤖 Review assisted by Claude Code

…d qualifiers

An unresolved ambiguous name may be a type from a type-import-on-demand
or a static field / static member type from a static-import-on-demand,
so both kinds of on-demand imports must be considered used. Previously
only the type imports on demand were cleared, which introduced a false
positive for static imports on demand.

- Simplify visit(ASTAmbiguousName): the parent is always ASTClassType or
  ASTExpression, so onlyStatic was always false
- Prefix comments about incomplete auxclasspath workarounds with
  "Incomplete auxclasspath: "
- Add tests for static on-demand imports and for resolved types
- Add release notes entry for pmd#7215

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@adangel adangel added this to the 7.30.0 milestone Nov 13, 2026
@adangel
adangel merged commit e720bd7 into pmd:main Nov 13, 2026
13 checks passed
@adangel adangel self-assigned this Nov 13, 2026
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.

[java] UnnecessaryImport: False positive for on-demand imports with incomplete aux-classpath

4 participants