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 with static imports on subclasses · Issue #3132 · pmd/pmd

[java] UnusedImports with static imports on subclasses #3132

New issue
Copy link
New issue
Copy link
Closed
#3139
Closed
[java] UnusedImports with static imports on subclasses#3132
#3139
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
6.32.0

Description

@linusjf
linusjf
opened on Feb 23, 2021
Issue body actions

Affects PMD Version:
6.31.0

Rule:

UnusedImports

Description:

I have a static import of all methods in a class but the methods referenced in the code are from its base class. The rule flags the derived class' import as unused despite the compiler recognising the import as valid.

This is unexpected.

Note:

In my opinion, this is fine since it's (strictly) true that the methods do not belong to the specified import. I interpret it as forcing the user the use the correct import. If there are methods available in the derived class and if even one is referenced, then it is used and there is no complation or rule violation for the base class' methods. It's a corner case that probably will occur only in error.
I think the rule must recognise that the parent class' methods are being referenced instead and suggest that the parent class be used instead. It's more of a case of MisusedImport rather than UnusedImport.

Code Sample demonstrating the issue:

class Hello {
    public static void sayHello() {
        //...
    }
}

class HelloMore extends Hello {
    // ...
}

import static HelloMore.*; // flagged as unused

public class Test {
    public static void main(String... args) {
        sayHello();
    }
}

Expected outcome:
No violation should be reported.

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

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

Referred from: #3130 (comment)

Activity

  1. linusjf
    added
    a:false-negativePMD doesn't flag a problematic piece of code
    a:false-positivePMD flags a piece of code that is not problematic
    on Feb 23, 2021
  2. linusjf
    changed the title [-][java] [java] False positive for rule UnusedImports[/-] [+][java] False positive for rule UnusedImports[/+] on Feb 23, 2021
  3. adangel
    removed
    a:false-negativePMD doesn't flag a problematic piece of code
    on Feb 25, 2021
  4. adangel
    changed the title [-][java] False positive for rule UnusedImports[/-] [+][java] UnusedImports with static imports on subclasses[/+] on Feb 25, 2021
  5. adangel
    added a commit that references this issue on Feb 25, 2021

    [java] UnusedImports with static imports on subclasses (fixes pmd#3132)

    This issue will close once commit 861a646 is merged into the 'main' branch.
    861a646
  6. adangel
    added this to the 6.32.0 milestone on Feb 25, 2021
  7. adangel
    closed this as completedin #3139on Feb 26, 2021
  8. adangel
    added a commit that references this issue on Feb 26, 2021

    Merge pull request #3139 from adangel:issue-3132-unused-static-import

    This issue will close once commit 93a0805 is merged into the 'main' branch.
    93a0805
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 problematic

    Type

    No type

    Projects

    No projects

      Milestone

      • 6.32.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.