unreachable code: qualified fields and array access in assignments (IDEA-144428)

This commit is contained in:
Anna Kozlova
2015-08-28 18:33:15 +03:00
parent e68462d50a
commit 3d96977c2c
5 changed files with 63 additions and 20 deletions
@@ -95,7 +95,8 @@ public class HighlightControlFlowUtil {
// do not compute constant expressions for if() statement condition
// see JLS 14.20 Unreachable Statements
try {
final ControlFlow controlFlow = getControlFlowNoConstantEvaluate(codeBlock);
AllVariablesControlFlowPolicy policy = AllVariablesControlFlowPolicy.getInstance();
final ControlFlow controlFlow = ControlFlowFactory.getInstance(codeBlock.getProject()).getControlFlow(codeBlock, policy, false, false);
final PsiElement unreachableStatement = ControlFlowUtil.getUnreachableStatement(controlFlow);
if (unreachableStatement != null) {
String description = JavaErrorMessages.message("unreachable.statement");
@@ -1301,29 +1301,39 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
if (rExpr != null) {
rExpr.accept(this);
}
final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)lExpr;
PsiExpression qualifierExpression = referenceExpression.getQualifierExpression();
PsiVariable variable = getUsedVariable(referenceExpression);
if (qualifierExpression == null ||
qualifierExpression instanceof PsiThisExpression ||
variable instanceof PsiField && variable.hasModifierProperty(PsiModifier.STATIC)) {
if (variable != null) {
if (myAssignmentTargetsAreElements) {
startElement(lExpr);
}
if (expression.getOperationTokenType() != JavaTokenType.EQ) {
generateReadInstruction(variable);
}
generateWriteInstruction(variable);
if (myAssignmentTargetsAreElements) finishElement(lExpr);
PsiVariable variable = getUsedVariable((PsiReferenceExpression)lExpr);
if (variable != null) {
if (myAssignmentTargetsAreElements) {
startElement(lExpr);
}
if (expression.getOperationTokenType() != JavaTokenType.EQ) {
generateReadInstruction(variable);
}
generateWriteInstruction(variable);
if (myAssignmentTargetsAreElements) finishElement(lExpr);
}
else {
lExpr.accept(this); //?
}
}
else if (lExpr instanceof PsiArrayAccessExpression &&
((PsiArrayAccessExpression)lExpr).getArrayExpression() instanceof PsiReferenceExpression){
PsiVariable variable = getUsedVariable((PsiReferenceExpression)((PsiArrayAccessExpression)lExpr).getArrayExpression());
if (variable != null) {
generateReadInstruction(variable);
final PsiExpression indexExpression = ((PsiArrayAccessExpression)lExpr).getIndexExpression();
if (indexExpression != null) {
indexExpression.accept(this);
}
} else {
lExpr.accept(this);
}
if (rExpr != null) {
rExpr.accept(this);
}
}
else if (lExpr != null) {
lExpr.accept(this);
if (rExpr != null) {
@@ -0,0 +1,31 @@
class Test {
String[] days = {"",""};
public static void foo1() {
for (; ; ) {
break;
<error descr="Unreachable statement">new Test().days = null;</error>
}
}
void bar1() {
for (; ; ) {
break;
<error descr="Unreachable statement">days = null;</error>
}
}
static void foo2() {
for (; ; ) {
break;
<error descr="Unreachable statement">new Test().days[0] = null;</error>
}
}
void bar2() {
for (;;) {
break;
<error descr="Unreachable statement">days[0] = null;</error>
}
}
}
@@ -1,8 +1,8 @@
public class A {
public void testInlineRefactoring() {
int[] array = ar();
arr<caret>ay[1] = 22;
int[] ar<caret>ray = ar();
array[1] = 22;
}
private int[] ar() {
@@ -58,4 +58,5 @@ public class LightAdvHighlightingJdk6Test extends LightDaemonAnalyzerTestCase {
public void testGenericArrayCreationWithGenericTypeWithOneUnboundedWildcardOneNormalParams() { doTest(false, false); }
public void testAgentPremain() { doTest(false, false); }
public void testInitializedBeforeUsed() { doTest(false, false); }
public void testUnreachableAssignments() { doTest(false, false); }
}