[java-highlighting] IDEA-284251 Better unreachable statement highlighting

GitOrigin-RevId: 738ac8d77fdf9000f2cb8c791a7ed3bfbddafca7
This commit is contained in:
Tagir Valeev
2021-12-08 02:48:46 +00:00
committed by intellij-monorepo-bot
parent 5aba52d3ad
commit ae33969db0
2 changed files with 58 additions and 2 deletions
@@ -1182,8 +1182,8 @@ public final class ControlFlowUtil {
@Nullable
private static PsiElement getUnreachableExpressionParent(@Nullable PsiElement element) {
if (element instanceof PsiExpression) {
final PsiElement expression = PsiTreeUtil.findFirstParent(element, e -> !(e.getParent() instanceof PsiParenthesizedExpression));
if (expression != null) {
PsiElement expression = PsiTreeUtil.findFirstParent(element, e -> !(e.getParent() instanceof PsiParenthesizedExpression));
while (expression != null) {
final PsiElement parent = expression.getParent();
if (parent instanceof PsiExpressionStatement) {
return getUnreachableStatementParent(parent);
@@ -1194,6 +1194,11 @@ public final class ControlFlowUtil {
parent instanceof PsiForeachStatement && ((PsiForeachStatement)parent).getIteratedValue() == expression) {
return parent;
}
if (parent instanceof PsiExpression) {
expression = parent;
} else {
break;
}
}
}
return null;
@@ -11,12 +11,43 @@ class WithCondition {
}
void testIf2(boolean b) {
return;
<error descr="Unreachable statement">if</error> (b == true) {
System.out.println("Never");
}
else {
System.out.println("Never ever");
}
}
void testIf3(boolean b) {
return;
<error descr="Unreachable statement">if</error> ((b)) {
System.out.println("Never");
}
}
void testIf4() {
return;
<error descr="Unreachable statement">if</error> (Math.abs(1) > 2) {
System.out.println();
}
}
void testIf5() {
return;
<error descr="Unreachable statement">if</error> (new int[] {1,2,3}.length == 3) {
System.out.println();
}
}
void testIf6(boolean b, boolean c, boolean d) {
return;
<error descr="Unreachable statement">if</error> (b ? c : d) {
System.out.println();
}
}
void testSwitch(int b) {
return;
<error descr="Unreachable statement">switch</error> (b) {
@@ -25,6 +56,14 @@ class WithCondition {
}
}
void testSwitch2(int b) {
return;
<error descr="Unreachable statement">switch</error> (b+1) {
case 1:
System.out.println("Never");
}
}
void testFor1(int b) {
return;
<error descr="Unreachable statement">for</error> (int i = 0; i < b; i++) {
@@ -77,6 +116,13 @@ class WithCondition {
}
}
void testWhile2(boolean b) {
return;
<error descr="Unreachable statement">while</error> (!b) {
System.out.println("Never");
}
}
void testDoWhile1(boolean b) {
return;
<error descr="Unreachable statement">do</error> System.out.println("Never");
@@ -90,4 +136,9 @@ class WithCondition {
}
while (b);
}
int testReturn() {
return 1;
<error descr="Unreachable statement">return 2;</error>
}
}