[java] Fix NPE with ForLoopCanBeForeachRule when using this

This commit is contained in:
Andreas Dangel
2018-05-19 12:02:11 +02:00
parent 0b90fd0154
commit 17fc7fae5a
2 changed files with 24 additions and 3 deletions

View File

@@ -340,8 +340,10 @@ public class ForLoopCanBeForeachRule extends AbstractJavaRule {
Node prefix = suffix.jjtGetParent().jjtGetChild(0);
if (!(prefix instanceof ASTPrimaryPrefix) && prefix.jjtGetNumChildren() != 1
&& !(prefix.jjtGetChild(0) instanceof ASTName)) {
if (!(prefix instanceof ASTPrimaryPrefix) || prefix.jjtGetNumChildren() != 1
|| !(prefix.jjtGetChild(0) instanceof ASTName)) {
// it's either not a primary prefix, doesn't have children (can happen with this./super.)
// or first child is not a name
return false;
}

View File

@@ -304,7 +304,8 @@
}
}
]]></code>
</test-code> <test-code>
</test-code>
<test-code>
<description>NPE when for init is there, but not a local var declaration, refs #884</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
@@ -330,5 +331,23 @@
]]></code>
</test-code>
<test-code>
<description>NPE when using instance fields with this</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.util.List;
public class Test {
private int[] hashes;
public void foo() {
List<String> stringList;
this.hashes = new int[stringList.size()];
for (int i = 0; i < stringList.size(); i++) {
this.hashes[i] = stringList.get(i).hashCode();
}
}
}
]]></code>
</test-code>
</test-data>