GeezerVison is showing github.com — [java] UnusedImports: False positive if wildcard is used and only static methods · Issue #2016 · pmd/pmd
[java] UnusedImports: False positive if wildcard is used and only static methods #2016Description
Affects PMD Version:
confirmed from 6.9.0 to 6.17.0 and 6.18
Rule:
UnusedImports
Description:
False positives from UnusedImports in some specific conditions:
- Import with wildcard (example: import java.util.*)
- Using only calls to static methos who take parameters and return in java.lang (String, Object, etc)
Code Sample demonstrating the issue:
package demo;
import java.util.*;
public class Demo {
public void testFunction() {
Objects.toString(null);
}
}
Running PMD through: CLI and Ant
bash run.sh pmd -d src/ -R category/java/bestpractices.xml/UnusedImports -auxclasspath classes
sep 15, 2019 12:51:53 AM net.sourceforge.pmd.PMD processFiles
ADVERTENCIA: This analysis could be faster, please consider using Incremental Analysis: https://pmd.github.io/pmd-6.17.0/pmd_userdocs_incremental_analysis.html
src/demo/Demo.java:3: Avoid unused imports such as 'java.util'
Curiously, placing the following test code in UnusedImports.xml works fine:
<test-code>
<description>False positive in Unused imports with Wildcard</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
import java.util.*;
public class Demo {
public void testFunction() {
Objects.toString(null);
}
}
]]></code>
</test-code>
Activityjsotuyod commented on Sep 19, 2019 @albfernandez I can't reproduce this, not as a test case nor from CLI with 6.19.0-SNAPSHOT… Any further info you can provide? albfernandez commented on Sep 19, 2019 Author show commentMore actions albfernandez commented on Sep 19, 2019 ContributorAuthor More actions Hi
I've done some more researching:
The problem occurs when you set the auxclasspath. If you don't set the auxclasspath it works fine
This doesn't work (prints a violation)
bash pmd-bin-6.18.0/bin/run.sh pmd -d src/ -R category/java/bestpractices.xml/UnusedImports -auxclasspath classes
this works
bash pmd-bin-6.18.0/bin/run.sh pmd -d src/ -R category/java/bestpractices.xml/UnusedImports
The same applies to Ant.
I need to use auxclasspath for other rules to work adangel commented on Nov 22, 2019 With type resolution enabled and a correct auxclasspath, we resolve the type of the ASTName node "Object.toString(null)" as "String" - which is the result type of the method call. I think, what we need to do instead is, assign the result type of the method call not to the ASTName, but to the expression:
PrimaryExpression -> Type = java.lang.String
|- PrimaryPrefix -> Type = java.util.Objects
|- Name "Objects.toString" -> Type = java.util.Objects
|- PrimarySuffix
|- Arguments
|- ArgumentList
albfernandez commented on Nov 22, 2019 ContributorAuthor More actions I've tested it and seems to work fine for me.
Thanks added a commit that references this issue on Nov 29, 2019This issue will close once commit a85638a is merged into the 'main' branch.a85638a jorn86 commented on Nov 29, 2021 This is in the 6.20 milestone, does it mean it's fixed in that version? Because I'm using 6.26 and this issue still occurs. Reacted by Jonathan Buzzetti adangel commented on Nov 29, 2021 @jorn86 Yes, this is what it means. This issue is supposed to be fixed with 6.20.0 already.
If you still encounter this, then you might have a different case that's not covered. Please open a new issue then. Thanks!
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|