OptionalIsPresentInspection: do not warn if map() part can be null

If we cannot determine the non-nullity of map expression, then info
level is used. Fixes IDEA-172609 "Replace Optional.isPresent() checks
with functional-style expressions" is broken.
This commit is contained in:
Tagir Valeev
2017-05-31 17:48:57 +07:00
parent 763953d92a
commit 588d978362
9 changed files with 102 additions and 8 deletions
@@ -1,4 +1,4 @@
// "Replace Optional.isPresent() condition with functional style expression" "true"
// "Replace Optional.isPresent() condition with functional style expression" "GENERIC_ERROR_OR_WARNING"
import java.util.Arrays;
import java.util.List;
@@ -0,0 +1,13 @@
// "Replace Optional.isPresent() condition with functional style expression" "INFORMATION"
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
public class Main {
public void test(Optional<String> opt, Function<String, Object> onPresent, Supplier<Object> onEmpty) {
// information level: could be semantic change if onPresent returns null
Object o = opt.map(onPresent::apply).orElseGet(onEmpty::get);
}
}
@@ -0,0 +1,14 @@
// "Replace Optional.isPresent() condition with functional style expression" "GENERIC_ERROR_OR_WARNING"
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
public class Main {
public void test(Optional<String> opt, Function<String, @NotNull Object> onPresent, Supplier<Object> onEmpty) {
Object o = opt.map(onPresent::apply).orElseGet(onEmpty::get);
}
}
@@ -1,4 +1,4 @@
// "Replace Optional.isPresent() condition with functional style expression" "true"
// "Replace Optional.isPresent() condition with functional style expression" "GENERIC_ERROR_OR_WARNING"
import java.util.Arrays;
import java.util.List;
@@ -0,0 +1,13 @@
// "Replace Optional.isPresent() condition with functional style expression" "INFORMATION"
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
public class Main {
public void test(Optional<String> opt, Function<String, Object> onPresent, Supplier<Object> onEmpty) {
// information level: could be semantic change if onPresent returns null
Object o = opt.is<caret>Present() ? onPresent.apply(opt.get()) : onEmpty.get();
}
}
@@ -0,0 +1,14 @@
// "Replace Optional.isPresent() condition with functional style expression" "GENERIC_ERROR_OR_WARNING"
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
public class Main {
public void test(Optional<String> opt, Function<String, @NotNull Object> onPresent, Supplier<Object> onEmpty) {
Object o = opt.i<caret>sPresent() ? onPresent.apply(opt.get()) : onEmpty.get();
}
}