diff --git a/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java b/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java index 5cedc96d59b4..6c79ed10075c 100644 --- a/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/OptionalIsPresentInspection.java @@ -157,10 +157,9 @@ public class OptionalIsPresentInspection extends BaseJavaBatchLocalInspectionToo if (!(element instanceof PsiMethodCallExpression)) return false; PsiMethodCallExpression call = (PsiMethodCallExpression)element; if (call.getArgumentList().getExpressions().length != 0) return false; - if (!"get".equals(call.getMethodExpression().getReferenceName())) return false; - PsiExpression qualifier = call.getMethodExpression().getQualifierExpression(); - if (!(qualifier instanceof PsiReferenceExpression)) return false; - return ((PsiReferenceExpression)qualifier).isReferenceTo(variable); + PsiReferenceExpression methodExpression = call.getMethodExpression(); + return "get".equals(methodExpression.getReferenceName()) && + ExpressionUtils.isReferenceTo(methodExpression.getQualifierExpression(), variable); } @Contract("_, null, _ -> false") @@ -374,7 +373,7 @@ public class OptionalIsPresentInspection extends BaseJavaBatchLocalInspectionToo if (falseElement != null && !(falseElement instanceof PsiEmptyStatement)) return false; if (!(trueElement instanceof PsiExpressionStatement)) return false; PsiExpression expression = ((PsiExpressionStatement)trueElement).getExpression(); - return isOptionalLambdaCandidate(optionalVariable, expression, null); + return isOptionalLambdaCandidate(optionalVariable, expression, null) && !isOptionalGetCall(expression, optionalVariable); } @Override diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeConsumerJustGet.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeConsumerJustGet.java new file mode 100644 index 000000000000..6a1188036e3e --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/optionalIsPresent/beforeConsumerJustGet.java @@ -0,0 +1,11 @@ +// "Replace Optional.isPresent() condition with functional style expression" "false" + +import java.util.*; + +public class Main { + public void testOptional(Optional str) { + if (str.isPresent()) { + str.get(); + } + } +} \ No newline at end of file