Shifting more code into the framework
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4070 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -7,7 +7,54 @@ These are new rules that are still in progress
|
||||
</description>
|
||||
|
||||
|
||||
<!--
|
||||
<rule name="SimplifyConditional"
|
||||
message="No need to check for null before an instanceof"
|
||||
class="net.sourceforge.pmd.rules.XPathRule"
|
||||
externalInfoUrl="http://pmd.sourceforge.net/rules/design.html#SimplifyConditional">
|
||||
<description>
|
||||
No need to check for null before an instanceof; the instanceof keyword returns false when given a null argument.
|
||||
</description>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//Expression
|
||||
[ConditionalOrExpression
|
||||
[EqualityExpression[@Image='==']
|
||||
//NullLiteral
|
||||
and
|
||||
UnaryExpressionNotPlusMinus
|
||||
[@Image='!']//InstanceOfExpression[PrimaryExpression
|
||||
//Name/@Image = ancestor::ConditionalOrExpression/EqualityExpression
|
||||
//PrimaryPrefix/Name/@Image]]
|
||||
or
|
||||
ConditionalAndExpression
|
||||
[EqualityExpression[@Image='!=']//NullLiteral
|
||||
and
|
||||
InstanceOfExpression
|
||||
[PrimaryExpression[count(PrimarySuffix[@ArrayDereference='true'])=0]
|
||||
//Name/@Image = ancestor::ConditionalAndExpression
|
||||
/EqualityExpression//PrimaryPrefix/Name/@Image]]]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<priority>3</priority>
|
||||
<example>
|
||||
<![CDATA[
|
||||
class Foo {
|
||||
void bar(Object x) {
|
||||
if (x != null && x instanceof Bar) {
|
||||
// just drop the "x != null" check
|
||||
}
|
||||
}
|
||||
} ]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
|
||||
|
||||
<!--
|
||||
<rule name="UselessAssignment"
|
||||
message="This assignment to ''{0}'' is useless"
|
||||
class="net.sourceforge.pmd.rules.UselessAssignment"
|
||||
|
@ -5,6 +5,8 @@ package net.sourceforge.pmd.ast;
|
||||
import net.sourceforge.pmd.symboltable.NameDeclaration;
|
||||
import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ASTVariableDeclaratorId extends SimpleNode {
|
||||
|
||||
public ASTVariableDeclaratorId(int id) {
|
||||
@ -32,6 +34,10 @@ public class ASTVariableDeclaratorId extends SimpleNode {
|
||||
nameDeclaration = decl;
|
||||
}
|
||||
|
||||
public List getUsages() {
|
||||
return (List)getScope().getVariableDeclarations().get(nameDeclaration);
|
||||
}
|
||||
|
||||
public void bumpArrayDepth() {
|
||||
arrayDepth++;
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import net.sourceforge.pmd.AbstractRule;
|
||||
import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration;
|
||||
import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
|
||||
import net.sourceforge.pmd.symboltable.NameOccurrence;
|
||||
import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@ -16,12 +15,11 @@ public class UnusedLocalVariableRule extends AbstractRule {
|
||||
|
||||
public Object visit(ASTVariableDeclaratorId node, Object data) {
|
||||
if (node.jjtGetParent().jjtGetParent() instanceof ASTLocalVariableDeclaration) {
|
||||
VariableNameDeclaration decl = (VariableNameDeclaration)node.getNameDeclaration();
|
||||
// TODO this isArray() check misses some cases
|
||||
// need to add DFAish code to determine if an array
|
||||
// is initialized locally or gotten from somewhere else
|
||||
if (!decl.isArray() && !actuallyUsed((List)node.getScope().getVariableDeclarations().get(decl))) {
|
||||
addViolation(data, decl.getNode(), decl.getImage());
|
||||
if (!node.getNameDeclaration().isArray() && !actuallyUsed(node.getUsages())) {
|
||||
addViolation(data, node.getNameDeclaration().getNode(), node.getNameDeclaration().getImage());
|
||||
}
|
||||
}
|
||||
return data;
|
||||
|
Reference in New Issue
Block a user