mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-168715 Dead code inspection doesn't work in loop (Conservatively handle including bound)
This commit is contained in:
+6
-1
@@ -514,8 +514,13 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
|
||||
private void addCountingLoopBound(PsiForStatement statement) {
|
||||
CountingLoop loop = CountingLoop.from(statement);
|
||||
if (loop == null) return;
|
||||
// Add known-to-be-true condition inside counting loop, effectively converting
|
||||
PsiLocalVariable counter = loop.getCounter();
|
||||
if (loop.isIncluding() && !(PsiType.LONG.equals(counter.getType()) && PsiType.INT.equals(loop.getBound().getType()))) {
|
||||
Object bound = ExpressionUtils.computeConstantExpression(loop.getBound());
|
||||
// could be for(int i=0; i<=Integer.MAX_VALUE; i++) which will overflow: conservatively skip this
|
||||
if (!(bound instanceof Number)) return;
|
||||
if (bound.equals(Long.MAX_VALUE) || bound.equals(Integer.MAX_VALUE)) return;
|
||||
}
|
||||
PsiExpression initializer = loop.getInitializer();
|
||||
if (!PsiType.INT.equals(initializer.getType()) && !PsiType.LONG.equals(initializer.getType())) return;
|
||||
DfaValue origin = null;
|
||||
|
||||
@@ -25,7 +25,7 @@ public class LongRangeLoop {
|
||||
|
||||
public static void loopOrigin(long i) {
|
||||
if(i > 0) {
|
||||
for(long j = i; j < 1000; ++j) {
|
||||
for (long j = i; j <= 1000; ++j) {
|
||||
if (<warning descr="Condition 'j < 0' is always 'false'">j < 0</warning>) {
|
||||
System.out.println("Impossible");
|
||||
}
|
||||
@@ -33,6 +33,30 @@ public class LongRangeLoop {
|
||||
}
|
||||
}
|
||||
|
||||
public static void loopUnknownBoundIncluding(int bound) {
|
||||
for (int i = 0; i <= bound; i++) {
|
||||
if (i == -1) {
|
||||
System.out.println("Overflow detected: bound was Integer.MAX_VALUE");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void loopUnknownBoundExcluding(int bound) {
|
||||
for (int i = 0; i < bound; i++) {
|
||||
if (<warning descr="Condition 'i == -1' is always 'false'">i == -1</warning>) {
|
||||
System.out.println("Impossible even if bound is Integer.MAX_VALUE");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void loopUnknownBoundIncludingLong(int bound) {
|
||||
for (long i = 0; i <= bound; i++) {
|
||||
if (<warning descr="Condition 'i == -1' is always 'false'">i == -1</warning>) {
|
||||
System.out.println("Impossible even if bound is Integer.MAX_VALUE");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void IDEA168715() {
|
||||
for (int i = 1; i < 100; i++) {
|
||||
if (<warning descr="Condition 'i == 0' is always 'false'">i == 0</warning>) {
|
||||
|
||||
Reference in New Issue
Block a user