From ef18609309629e34ecc42f8eb3698d4a6b285f57 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 29 Feb 2024 13:33:04 +0100 Subject: [PATCH] [java] UnnecessaryImport - keep analyzing with failed overload selection Fixes #4816 --- docs/pages/release_notes.md | 2 ++ .../rule/codestyle/UnnecessaryImportRule.java | 3 ++- .../unnecessaryimport/item/Item.java | 11 ++++++++ .../unnecessaryimport/item/ItemProducer.java | 13 +++++++++ .../rule/codestyle/xml/UnnecessaryImport.xml | 27 +++++++++++++++++++ 5 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/unnecessaryimport/item/Item.java create mode 100644 pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/unnecessaryimport/item/ItemProducer.java diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 5848d4a8a33..c3f2b107790 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -284,6 +284,7 @@ The rules have been moved into categories with PMD 6. * [#4631](https://github.com/pmd/pmd/issues/4631): \[java] UnnecessaryFullyQualifiedName fails to recognize illegal self reference in enums * [#4645](https://github.com/pmd/pmd/issues/4645): \[java] CommentDefaultAccessModifier - False Positive with JUnit5's ParameterizedTest * [#4754](https://github.com/pmd/pmd/pull/4754): \[java] EmptyControlStatementRule: Add allowCommentedBlocks property + * [#4816](https://github.com/pmd/pmd/issues/4816): \[java] UnnecessaryImport false-positive on generic method call with on lambda * java-design * [#174](https://github.com/pmd/pmd/issues/174): \[java] SingularField false positive with switch in method that both assigns and reads field * java-errorprone @@ -1442,6 +1443,7 @@ Language specific fixes: * [#4631](https://github.com/pmd/pmd/issues/4631): \[java] UnnecessaryFullyQualifiedName fails to recognize illegal self reference in enums * [#4645](https://github.com/pmd/pmd/issues/4645): \[java] CommentDefaultAccessModifier - False Positive with JUnit5's ParameterizedTest * [#4754](https://github.com/pmd/pmd/pull/4754): \[java] EmptyControlStatementRule: Add allowCommentedBlocks property + * [#4816](https://github.com/pmd/pmd/issues/4816): \[java] UnnecessaryImport false-positive on generic method call with on lambda * java-design * [#174](https://github.com/pmd/pmd/issues/174): \[java] SingularField false positive with switch in method that both assigns and reads field * [#1014](https://github.com/pmd/pmd/issues/1014): \[java] LawOfDemeter: False positive with lambda expression diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryImportRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryImportRule.java index cc04576bc5d..e20a38c825d 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryImportRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryImportRule.java @@ -235,7 +235,8 @@ public Object visit(ASTMethodCall node, Object data) { if (node.getQualifier() == null) { OverloadSelectionResult overload = node.getOverloadSelectionInfo(); if (overload.isFailed()) { - return null; // todo we're erring towards FPs + // don't try further, but still visit all ASTClassType nodes in the AST. + return super.visit(node, data); // todo we're erring towards FPs } ShadowChainIterator scopeIter = diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/unnecessaryimport/item/Item.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/unnecessaryimport/item/Item.java new file mode 100644 index 00000000000..a01d5065db3 --- /dev/null +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/unnecessaryimport/item/Item.java @@ -0,0 +1,11 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.item; + +public class Item { + public String getValue() { + return ""; + } +} diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/unnecessaryimport/item/ItemProducer.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/unnecessaryimport/item/ItemProducer.java new file mode 100644 index 00000000000..51a3a7ec8c1 --- /dev/null +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/codestyle/unnecessaryimport/item/ItemProducer.java @@ -0,0 +1,13 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.item; + +import java.util.stream.Stream; + +public class ItemProducer { + public Stream stream() { + return null; + } +} diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryImport.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryImport.xml index e22a37a6b3b..42000d89e85 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryImport.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryImport.xml @@ -1168,4 +1168,31 @@ public class Sample { } ]]> + + + [java] UnnecessaryImport false-positive on generic method call with on lambda #4816 + 0 + X run(Function f) { return null; } + + public Set sample() { + return run((producer) -> producer + .stream() + .map(Item::getValue) + .collect(Collectors.toCollection(TreeSet::new))); + } +} +]]> +