From 61ac76cf4857075dc90628f1f27d26ac6537862f Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 29 Apr 2019 16:35:42 +0700 Subject: [PATCH] IDEA-211702 Incorrect inspection "Can be replaced with single expression in functional style" when types differ GitOrigin-RevId: 4006edb212486fa38b31b26ecbed881e6079f6e6 --- .../OptionalIsPresentInspection.java | 14 ++++++++++---- .../beforeDifferentReturnTypes.java | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeDifferentReturnTypes.java diff --git a/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java b/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java index ad9aca882f62..e0395e33c477 100644 --- a/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java @@ -191,10 +191,16 @@ public class OptionalIsPresentInspection extends AbstractBaseJavaLocalInspection } PsiType falseType = falseExpression.getType(); PsiType trueType = expression.getType(); - // like x ? double_expression : integer_expression; support only if integer_expression is simple literal, - // so could be converted explicitly to double - if (falseType instanceof PsiPrimitiveType && trueType instanceof PsiPrimitiveType && - !falseType.equals(trueType) && JavaPsiMathUtil.getNumberFromLiteral(falseExpression) == null) { + if (falseType == null || trueType == null) return ProblemType.NONE; + if (falseType instanceof PsiPrimitiveType && trueType instanceof PsiPrimitiveType) { + if (falseType.equals(trueType) || JavaPsiMathUtil.getNumberFromLiteral(falseExpression) != null) { + // like x ? double_expression : integer_expression; support only if integer_expression is simple literal, + // so could be converted explicitly to double + return ProblemType.WARNING; + } + return ProblemType.NONE; + } + if (!trueType.isAssignableFrom(falseType)) { return ProblemType.NONE; } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeDifferentReturnTypes.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeDifferentReturnTypes.java new file mode 100644 index 000000000000..a6d30997556d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeDifferentReturnTypes.java @@ -0,0 +1,14 @@ +// "Replace Optional.isPresent() condition with functional style expression" "false" +import java.util.Optional; + +class Test { + Object get() { + Optional obj = Stream.of("one", "two").filter(s -> Math.sqrt(s.length()) > 100).findFirst(); + if (obj.isPresent()) { + return obj.get(); + } else { + return 7; + } + } + +} \ No newline at end of file