ObviousNullCheckInspection: improve extraction of side-effect statements when ternary operator or short-circuiting logic is involved.

This commit is contained in:
Tagir Valeev
2017-05-18 16:47:19 +07:00
parent f5af458506
commit f7a43a77ff
4 changed files with 338 additions and 8 deletions
@@ -0,0 +1,68 @@
// "Fix all 'Null-check method is called with obviously non-null argument' problems in file" "true"
import java.util.Objects;
public class Test {
Test(int i) {
}
public static void testTernaryLeft() {
if (args.length > 0) {
new Test(1);
new Test(2);
}
}
public static void testTernaryRight() {
if (args.length <= 0) {
new Test(2);
new Test(3);
}
}
public static void testTernaryBoth() {
if (args.length > 0) {
new Test(1);
} else {
new Test(2);
new Test(3);
}
}
public static void testAndTernarySimply() {
new Test(1).hashCode();
}
public static void testAndTernaryBranch() {
if (new Test(1).hashCode() <= 0 || args.length <= 0) {
new Test(2);
}
}
public static void testAndBoth() {
if (new Test(1).hashCode() > 0) {
new Test(2).hashCode();
}
}
public static void testAndTwoOfThree() {
if (new Test(1).hashCode() > 0) {
new Test(2).hashCode();
}
}
public static void testAndTwoOfThreePlusBranch() {
if (new Test(1).hashCode() > 0 && new Test(2).hashCode() > 0
&& args.length > 0) {
new Test(3).toString();
}
}
public static void testAndOrMixed() {
if (new Test(1).hashCode() <= 0 || new Test(2).hashCode() <= 0) {
if (new Test(3).hashCode() + new Test(4).hashCode() > 1) {
new Test(5).hashCode();
new Test(6).hashCode();
}
}
}
}
@@ -0,0 +1,47 @@
// "Fix all 'Null-check method is called with obviously non-null argument' problems in file" "true"
import java.util.Objects;
public class Test {
Test(int i) {
}
public static void testTernaryLeft() {
Objects.requireNonNull("xyz" +<caret> (args.length > 0 ? new Test(1) + ":" + new Test(2) : ""));
}
public static void testTernaryRight() {
Objects.requireNonNull("xyz" + (args.length > 0 ? "null" : new Test(2)+":"+new Test(3)));
}
public static void testTernaryBoth() {
Objects.requireNonNull("xyz" + (args.length > 0 ? new Test(1) : new Test(2)+":"+new Test(3)));
}
public static void testAndTernarySimply() {
Objects.requireNonNull("xyz" + (new Test(1).hashCode() > 0 && args.length > 0 ? "x" : "y"));
}
public static void testAndTernaryBranch() {
Objects.requireNonNull("xyz" + (new Test(1).hashCode() > 0 && args.length > 0 ? "x" : new Test(2)));
}
public static void testAndBoth() {
Objects.requireNonNull("xyz" + (new Test(1).hashCode() > 0 && new Test(2).hashCode() > 0 ? "x" : "y"));
}
public static void testAndTwoOfThree() {
Objects.requireNonNull("xyz" + (new Test(1).hashCode() > 0 && new Test(2).hashCode() > 0
&& args.length > 0 ? "x" : "y"));
}
public static void testAndTwoOfThreePlusBranch() {
Objects.requireNonNull("xyz" + (new Test(1).hashCode() > 0 && new Test(2).hashCode() > 0
&& args.length > 0 ? new Test(3).toString() : "y"));
}
public static void testAndOrMixed() {
Objects.requireNonNull("xyz" + (new Test(1).hashCode() > 0 && new Test(2).hashCode() > 0
|| new Test(3).hashCode() + new Test(4).hashCode() > 1 &&
new Test(5).hashCode() + new Test(6).hashCode() > 2));
}
}