Fix NPE in UnnecessaryLocalBeforeReturnRule

- Fixes #1804
This commit is contained in:
Juan Martín Sotuyo Dodero
2019-04-30 14:53:55 -03:00
parent 3a42d14ff2
commit 9545eada2f
2 changed files with 18 additions and 1 deletions

View File

@ -136,7 +136,7 @@ public class UnnecessaryLocalBeforeReturnRule extends AbstractJavaRule {
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;
}
}

View File

@ -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>