Skip to content
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

UnusedImports does not report unused static imports when the method is used as a method reference #12923

Closed
ksobolew opened this issue Mar 28, 2023 · 4 comments

Comments

@ksobolew
Copy link

ksobolew commented Mar 28, 2023

I have read check documentation: https://checkstyle.sourceforge.io/config_imports.html#UnusedImports
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

kudivuhadi@MBP-Krzysztof scratches %  javac scratch.java 
kudivuhadi@MBP-Krzysztof scratches %  ls -l
total 24
-rw-r--r--  1 kudivuhadi  staff  1165 28 mar 12:00 Dummy.class
-rw-r--r--@ 1 kudivuhadi  staff   223 28 mar 12:00 scratch.java

kudivuhadi@MBP-Krzysztof scratches %  cat config.xml 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "http://checkstyle.sourceforge.net/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="TreeWalker">
        <module name="UnusedImports" />
    </module>
</module>

kudivuhadi@MBP-Krzysztof scratches %  cat scratch.java
import java.util.Optional;
import java.util.Set;

import static java.lang.String.format;
import static java.lang.String.join;

class Dummy
{
    public static void main(String[] args)
    {
        Optional<String> test = Optional.empty();
        test.map(String::format);
    }
}

kudivuhadi@MBP-Krzysztof scratches %  RUN_LOCALE="-Duser.language=en -Duser.country=US"
kudivuhadi@MBP-Krzysztof scratches %  java $RUN_LOCALE -jar checkstyle-10.9.3-all.jar \
   -c config.xml scratch.java
Starting audit...
[ERROR] scratch.java:2:8: Unused import - java.util.Set. [UnusedImports]
[ERROR] scratch.java:5:15: Unused import - java.lang.String.join. [UnusedImports]
Audit done.
Checkstyle ends with 2 errors.

The Java file contains three unused imports: the import of java.util.Set and static imports of java.lang.String#format and java.lang.String#join. Only two of them are reported, while the static import of java.lang.String#format is a false negative. From what it looks like, it's not reported because it's "used" as a method reference, as in test.map(String::format);, which is wrong, because with a method reference only the type needs to be imported and that's enough to resolve the method itself.

@nrmancuso
Copy link
Member

@ksobolew interesting nuance, thanks for raising this issue!

renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Mar 29, 2023
…mports when the method is used as a method reference

Added an additional check to not include method ref into referenced types.
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Mar 29, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Mar 30, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Mar 30, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Mar 31, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Mar 31, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Apr 1, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Apr 1, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Apr 1, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Apr 1, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Apr 2, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Apr 5, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Apr 13, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Apr 14, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Apr 17, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Apr 18, 2023
nrmancuso pushed a commit to renjith7189/checkstyle that referenced this issue Apr 19, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue Apr 19, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue May 1, 2023
renjith7189 added a commit to renjith7189/checkstyle that referenced this issue May 2, 2023
@checkstyle checkstyle deleted a comment from 0xbakry Feb 23, 2024
@checkstyle checkstyle deleted a comment from nrmancuso Feb 23, 2024
@romani
Copy link
Member

romani commented Feb 23, 2024

@mahfouz72 , can you help us to finish this issue ? fix is almost done at #12933, only small step is required. please create new PR and resuse code

@mahfouz72
Copy link
Member

@romani ok will take it.will send a new PR asap

mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Feb 25, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Feb 25, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Feb 25, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Feb 25, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Feb 25, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Feb 27, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Feb 28, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Feb 28, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Feb 28, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Feb 28, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Feb 28, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Feb 28, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Feb 28, 2024
rnveach pushed a commit to mahfouz72/checkstyle that referenced this issue Mar 18, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Mar 19, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Mar 19, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Mar 27, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Apr 1, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Apr 1, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Apr 1, 2024
@romani romani added the bug label Apr 4, 2024
@github-actions github-actions bot added this to the 10.15.1 milestone Apr 4, 2024
@mahfouz72
Copy link
Member

@romani I think this should be closed now

@romani romani closed this as completed Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants