RFE - 1627581 - Unused(*) rules should respect annotations

@SuppressWarnings("unused") will suppress the rules currently in unusedcode.xml ruleset

Including unit tests.


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4971 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Allan Caplan
2007-01-24 02:44:17 +00:00
parent 0b79372cf5
commit 9a785ded03
5 changed files with 47 additions and 1 deletions

View File

@ -8,6 +8,7 @@ Fixed bug 1634078 - StringToString now recognizes toString on a String Array, ra
Fixed bug 1631646 - UselessOperationOnImmutable doesn't throw on variable.method().variable.
Fixed bug 1627830 - UseLocaleWithCaseConversions now works with compound string operations
Applied patch 1612455 - RFE 1411022 CompareObjectsWithEquals now catches the case where comparison is against new Object
Implemented RFE 1627581 - SuppressWarnings("unused") now suppresses all warnings in unusedcode.xml
XPath rules are now chained together for an extra speedup in processing
December 19, 2006 - 3.9:

View File

@ -289,6 +289,20 @@ public class Foo {
int y = 4 >> x;
foo(y);
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
unused local with assignment - Suppressed
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
@SuppressWarnings("unused")
public class Foo {
void foo() {
String fr = new String();
}
}
]]></code>
</test-code>

View File

@ -312,6 +312,18 @@ public class Foo {
void bar() {
super.x++;
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
SuppressWarnings("unused") unused private field
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
@SuppressWarnings("unused")
private String foo;
}
]]></code>
</test-code>

View File

@ -264,6 +264,18 @@ public class Foo {
int x = x();
}
private int x() { return 42;}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
SuppressWarnings("unused") - simple unused private method
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
@SuppressWarnings("unused")
private void foo() {}
}
]]></code>
</test-code>

View File

@ -4,10 +4,14 @@ package net.sourceforge.pmd.ast;
import net.sourceforge.pmd.Rule;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class ASTAnnotation extends SimpleJavaNode {
private static List unusedRules = Arrays.asList(new String[]{"UnusedPrivateField","UnusedLocalVariable","UnusedPrivateMethod","UnusedFormalParameter"});
public ASTAnnotation(int id) {
super(id);
}
@ -30,7 +34,10 @@ public class ASTAnnotation extends SimpleJavaNode {
for (Iterator iter = nodes.iterator(); iter.hasNext();) {
ASTLiteral element = (ASTLiteral) iter.next();
if (element.hasImageEqualTo("\"PMD\"")
|| element.hasImageEqualTo(ruleAnno)) {
|| element.hasImageEqualTo(ruleAnno)
// the SuppressWarnings("unused") annotation allows unused code
// to be igored and is a Java standard annotation
|| (element.hasImageEqualTo("\"unused\"") && unusedRules.contains(rule.getName()))) {
return true;
}
}