Idempotent loop body inspection

Fixes IDEA-180227 Better infinite loop detection
This commit is contained in:
Tagir Valeev
2017-10-13 12:53:40 +07:00
parent d4d37d0c73
commit 326b6c5ba0
6 changed files with 274 additions and 1 deletions
@@ -0,0 +1,57 @@
import java.util.*;
class IdempotentLoopBody {
String getUniqueName(String baseName, Set<String> names) {
int index = 1;
String name = baseName;
<warning descr="Idempotent loop body">while</warning>(names.contains(name)) {
name = baseName + index;
}
return name;
}
String getUniqueNameComplex(String baseName, Set<String> names) {
int index = 1;
String name = baseName;
<warning descr="Idempotent loop body">while</warning> (names.contains(name)) {
String suffix = String.valueOf(index);
if (suffix.length() == 1) {
suffix = "0" + suffix;
}
name = baseName + suffix;
}
return name;
}
String getUniqueNameCorrect(String baseName, Set<String> names) {
int index = 1;
String name = baseName;
while(names.contains(name)) {
name = baseName + (index++);
}
return name;
}
String getUniqueNameFor(String baseName, Set<String> names) {
int index = 1;
String name;
<warning descr="Idempotent loop body">for</warning>(name = baseName; names.contains(name); name = baseName + index);
return name;
}
String leftPad(String val, int desiredLength) {
String result = val;
<warning descr="Idempotent loop body">while</warning>(result.length() < desiredLength) {
result = " " + val;
}
return result;
}
String leftPadCorrect(String val, int desiredLength) {
String result = val;
while(result.length() < desiredLength) {
result = " " + result;
}
return result;
}
}