Java: Highlight unreachable assignment of array element (IDEA-199556)

This commit is contained in:
Pavel Dolgov
2018-10-26 13:11:13 +03:00
parent 4c44e36881
commit e6e432446a
3 changed files with 13 additions and 0 deletions
@@ -1324,6 +1324,7 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
myStartStatementStack.pushStatement(rExpr == null ? expression : rExpr, false);
myEndStatementStack.pushStatement(rExpr == null ? expression : rExpr, false);
boolean generatedWriteInstruction = false;
PsiExpression lExpr = PsiUtil.skipParenthesizedExprDown(expression.getLExpression());
if (lExpr instanceof PsiReferenceExpression) {
PsiVariable variable = getUsedVariable((PsiReferenceExpression)lExpr);
@@ -1344,6 +1345,7 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
rExpr.accept(this);
}
generateWriteInstruction(variable);
generatedWriteInstruction = true;
if (myAssignmentTargetsAreElements) finishElement(lExpr);
}
@@ -1376,6 +1378,8 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
rExpr.accept(this);
}
}
//each statement should contain at least one instruction in order to getElement(offset) work
if (!generatedWriteInstruction) emitEmptyInstruction();
myStartStatementStack.popStatement();
myEndStatementStack.popStatement();
@@ -0,0 +1,7 @@
class C {
void foo(String arg) throws Exception {
Object[] objects;
throw new Exception("");
<error descr="Unreachable statement">objects[0] = arg;</error>
}
}
@@ -418,6 +418,8 @@ public class LightAdvHighlightingTest extends LightDaemonAnalyzerTestCase {
doTest(false);
}
public void testUnreachableArrayElementAssignment() { doTest(false); }
public void testInsane() {
configureFromFileText("x.java", "class X { \nx_x_x_x\n }");
List<HighlightInfo> infos = highlightErrors();