From 42b0bfe079d47d70d3e5ea5b79727ef488787f18 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Tue, 1 Mar 2016 13:00:29 +0100 Subject: [PATCH] IDEA-152373 (Optional.get without Optional.isPresent warning when check is present) --- .../bugs/OptionalGetWithoutIsPresentInspection.java | 11 +++++++++-- .../OptionalGetWithoutIsPresent.java | 10 ++++++++++ .../OptionalGetWithoutIsPresentInspectionTest.java | 3 +++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/OptionalGetWithoutIsPresentInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/OptionalGetWithoutIsPresentInspection.java index 5be18f7f4096..6c3822dc1b7d 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/OptionalGetWithoutIsPresentInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/bugs/OptionalGetWithoutIsPresentInspection.java @@ -88,13 +88,20 @@ public class OptionalGetWithoutIsPresentInspection extends BaseInspection { final PsiIfStatement ifStatement = (PsiIfStatement)sibling; final PsiExpression condition = ifStatement.getCondition(); if (condition != null) { - if (!ControlFlowUtils.statementMayCompleteNormally(ifStatement.getThenBranch())) { + final PsiElement target = optionalReference.resolve(); + if (!(target instanceof PsiVariable)) { + return true; + } + final PsiVariable variable = (PsiVariable)target; + final PsiStatement thenBranch = ifStatement.getThenBranch(); + if (!ControlFlowUtils.statementMayCompleteNormally(thenBranch) || VariableAccessUtils.variableIsAssigned(variable, thenBranch)) { checker.negate = true; if (checker.checkExpression(condition)) { return true; } } - else if (!ControlFlowUtils.statementMayCompleteNormally(ifStatement.getElseBranch())) { + final PsiStatement elseBranch = ifStatement.getElseBranch(); + if (!ControlFlowUtils.statementMayCompleteNormally(elseBranch) || VariableAccessUtils.variableIsAssigned(variable, elseBranch)) { checker.negate = false; if (checker.checkExpression(condition)) { return true; diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/optional_get_without_is_present/OptionalGetWithoutIsPresent.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/optional_get_without_is_present/OptionalGetWithoutIsPresent.java index 641210bd6a56..0c1e79aac2b9 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/optional_get_without_is_present/OptionalGetWithoutIsPresent.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/optional_get_without_is_present/OptionalGetWithoutIsPresent.java @@ -50,4 +50,14 @@ class OptionalWithoutIsPresent { org.testng.Assert.assertTrue(o3.isPresent()); System.out.println(o3.get()); } + + public static String demo() { + Optional holder = Optional.empty(); + + if (! holder.isPresent()) { + holder = Optional.of("hello world"); + } + + return holder.get(); + } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/OptionalGetWithoutIsPresentInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/OptionalGetWithoutIsPresentInspectionTest.java index 49592dc51e33..81156b9ba0f7 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/OptionalGetWithoutIsPresentInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/OptionalGetWithoutIsPresentInspectionTest.java @@ -79,6 +79,9 @@ public class OptionalGetWithoutIsPresentInspectionTest extends LightInspectionTe " public static Optional empty() {" + " return new Optional<>();" + " }" + + " public static Optional of(T value) {" + + " return new Optional<>(value);" + + " }" + "}", "package java.util;" +