Skip to content

[java] FP in UnusedImports for ambiguous static on-demand imports #2277

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

Closed
kris-scheibe opened this issue Feb 10, 2020 · 0 comments · Fixed by #2278
Closed

[java] FP in UnusedImports for ambiguous static on-demand imports #2277

kris-scheibe opened this issue Feb 10, 2020 · 0 comments · Fixed by #2278
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@kris-scheibe
Copy link
Contributor

kris-scheibe commented Feb 10, 2020

Affects PMD Version: 6.21

Rule: UnusedImport

Description:
When multiple static on-demand imports match the same method in the code, a regular static import must be used to specify the actual method to use.
The rule may flag the static import as unused, even though it is required.

Also, the rule currently behaves differently, depending on the order of the statements.
In the example below, the "valueOf" method is matched to the "Integer.*" import, which leaves the the "Long.valueOf" import unmatched.
If the "valueOf" method is moved to the bottom of the method, all imports are matched and nothing is flagged.

Code Sample demonstrating the issue:

import static java.lang.Integer.*;
import static java.lang.Long.valueOf;
import static java.lang.Long.*;

public class Foo {
    public void foo() {
        // the "valueOf" method is both in Integer.* and Long.*
        // we need an explicit static import to specify one of them, e.g. Long.valueOf
        valueOf("123", 10);

        // covered by Integer.*
        int i = parseInt("123");

        // covered by Long.*
        long l = parseLong("123");
    }
}

Running PMD through: Other

@adangel adangel added the a:false-positive PMD flags a piece of code that is not problematic label Feb 13, 2020
@adangel adangel added this to the 6.22.0 milestone Feb 13, 2020
@adangel adangel changed the title [java] FP in UnusedImports for ambiguous on-demand imports [java] FP in UnusedImports for ambiguous static on-demand imports Feb 13, 2020
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.

2 participants