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

[java] UnusedImports false positive for static import with package-private method usage #1209

Closed
eikemeier opened this Issue Jun 25, 2018 · 1 comment

Comments

Projects
None yet
3 participants
@eikemeier

eikemeier commented Jun 25, 2018

Affects PMD Version:

6.4.0

Rule:

https://pmd.github.io/pmd-6.4.0/pmd_rules_java_bestpractices.html#unusedimports

Description:

A false positive is generated for the UnusedImports rule on static import of default (package-private) access functions.

Code Sample demonstrating the issue:

src/main/java/com/github/pmd/Imports.java:

package com.github.pmd;

import static com.github.pmd.ProtectedUtils.*;
import static com.github.pmd.PublicUtils.*;

public class Imports {
    int importtest() {
        int i = 0;
        i = f1(i);
        i = g1(i);
        i = f2(i);
        i = g2(i);
        i = f3(i);
        i = g3(i);
        return i;
    }
}

On line three PMD complains …/src/main/java/com/github/pmd/Imports.java:3: Avoid unused imports such as 'com.github.pmd.ProtectedUtils', line four is fine.

src/main/java/com/github/pmd/ProtectedUtils.java:

package com.github.pmd;

final class ProtectedUtils {
    private ProtectedUtils() {
    }

    static int f1(int x) {
        return x + 1;
    }

    static int f2(int x) {
        return x + 1;
    }

    static int f3(int x) {
        return x + 1;
    }
}

src/main/java/com/github/pmd/PublicUtils.java:

package com.github.pmd;

public class PublicUtils {
    private PublicUtils() {
    }

    public static int g1(int x) {
        return x + 1;
    }

    public static int g2(int x) {
        return x + 1;
    }

    public static int g3(int x) {
        return x + 1;
    }
}

Running PMD through: Gradle

Example build.gradle:

plugins {
    id 'java-library'
    id 'pmd'
}

group 'com.github.pmd'
version '1.0'

apply plugin: 'java'
apply plugin: 'java-library'

sourceCompatibility = 1.8

repositories {
    jcenter()
}

pmd {
    consoleOutput = true
    toolVersion = '6.4.0'
    ruleSets = ['category/java/bestpractices.xml']
}
@jsotuyod

This comment has been minimized.

Member

jsotuyod commented Jul 10, 2018

Interesting... this looks like a duplicate of #925 but should already be fixed... maybe we missed a case back then.

@adangel adangel changed the title from [java] UnusedImports false positive for static import to [java] UnusedImports false positive for static import with package-private usage Oct 24, 2018

@adangel adangel self-assigned this Oct 24, 2018

@adangel adangel changed the title from [java] UnusedImports false positive for static import with package-private usage to [java] UnusedImports false positive for static import with package-private method usage Oct 24, 2018

@adangel adangel added this to the 6.9.0 milestone Oct 24, 2018

adangel added a commit to adangel/pmd that referenced this issue Oct 24, 2018

@adangel adangel added the has:pr label Oct 24, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment