[apex] Exception applying rule UnusedLocalVariable on trigger

Fixes #2554
This commit is contained in:
Andreas Dangel
2020-05-30 11:08:44 +02:00
parent bc4a1d67eb
commit c680b260b9
3 changed files with 18 additions and 0 deletions

View File

@ -16,6 +16,9 @@ This is a {{ site.pmd.release_type }} release.
### Fixed Issues
* apex-bestpractices
* [#2554](https://github.com/pmd/pmd/issues/2554): \[apex] Exception applying rule UnusedLocalVariable on trigger
### API Changes
### External Contributions

View File

@ -24,6 +24,10 @@ public class UnusedLocalVariableRule extends AbstractApexRule {
String variableName = node.getImage();
ASTBlockStatement variableContext = node.getFirstParentOfType(ASTBlockStatement.class);
if (variableContext == null) {
// if there is no parent BlockStatement, e.g. in triggers
return data;
}
List<ApexNode<?>> potentialUsages = new ArrayList<>();

View File

@ -83,6 +83,17 @@ public class Foo {
public String fieldUsage() {
return myfield;
}
}
]]></code>
</test-code>
<test-code>
<description>NPE with triggers (#2554)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
trigger leadOwnerUpdate on Lead (after update) {
for(Lead Id : Trigger.new) {
}
}
]]></code>
</test-code>