Simplify boolean expression improvements

1. Add "may change semantics" when side-effect cannot be extracted
2. Use condition reporting (instead of constant value reporting) for non-short-circuiting & and | (fixes IDEA-216245)
3. Use SimplifyBooleanExpressionFix for boolean constant value instead of replace fix.

GitOrigin-RevId: f3e63a75bd92a7305e36fce58bf9e34bdbb03c51
This commit is contained in:
Tagir Valeev
2019-07-02 06:52:16 +03:00
committed by intellij-monorepo-bot
parent a7decca33a
commit 0b3fd46151
9 changed files with 118 additions and 21 deletions
@@ -0,0 +1,10 @@
// "Simplify 'Objects.nonNull(...)' to true extracting side effects" "true"
import java.util.Set;
import java.util.Objects;
class X {
void test(Set<String> set) {
set.add("foo");
boolean b = true;
}
}
@@ -0,0 +1,13 @@
// "Unwrap 'do-while' statement (may change semantics)" "true"
import org.jetbrains.annotations.Contract;
class X {
@Contract("_ -> true")
boolean test(Object obj) {
return true;
}
void doSmth(Object obj) {
System.out.println("aaahh");
}
}
@@ -0,0 +1,23 @@
// "Simplify 'pureConsumer(...)' to true extracting side effects" "true"
import org.jetbrains.annotations.Contract;
public class Main {
private static int counter = 0;
public static void main(String[] args) {
while (true) {
sideEffect();
if (!(counter < 5)) break;
System.out.println(counter);
}
}
@Contract(value = "!null->true;null->false", pure = true)
private static boolean pureConsumer(Object consumed) {
return consumed != null;
}
private static int sideEffect() {
return counter++;
}
}
@@ -0,0 +1,9 @@
// "Simplify 'Objects.nonNull(...)' to true extracting side effects" "true"
import java.util.Set;
import java.util.Objects;
class X {
void test(Set<String> set) {
boolean b = Objects.nonNull(set.<caret>add("foo"));
}
}
@@ -0,0 +1,16 @@
// "Unwrap 'do-while' statement (may change semantics)" "true"
import org.jetbrains.annotations.Contract;
class X {
@Contract("_ -> true")
boolean test(Object obj) {
return true;
}
void doSmth(Object obj) {
do {
System.out.println("aaahh");
}
while(obj <caret>!= null && test(obj) && obj == null);
}
}
@@ -0,0 +1,21 @@
// "Simplify 'pureConsumer(...)' to true extracting side effects" "true"
import org.jetbrains.annotations.Contract;
public class Main {
private static int counter = 0;
public static void main(String[] args) {
while (pureConsumer<caret>(sideEffect()) & counter < 5) {
System.out.println(counter);
}
}
@Contract(value = "!null->true;null->false", pure = true)
private static boolean pureConsumer(Object consumed) {
return consumed != null;
}
private static int sideEffect() {
return counter++;
}
}