From 63fbfbd8c7f38b0f9cae20a02fb0a7041f11d221 Mon Sep 17 00:00:00 2001 From: kris-scheibe Date: Mon, 10 Feb 2020 23:20:08 +0100 Subject: [PATCH 1/3] UnusedImports rule now matches regular static imports first and on-demand static imports after that --- .../rule/bestpractices/UnusedImportsRule.java | 15 ++++++++++- .../rule/bestpractices/xml/UnusedImports.xml | 26 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedImportsRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedImportsRule.java index da54a66ce0..ce0966823e 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedImportsRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedImportsRule.java @@ -152,14 +152,27 @@ public class UnusedImportsRule extends AbstractJavaRule { return; } ImportWrapper candidate = getImportWrapper(node); + + // check exact imports Iterator it = imports.iterator(); while (it.hasNext()) { ImportWrapper i = it.next(); - if (i.matches(candidate)) { + if (!i.isStaticOnDemand() && i.matches(candidate)) { it.remove(); return; } } + + // check static on-demand imports + it = imports.iterator(); + while (it.hasNext()) { + ImportWrapper i = it.next(); + if (i.isStaticOnDemand() && i.matches(candidate)) { + it.remove(); + return; + } + } + if (TypeNode.class.isAssignableFrom(node.getClass()) && ((TypeNode) node).getType() != null) { Class c = ((TypeNode) node).getType(); if (c.getPackage() != null) { diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnusedImports.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnusedImports.xml index 13438e73cd..5fe5e64653 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnusedImports.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnusedImports.xml @@ -552,4 +552,30 @@ public class Issue2016 { } ]]> + + + 0 + + java 1.5 + From 1bba78cc6578424e455e3f846837f6fbd8ee7f94 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 13 Feb 2020 11:12:44 +0100 Subject: [PATCH 2/3] [java] Remove unnecessary CDATA for UnusedImports tests --- .../rule/bestpractices/xml/UnusedImports.xml | 116 ++++++++---------- 1 file changed, 50 insertions(+), 66 deletions(-) diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnusedImports.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnusedImports.xml index 5fe5e64653..321098f929 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnusedImports.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnusedImports.xml @@ -3,20 +3,18 @@ xmlns="http://pmd.sourceforge.net/rule-tests" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd"> + - + simple unused single type import 1 + - + one used single type import 0 + - + 2 unused single-type imports 2 + - + 1 used single type import 0 + - + 1 import stmt, used only in throws clause 0 + - + for loop 0 java 1.5 + - + Generics 0 java 1.5 + - + Generics 2 0 java 1.5 + - + Annotations 0 java 1.5 + - + Annotations 2 0 java 1.5 + - + import from default package 0 + - + import from default package 1 + - + Used static import 0 java 1.5 + - + Unused static import 1 java 1.5 + - + On demand import 0 + - + imports used in javadoc comment, see also bug #254 0 + #1280 False Positive in UnusedImports when import used in javadoc 0 @@ -258,9 +243,7 @@ public class Foo { - + Bug 2606609 : False "UnusedImports" positive in package-info.java 0 + #1181 unused import false positive if used as parameter in javadoc only. 0 @@ -308,6 +292,7 @@ public class Foo { } ]]> + #1280 False Positive in UnusedImports when import used in javadoc 0 @@ -323,6 +308,7 @@ public class Foo { } ]]> + #914 False +ve from UnusedImports with wildcard static imports 0 @@ -386,10 +372,10 @@ import foo.bar.Fixed_Values; */ public interface Interface { - /** - * @throws Under_Score_Exception - */ - void doSomething(); + /** + * @throws Under_Score_Exception + */ + void doSomething(); } ]]> @@ -405,7 +391,7 @@ public class Foo { /** * {@link Bar#doSomething(GroupLayout.Group)} - */ + */ void doSomething(); } ]]> @@ -517,7 +503,7 @@ public class VendingV2PaymentRequest { } ]]> - + #2025 False Positive in UnusedImports for params when using @link with FQCN 0 @@ -528,7 +514,7 @@ import spark.Request; // flaged, should not * {@link foo.bar.MyController#startTransaction(Request)} */ public class VendingV2PaymentRequest { -} +} ]]> @@ -552,10 +538,9 @@ public class Issue2016 { } ]]> + - + resolve ambiguous static on-demand imports (#2277) 0 - java 1.5 From 1f75eb551fafdd24cc9163fbd2731819ee5b0099 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 13 Feb 2020 11:15:26 +0100 Subject: [PATCH 3/3] [doc] Update release notes, fixes #2277, refs #2278 --- docs/pages/release_notes.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 4ea9b3b8b4..34a8b6ff80 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -23,6 +23,8 @@ For the changes, see [PMD Designer Changelog](https://github.com/pmd/pmd-designe * java * [#2268](https://github.com/pmd/pmd/issues/2268): \[java] Improve TypeHelper resilience +* java-bestpractices + * [#2277](https://github.com/pmd/pmd/issues/2277): \[java] FP in UnusedImports for ambiguous static on-demand imports * java-errorprone * [#2250](https://github.com/pmd/pmd/issues/2250): \[java] InvalidLogMessageFormat flags logging calls using a slf4j-Marker * java-performance @@ -36,6 +38,7 @@ For the changes, see [PMD Designer Changelog](https://github.com/pmd/pmd-designe * [#2253](https://github.com/pmd/pmd/pull/2253): \[modelica] Remove duplicated dependencies - [Piotrek Żygieło](https://github.com/pzygielo) * [#2256](https://github.com/pmd/pmd/pull/2256): \[doc] Corrected XML attributes in release notes - [Maikel Steneker](https://github.com/maikelsteneker) * [#2276](https://github.com/pmd/pmd/pull/2276): \[java] AppendCharacterWithCharRule ignore literals in expressions - [Kris Scheibe](https://github.com/kris-scheibe) +* [#2278](https://github.com/pmd/pmd/pull/2278): \[java] fix UnusedImports rule for ambiguous static on-demand imports - [Kris Scheibe](https://github.com/kris-scheibe) {% endtocmaker %}