Skip to content

Commit

Permalink
[java] UnnecessaryImport - keep analyzing with failed overload selection
Browse files Browse the repository at this point in the history
Fixes pmd#4816
  • Loading branch information
adangel committed Feb 29, 2024
1 parent 3b0dd7a commit ef18609
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/pages/release_notes.md
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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<JMethodSig, ScopeInfo> scopeIter =
Expand Down
@@ -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 "";
}
}
@@ -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<Item> stream() {
return null;
}
}
Expand Up @@ -1168,4 +1168,31 @@ public class Sample {
}
]]></code>
</test-code>

<test-code>
<description>[java] UnnecessaryImport false-positive on generic method call with on lambda #4816</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport;
import java.util.function.Function;
import java.util.stream.Collectors; // UnnecessaryImport: Unused import 'java.util.stream.Collectors'
import java.util.TreeSet; // UnnecessaryImport: Unused import 'java.util.TreeSet'
import java.util.Set;
import net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.item.Item; // UnnecessaryImport: Unused import 'net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.item.Item'
import net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.item.ItemProducer;
public class Example {
private <X> X run(Function<ItemProducer, X> f) { return null; }
public Set<String> sample() {
return run((producer) -> producer
.stream()
.map(Item::getValue)
.collect(Collectors.toCollection(TreeSet::new)));
}
}
]]></code>
</test-code>
</test-data>

0 comments on commit ef18609

Please sign in to comment.