Variable is used if method is called on it
This commit is contained in:
@ -4,11 +4,14 @@
|
||||
|
||||
package net.sourceforge.pmd.lang.apex.rule.bestpractices;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTReferenceExpression;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTVariableDeclaration;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ASTVariableExpression;
|
||||
import net.sourceforge.pmd.lang.apex.ast.ApexNode;
|
||||
import net.sourceforge.pmd.lang.apex.rule.AbstractApexRule;
|
||||
|
||||
public class UnusedLocalVariableRule extends AbstractApexRule {
|
||||
@ -21,9 +24,15 @@ public class UnusedLocalVariableRule extends AbstractApexRule {
|
||||
String variableName = node.getImage();
|
||||
|
||||
ASTBlockStatement variableContext = node.getFirstParentOfType(ASTBlockStatement.class);
|
||||
List<ASTVariableExpression> potentialUsages = variableContext.findDescendantsOfType(ASTVariableExpression.class);
|
||||
|
||||
for (ASTVariableExpression usage : potentialUsages) {
|
||||
List<ApexNode<?>> potentialUsages = new ArrayList<>();
|
||||
|
||||
// Variable expression catch things like the `a` in `a + b`
|
||||
potentialUsages.addAll(variableContext.findDescendantsOfType(ASTVariableExpression.class));
|
||||
// Reference expressions catch things like the `a` in `a.foo()`
|
||||
potentialUsages.addAll(variableContext.findDescendantsOfType(ASTReferenceExpression.class));
|
||||
|
||||
for (ApexNode<?> usage : potentialUsages) {
|
||||
if (usage.getParent() == node) {
|
||||
continue;
|
||||
}
|
||||
|
@ -51,6 +51,11 @@ public class Foo {
|
||||
return 'some other string';
|
||||
}
|
||||
}
|
||||
|
||||
public void hasMethodCalledOnIt() {
|
||||
String foo = 'foobar';
|
||||
foo.substringAfter('foo');
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</code>
|
||||
|
Reference in New Issue
Block a user