Optional compared with null: do not warn when subsequent isPresent check is found

IDEA-187578  Inspection to detect when Optional is compared with null
This commit is contained in:
Tagir Valeev
2018-03-09 19:30:59 +07:00
parent 273862e0bb
commit 350eba5000
3 changed files with 76 additions and 1 deletions
@@ -17,4 +17,25 @@ public class Test {
}
}
void test3 (Optional<String> opt) {
// this case is ignored
if(opt == null || !opt.isPresent()) {
System.out.println("null or absent");
}
}
void test4 (Optional<String> opt) {
// this case is ignored as well
if(opt != null && opt.isPresent()) {
System.out.println("present");
}
}
void test5 (Optional<String> opt, Optional<String> opt2) {
// warn: different optionals used
if(opt.isPresent() && opt2.isPresent()) {
System.out.println("present");
}
}
}
@@ -16,4 +16,25 @@ public class Test {
}
}
void test3 (Optional<String> opt) {
// this case is ignored
if(opt == null || !opt.isPresent()) {
System.out.println("null or absent");
}
}
void test4 (Optional<String> opt) {
// this case is ignored as well
if(opt != null && opt.isPresent()) {
System.out.println("present");
}
}
void test5 (Optional<String> opt, Optional<String> opt2) {
// warn: different optionals used
if(opt != null && opt2.isPresent()) {
System.out.println("present");
}
}
}