Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] UnnecessaryImport false positive for on-demand imports #2655

Closed
pzygielo opened this issue Jul 19, 2020 · 3 comments · Fixed by #3241
Closed

[java] UnnecessaryImport false positive for on-demand imports #2655

pzygielo opened this issue Jul 19, 2020 · 3 comments · Fixed by #3241
Assignees
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@pzygielo
Copy link
Contributor

pzygielo commented Jul 19, 2020

Affects PMD Version:
6.25 6.34.0 (this ticket was originally for UnusedImports, replaced by UnnecessaryImport)

Rule:
UnnecessaryImports

Description:
Used IMO star-import reported as

[INFO] PMD Failure: m.U:3 Rule:UnusedImports Priority:4 Avoid unused imports such as 'p'.

Code Sample demonstrating the issue:
maven project with:

package p;

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

import p.*;

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

Expected outcome:
No violation reported, as p.C.V is resolved from p.*.

  • Does PMD report a violation, where there shouldn't be one? -> false-positive
    Yes it does.

  • Is PMD missing to report a violation, where there should be one? -> false-negative
    No.

Running PMD through:
maven

Similar open issues, but reported for JavaDoc usage of import, while I observe it with code

Similar and closed

It's possible I'm doing something wrong.

@pzygielo pzygielo added the a:bug PMD crashes or fails to analyse a file. label Jul 19, 2020
@adangel adangel added a:false-positive PMD flags a piece of code that is not problematic and removed a:bug PMD crashes or fails to analyse a file. labels Aug 23, 2020
@pzygielo
Copy link
Contributor Author

I'm afraid the new, replacement UnnecessaryImport rule reports the same FP.

@oowekyala oowekyala changed the title [java] UnusedImports false positive for on-demand imports [java] UnnecessaryImport false positive for on-demand imports Apr 24, 2021
@adangel adangel self-assigned this Apr 26, 2021
@adangel adangel added this to the 6.35.0 milestone Apr 26, 2021
@jonybuzz
Copy link

Still happening for this config:

...
    <properties>
        <java.version>11</java.version>
        <pmdVersion>6.41.0</pmdVersion>
    </properties>
    <build>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.15.0</version>
                <executions>
                    <execution>
                        <id>pmd-while-compiling</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <printFailingErrors>true</printFailingErrors>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>net.sourceforge.pmd</groupId>
                        <artifactId>pmd-core</artifactId>
                        <version>${pmdVersion}</version>
                    </dependency>
                    <dependency>
                        <groupId>net.sourceforge.pmd</groupId>
                        <artifactId>pmd-java</artifactId>
                        <version>${pmdVersion}</version>
                    </dependency>
                    <dependency>
                        <groupId>net.sourceforge.pmd</groupId>
                        <artifactId>pmd-javascript</artifactId>
                        <version>${pmdVersion}</version>
                    </dependency>
                    <dependency>
                        <groupId>net.sourceforge.pmd</groupId>
                        <artifactId>pmd-jsp</artifactId>
                        <version>${pmdVersion}</version>
                    </dependency>
                </dependencies>
            </plugin>
    </build>
...

@oowekyala
Copy link
Member

@jonybuzz Can you please provide a code example? We have a passing regression test for this issue in our sources...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants