diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/UnusedLocalVariableRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/UnusedLocalVariableRule.java new file mode 100644 index 0000000000..0afc2ca2f6 --- /dev/null +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/UnusedLocalVariableRule.java @@ -0,0 +1,27 @@ +package net.sourceforge.pmd.lang.apex.rule.bestpractices; + +import net.sourceforge.pmd.lang.apex.ast.ASTMethod; +import net.sourceforge.pmd.lang.apex.ast.ASTVariableDeclaration; +import net.sourceforge.pmd.lang.apex.ast.ASTVariableExpression; +import net.sourceforge.pmd.lang.apex.rule.AbstractApexRule; + +import java.util.List; + +public class UnusedLocalVariableRule extends AbstractApexRule { + @Override + public Object visit(ASTVariableDeclaration node, Object data) { + String variableName = node.getImage(); + + ASTMethod containerMethod = node.getFirstParentOfType(ASTMethod.class); + List potentialUsages = containerMethod.findChildrenOfType(ASTVariableExpression.class); + + for (ASTVariableExpression usage : potentialUsages) { + if (usage.getImage().equals(variableName)) { + return data; + } + } + + addViolation(data, node); + return data; + } +}