Skip to content

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

Closed

Description

@Chordrain

Affects PMD Version: 7.24.0

Rule: UnnecessaryImport — https://docs.pmd-code.org/latest/pmd_rules_java_codestyle.html#unnecessaryimport

Description:

When a class is referenced through an on-demand import (import p.*;), PMD reports the import as unused even though one of its members is in fact used in the file. Replacing the wildcard with the equivalent single-type import (import p.C;) makes the warning disappear, although in both cases C is resolved through that import statement.

Code Sample demonstrating the issue:

p/C.java:

package p;

public class C {
    public static final String V = "";
}

m/U.java — wildcard form, incorrectly flagged:

package m;

import p.*;

public class U {
    private void g() {
        String k = C.V;
    }
}

PMD output:

m/U.java:3:	UnnecessaryImport:	Unused import 'p.*'

Replacing the import with import p.C; removes the warning, even though the class C is referenced identically in both cases.

Expected outcome: PMD should not flag import p.*; as unnecessary when at least one type from p (here, C) is referenced in the compilation unit.

Running PMD through: CLI

Activity

  1. added
    a:false-positive
    on Oct 21, 2026
  2. Eymenonar commented on Oct 21, 2026

    @Eymenonar
    Contributor

    I can take this one. I’ll add a regression test and fix the unresolved qualified-type handling for on-demand imports.

  3. changed the title [-][java] UnnecessaryImport: false positive on on-demand (wildcard) imports whose member is actually used[/-] [+][java] UnnecessaryImport: False positive on on-demand (wildcard) imports whose member is actually used[/+] on Nov 6, 2026
  4. adangel commented on Nov 13, 2026

    @adangel
    Member

    Assuming this is a missing aux-classpath problem. Anyone tried to call PMD with the compiled project?

  5. adangel commented on Nov 13, 2026

    @adangel
    Member

    Ok, answering myself: This has been confirmed: #7217 (comment) and #7217 (comment)

  6. changed the title [-][java] UnnecessaryImport: False positive on on-demand (wildcard) imports whose member is actually used[/-] [+][java] UnnecessaryImport: False positive on on-demand (wildcard) imports whose member is actually used (incomplete aux-classpath)[/+] on Nov 13, 2026
  7. changed the title [-][java] UnnecessaryImport: False positive on on-demand (wildcard) imports whose member is actually used (incomplete aux-classpath)[/-] [+][java] UnnecessaryImport: False positive for on-demand imports with incomplete aux-classpath[/+] on Nov 13, 2026
  8. adangel commented on Nov 13, 2026

    @adangel
    Member

    Just to be clear, if all the types can be resolved, there is no problem:

    import java.util.*;
    
    public class U {
        private void g() {
            Object k = Locale.ROOT;
            int y = Calendar.YEAR;
        }
    }
  9. added this to the 7.30.0 milestone on Nov 13, 2026
  10. added a commit that references this issue on Nov 13, 2026
    e720bd7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions