IG: don't suggest to replace if with switch on non-constant (IDEA-204135)

This commit is contained in:
Bas Leijdekkers
2018-12-17 19:30:54 +01:00
parent a3b33f43a9
commit 32f3fccaf7
2 changed files with 33 additions and 1 deletions
@@ -230,7 +230,15 @@ public class SwitchUtils {
}
final PsiExpression left = check.getLeft();
final PsiExpression right = check.getRight();
return PsiUtil.isConstantExpression(left) ? right : left;
if (PsiUtil.isConstantExpression(left)) {
return right;
}
else if (PsiUtil.isConstantExpression(right)) {
return left;
}
else {
return null;
}
}
private static boolean canBeCaseLabel(PsiExpression expression, LanguageLevel languageLevel) {
@@ -92,4 +92,28 @@ class IfCanBeSwitch {
System.out.println("-");
}
}
}
class MyText implements Comparable<MyText> {
private String name;
String getSuperType() {
return name.startsWith("a") ? "b" : "c";
}
@Override
public int compareTo(MyText o) {
String superType = getSuperType();
if (superType.equals(o.getSuperType())) {
return this.name.compareTo(o.name);
} else if (superType.equals("a")) {
return -1;
} else if (superType.equals("b")) {
return 1;
} else if (superType.equals("c")) {
return o.getSuperType().equals("a") ? 1 : -1;
} else {
return 0;
}
}
}