From af30f4cef92d882d253ee2f0c9d5144737412635 Mon Sep 17 00:00:00 2001 From: Oleg Andreych Date: Sun, 18 Sep 2022 15:29:50 +0500 Subject: [PATCH] False-positive UnnecessaryFullyQualifiedName when nested and non-nested classes with the same name and in the same package are used together #4085 * Regression fix; --- .../UnnecessaryFullyQualifiedNameRule.java | 10 +++++----- .../xml/UnnecessaryFullyQualifiedName.xml | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java index 36773e3a91..5348abca59 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java @@ -188,10 +188,11 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule { if (matches.isEmpty()) { if (isJavaLangImplicit(node)) { - addViolation(data, node, new Object[]{node.getImage(), "java.lang.*", "implicit "}); + asCtx(data).addViolation(node, + node.getImage(), "java.lang.*", "implicit "); } else if (isSamePackage(node, name)) { if (!hasSameSimpleNameInScope(node)) { - addViolation(data, node, new Object[]{node.getImage(), currentPackage + ".*", "same package "}); + asCtx(data).addViolation(node, node.getImage(), currentPackage + ".*", "same package "); } } } else { @@ -217,10 +218,9 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule { return false; } - final String nodeSimpleName = nodeType.getSimpleName(); - for (ASTClassOrInterfaceDeclaration declarationDescendant : declarationDescendants) { - if (nodeSimpleName.equals(declarationDescendant.getSimpleName())) { + if (nodeType.getSimpleName().equals(declarationDescendant.getSimpleName()) + && !nodeType.getName().equals(declarationDescendant.getQualifiedName().toString())) { return true; } } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml index 865f62c789..426bb9b4b6 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml @@ -641,6 +641,21 @@ public class OuterTestClass { ]]> + + Should report fully-qualified name usage of a class in itself. + 1 + 4 + + + #2098 false positive with annotated package 0