False-positive UnnecessaryFullyQualifiedName when nested and non-nested classes with the same name and in the same package are used together #4085

* Regression fix;
This commit is contained in:
Oleg Andreych
2022-09-18 15:29:50 +05:00
parent 7f7f8f7b6a
commit af30f4cef9
2 changed files with 20 additions and 5 deletions

View File

@ -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;
}
}

View File

@ -641,6 +641,21 @@ public class OuterTestClass {
]]></code>
</test-code>
<test-code>
<description>Should report fully-qualified name usage of a class in itself.</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>4</expected-linenumbers>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryfullyqualifiedname;
public class TestClass {
public static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryfullyqualifiedname.TestClass INSTANCE(){
return new TestClass();
}
}
]]></code>
</test-code>
<test-code>
<description>#2098 false positive with annotated package</description>
<expected-problems>0</expected-problems>