Fixed array handling in UnusedFormalParameter

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4747 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2006-10-26 16:56:14 +00:00
parent ffc5bcb052
commit 6c5338fdaf
3 changed files with 24 additions and 4 deletions

View File

@ -25,7 +25,7 @@ Fixed bug in Ant task - CSV reports were being output as text.
Fixed false negatives in UseArraysAsList.
Fixed several JDK 1.5 parsing bugs.
Fixed several rules (exceptions on jdk 1.5 and jdk 1.6 source code).
Fixed array handling in AvoidReassigningParameters.
Fixed array handling in AvoidReassigningParameters and UnusedFormalParameter.
Rules can now call RuleContext.getSourceType() if they need to make different checks on JDK 1.4 and 1.5 code.
CloseResource rule now checks code without java.sql import.
ArrayIsStoredDirectly rule now checks Constructors

View File

@ -27,7 +27,9 @@ public class UnusedFormalParameterRuleTest extends SimpleAggregatorTst {
new TestDescriptor(TEST7, "anonymous inner class npe", 0, rule),
new TestDescriptor(TEST8, "unused constructor param", 1, rule),
new TestDescriptor(TEST9, "assigned but not used", 1, rule),
new TestDescriptor(TEST10, "skip array types for now", 0, rule)
new TestDescriptor(TEST10, "array element is set", 0, rule),
new TestDescriptor(TEST11, "unused array in constructor", 1, rule),
new TestDescriptor(TEST12, "unused array in method", 1, rule)
});
}
@ -104,4 +106,18 @@ public class UnusedFormalParameterRuleTest extends SimpleAggregatorTst {
" }" + PMD.EOL +
"}";
private static final String TEST11 =
"class Foo {" + PMD.EOL +
" int type;" + PMD.EOL +
" public Foo (int type, String[] s) {" + PMD.EOL +
" this.type = type;" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST12 =
"class Foo {" + PMD.EOL +
" private final void setLang(final String lang[]){" + PMD.EOL +
" }" + PMD.EOL +
"}";
}

View File

@ -40,7 +40,7 @@ public class UnusedFormalParameterRule extends AbstractRule {
for (Iterator i = vars.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
VariableNameDeclaration nameDecl = (VariableNameDeclaration) entry.getKey();
if (nameDecl.isArray() || actuallyUsed((List) entry.getValue())) {
if (actuallyUsed(nameDecl, (List) entry.getValue())) {
continue;
}
addViolation(data, node, new Object[]{node instanceof ASTMethodDeclaration ? "method" : "constructor", nameDecl.getImage()});
@ -48,10 +48,14 @@ public class UnusedFormalParameterRule extends AbstractRule {
}
}
private boolean actuallyUsed(List usages) {
private boolean actuallyUsed(VariableNameDeclaration nameDecl, List usages) {
for (Iterator j = usages.iterator(); j.hasNext();) {
NameOccurrence occ = (NameOccurrence) j.next();
if (occ.isOnLeftHandSide()) {
if (nameDecl.isArray() && occ.getLocation().jjtGetParent().jjtGetParent().jjtGetNumChildren() > 1) {
// array element access
return true;
}
continue;
} else {
return true;