Skip to content

[java] UnusedImports: False positive if wildcard is used and only static methods #2016

@albfernandez

Description

@albfernandez

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>

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematicin:type-resolutionAffects the type resolution code

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions