diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index eed986229f..8544ed8795 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -2,6 +2,7 @@ Fixed bug 2590258 - NPE with nicerhtml output Fixed bug 2317099 - False + in SimplifyCondition +Fixed bug 2606609 - False "UnusedImports" positive in package-info.java New rule: StrictExceptions : AvoidCatchingGenericException diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/imports/xml/UnusedImports.xml b/pmd/regress/test/net/sourceforge/pmd/rules/imports/xml/UnusedImports.xml index 46fad32ac4..20855d00db 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/imports/xml/UnusedImports.xml +++ b/pmd/regress/test/net/sourceforge/pmd/rules/imports/xml/UnusedImports.xml @@ -203,7 +203,7 @@ public class Foo { ]]> - + @@ -212,7 +212,8 @@ Bug 2606609 : False "UnusedImports" positive in package-info.java @DefaultAnnotation(NonNull.class) package net.sourceforge.pmd.test; -public class Bug {} +import edu.umd.cs.findbugs.annotations.DefaultAnnotation; +import edu.umd.cs.findbugs.annotations.NonNull; ]]> diff --git a/pmd/src/net/sourceforge/pmd/rules/imports/UnusedImportsRule.java b/pmd/src/net/sourceforge/pmd/rules/imports/UnusedImportsRule.java index 94455a6488..56b471b231 100644 --- a/pmd/src/net/sourceforge/pmd/rules/imports/UnusedImportsRule.java +++ b/pmd/src/net/sourceforge/pmd/rules/imports/UnusedImportsRule.java @@ -8,6 +8,7 @@ import net.sourceforge.pmd.ast.ASTClassOrInterfaceType; import net.sourceforge.pmd.ast.ASTCompilationUnit; import net.sourceforge.pmd.ast.ASTImportDeclaration; import net.sourceforge.pmd.ast.ASTName; +import net.sourceforge.pmd.ast.ASTPackageDeclaration; import net.sourceforge.pmd.ast.Comment; import net.sourceforge.pmd.ast.FormalComment; import net.sourceforge.pmd.ast.SimpleJavaNode; @@ -27,6 +28,14 @@ public class UnusedImportsRule extends AbstractRule { imports.clear(); super.visit(node, data); visitComments(node); + + /* special handling for Bug 2606609 : False "UnusedImports" positive in package-info.java + * package annotations are processed before the import clauses so they need to be examined + * again later on. + */ + if (node.jjtGetNumChildren()>0 && node.jjtGetChild(0) instanceof ASTPackageDeclaration) { + visit((ASTPackageDeclaration)node.jjtGetChild(0), data); + } for (ImportWrapper wrapper : imports) { addViolation(data, wrapper.getNode(), wrapper.getFullName()); }