mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-152373 (Optional.get without Optional.isPresent warning when check is present)
This commit is contained in:
+9
-2
@@ -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;
|
||||
|
||||
+10
@@ -50,4 +50,14 @@ class OptionalWithoutIsPresent {
|
||||
org.testng.Assert.assertTrue(o3.isPresent());
|
||||
System.out.println(o3.get());
|
||||
}
|
||||
|
||||
public static String demo() {
|
||||
Optional<String> holder = Optional.empty();
|
||||
|
||||
if (! holder.isPresent()) {
|
||||
holder = Optional.of("hello world");
|
||||
}
|
||||
|
||||
return holder.get();
|
||||
}
|
||||
}
|
||||
+3
@@ -79,6 +79,9 @@ public class OptionalGetWithoutIsPresentInspectionTest extends LightInspectionTe
|
||||
" public static<T> Optional<T> empty() {" +
|
||||
" return new Optional<>();" +
|
||||
" }" +
|
||||
" public static <T> Optional<T> of(T value) {" +
|
||||
" return new Optional<>(value);" +
|
||||
" }" +
|
||||
"}",
|
||||
|
||||
"package java.util;" +
|
||||
|
||||
Reference in New Issue
Block a user