SimplifyBooleanExpressionFix: support always-false last disjunct in if

Fixes IDEA-200961 Simplification of "!list.add(type) is always false" may change code semantic
This commit is contained in:
Tagir Valeev
2018-10-23 17:44:26 +07:00
parent 16bda0dc48
commit 2e36108630
5 changed files with 186 additions and 11 deletions
@@ -0,0 +1,54 @@
// "Fix all 'Constant conditions & exceptions' problems in file" "true"
import java.util.ArrayList;
import java.util.List;
class Mutant {
List<String> types = new ArrayList<>();
void consider(String type, boolean unmodifiable) {
if (unmodifiable) {
if(Math.random() > 0.5) {
System.out.println("1");
}
} else {
types.add(type);
System.out.println("2");
}
if (unmodifiable) {
if(Math.random() > 0.5) {
System.out.println("1");
}
} else {
types.add(type);
}
if (unmodifiable) {
if(Math.random() > 0.5) {
System.out.println("1");
}
} else {
types.add(type);
}
if (unmodifiable)
System.out.println("1");
else {
types.add(type);
System.out.println("2");
}
// Cannot extract side effect in the middle
if (unmodifiable || Math.random() > 0.5)
System.out.println("1");
else
System.out.println("2");
types.add(type);
if (unmodifiable)
System.out.println("1");
else
System.out.println("2");
System.out.println("types = " + types);
}
public static void main(String[] args) {
new Mutant().consider("A", false);
}
}
@@ -0,0 +1,45 @@
// "Fix all 'Constant conditions & exceptions' problems in file" "true"
import java.util.ArrayList;
import java.util.List;
class Mutant {
List<String> types = new ArrayList<>();
void consider(String type, boolean unmodifiable) {
if (unmodifiable || !types.<caret>add(type)) {
if(Math.random() > 0.5) {
System.out.println("1");
}
} else {
System.out.println("2");
}
if (unmodifiable || !types.add(type)) {
if(Math.random() > 0.5) {
System.out.println("1");
}
}
if (unmodifiable || !types.add(type))
if(Math.random() > 0.5) {
System.out.println("1");
}
if (unmodifiable || !types.add(type))
System.out.println("1");
else
System.out.println("2");
// Cannot extract side effect in the middle
if (unmodifiable || !types.add(type) || Math.random() > 0.5)
System.out.println("1");
else
System.out.println("2");
if (!types.add(type) || unmodifiable)
System.out.println("1");
else
System.out.println("2");
System.out.println("types = " + types);
}
public static void main(String[] args) {
new Mutant().consider("A", false);
}
}