Basic implementation of detecting apex unused local variables

This commit is contained in:
Gwilym Kuiper
2020-03-30 10:54:58 +01:00
parent a3cd0cf47d
commit d95763c504

View File

@ -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<ASTVariableExpression> potentialUsages = containerMethod.findChildrenOfType(ASTVariableExpression.class);
for (ASTVariableExpression usage : potentialUsages) {
if (usage.getImage().equals(variableName)) {
return data;
}
}
addViolation(data, node);
return data;
}
}