GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #2016

New issue
Copy link
New issue
Copy link
Closed
#2138
Closed
[java] UnusedImports: False positive if wildcard is used and only static methods#2016
#2138
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematicin:type-resolutionAffects the type resolution code
Milestone
6.20.0

Description

@albfernandez
albfernandez
opened on Sep 14, 2019
Issue body actions

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>

Activity

  1. jsotuyod commented on Sep 19, 2019

    @jsotuyod
    jsotuyod
    on Sep 19, 2019
    Member
    More actions

    @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?

  2. albfernandez commented on Sep 19, 2019

    @albfernandez
    albfernandez
    on Sep 19, 2019 · Hidden as outdated
    Author
    show commentMore actions
  3. albfernandez commented on Sep 19, 2019

    @albfernandez
    albfernandez
    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

  4. jsotuyod
    added
    a:false-positivePMD flags a piece of code that is not problematic
    on Oct 6, 2019
  5. adangel
    added
    in:type-resolutionAffects the type resolution code
    on Nov 22, 2019
  6. adangel commented on Nov 22, 2019

    @adangel
    adangel
    on Nov 22, 2019
    Member
    More actions

    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
    
  7. adangel
    added a commit that references this issue on Nov 22, 2019

    [java] UnusedImports: False positive if wildcard is used and only sta…

    dd391ab
  8. albfernandez commented on Nov 22, 2019

    @albfernandez
    albfernandez
    on Nov 22, 2019
    ContributorAuthor
    More actions

    I've tested it and seems to work fine for me.

    Thanks

  9. adangel
    added a commit that references this issue on Nov 29, 2019

    [java] Preserve possible type of the target reference for method invo…

    This issue will close once commit a85638a is merged into the 'main' branch.
    a85638a
  10. adangel
    added this to the 6.20.0 milestone on Nov 29, 2019
  11. adangel
    added
    has:pr
    on Nov 29, 2019
  12. adangel
    closed this as completedin #2138on Nov 29, 2019
  13. jorn86 commented on Nov 29, 2021

    @jorn86
    jorn86
    on Nov 29, 2021
    More actions

    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
  14. adangel commented on Nov 29, 2021

    @adangel
    adangel
    on Nov 29, 2021
    Member
    More actions

    @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!

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-positivePMD flags a piece of code that is not problematicin:type-resolutionAffects the type resolution code

    Type

    No type

    Projects

    No projects

      Milestone

      • 6.20.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.