[java] Fix NPE with ForLoopCanBeForeachRule when using this
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
@@ -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>
|
||||
|
Reference in New Issue
Block a user