diff --git a/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java b/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java index f8670487de95..4416fdd7fc6b 100644 --- a/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java @@ -186,9 +186,12 @@ public class OptionalIsPresentInspection extends AbstractBaseJavaLocalInspection if (!hasNoBadRefs) return ProblemType.NONE; if (!hasOptionalReference.get() || !(lambdaCandidate instanceof PsiExpression)) return ProblemType.INFO; PsiExpression expression = (PsiExpression)lambdaCandidate; - if (falseExpression != null && NullnessUtil.getExpressionNullness(expression, true) != Nullness.NOT_NULL) { + if (falseExpression != null && + !ExpressionUtils.isNullLiteral(falseExpression) && + NullnessUtil.getExpressionNullness(expression, true) != Nullness.NOT_NULL) { // falseExpression == null is "consumer" case (to be replaced with ifPresent()), // in this case we don't care about expression nullness + // if falseExpression is null literal, then semantics is preserved return ProblemType.INFO; } return ProblemType.WARNING; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/afterTernaryFunctionNull.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/afterTernaryFunctionNull.java new file mode 100644 index 000000000000..ce5765118c68 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/afterTernaryFunctionNull.java @@ -0,0 +1,13 @@ +// "Replace Optional.isPresent() condition with functional style expression" "GENERIC_ERROR_OR_WARNING" + +import java.util.Optional; +import java.util.function.Function; +import java.util.function.Supplier; + +public class Main { + + public void test(Optional opt, Function onPresent, Supplier onEmpty) { + // warning level: no semantics change + Object o = opt.map(onPresent::apply).orElse(null); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeTernaryFunctionNull.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeTernaryFunctionNull.java new file mode 100644 index 000000000000..ccdea8d7db3b --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeTernaryFunctionNull.java @@ -0,0 +1,13 @@ +// "Replace Optional.isPresent() condition with functional style expression" "GENERIC_ERROR_OR_WARNING" + +import java.util.Optional; +import java.util.function.Function; +import java.util.function.Supplier; + +public class Main { + + public void test(Optional opt, Function onPresent, Supplier onEmpty) { + // warning level: no semantics change + Object o = opt.isPresent() ? onPresent.apply(opt.get()) : null; + } +} \ No newline at end of file