Skip to content

Commit

Permalink
Fix NPE in UnnecessaryLocalBeforeReturnRule
Browse files Browse the repository at this point in the history
 - Fixes pmd#1804
  • Loading branch information
jsotuyod committed Apr 30, 2019
1 parent 3a42d14 commit 9545ead
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private boolean isInitDataModifiedAfterInit(final VariableNameDeclaration variab
final ASTBlockStatement location = occ.getLocation().getFirstParentOfType(ASTBlockStatement.class);

// Is it used after initializing our "unnecessary" local but before the return statement?
if (isAfter(location, initializerStmt) && isAfter(rtnStmt, location)) {
if (location != null && isAfter(location, initializerStmt) && isAfter(rtnStmt, location)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,23 @@ public class UnnecessaryLocalBeforeReturnFP {
.make(i);
return o; // true positive
}
}
]]></code>
</test-code>

<test-code>
<description>#1804 [java] NPE with fields</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class ObjectCreator {
private static final String A = "";
private static final String B = "" + A; // the existence of this line causes the NPE.
public Object create() {
final Object o = new Object(A);
return o;
}
}
]]></code>
</test-code>
Expand Down

0 comments on commit 9545ead

Please sign in to comment.