mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-153817 ('Optional.get() without isPresent() check' inspection should support while statement)
This commit is contained in:
+16
-3
@@ -109,6 +109,14 @@ public class OptionalGetWithoutIsPresentInspection extends BaseInspection {
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (sibling instanceof PsiWhileStatement) {
|
||||
final PsiWhileStatement whileStatement = (PsiWhileStatement)sibling;
|
||||
final PsiExpression condition = whileStatement.getCondition();
|
||||
checker.negate = true;
|
||||
if (checker.checkExpression(condition)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (sibling instanceof PsiAssertStatement) {
|
||||
final PsiAssertStatement assertStatement = (PsiAssertStatement)sibling;
|
||||
final PsiExpression condition = assertStatement.getAssertCondition();
|
||||
@@ -140,15 +148,15 @@ public class OptionalGetWithoutIsPresentInspection extends BaseInspection {
|
||||
sibling = PsiTreeUtil.getPrevSiblingOfType(sibling, PsiStatement.class);
|
||||
}
|
||||
checker.negate = false;
|
||||
PsiElement parent = PsiTreeUtil.getParentOfType(context, PsiIfStatement.class, PsiConditionalExpression.class,
|
||||
PsiElement parent = PsiTreeUtil.getParentOfType(context, PsiIfStatement.class, PsiWhileStatement.class, PsiConditionalExpression.class,
|
||||
PsiPolyadicExpression.class);
|
||||
while (parent != null) {
|
||||
parent.accept(checker);
|
||||
if (checker.hasIsPresentCall()) {
|
||||
return true;
|
||||
}
|
||||
parent = PsiTreeUtil.getParentOfType(parent, PsiPolyadicExpression.class, PsiIfStatement.class,
|
||||
PsiConditionalExpression.class);
|
||||
parent = PsiTreeUtil.getParentOfType(parent, PsiIfStatement.class, PsiWhileStatement.class, PsiConditionalExpression.class,
|
||||
PsiPolyadicExpression.class);
|
||||
}
|
||||
return checker.hasIsPresentCall();
|
||||
}
|
||||
@@ -188,6 +196,11 @@ public class OptionalGetWithoutIsPresentInspection extends BaseInspection {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitWhileStatement(PsiWhileStatement statement) {
|
||||
checkExpression(statement.getCondition());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitIfStatement(PsiIfStatement ifStatement) {
|
||||
final PsiStatement elseBranch = ifStatement.getElseBranch();
|
||||
|
||||
+25
-1
@@ -17,7 +17,6 @@ package com.siyeh.ig.bugs;
|
||||
|
||||
import com.intellij.codeInspection.InspectionProfileEntry;
|
||||
import com.siyeh.ig.LightInspectionTestCase;
|
||||
import junit.framework.TestCase;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
@@ -55,6 +54,31 @@ public class OptionalGetWithoutIsPresentInspectionTest extends LightInspectionTe
|
||||
"}");
|
||||
}
|
||||
|
||||
public void testWhile1() {
|
||||
doTest("import java.util.Optional;" +
|
||||
"class X {" +
|
||||
" void m() {" +
|
||||
" Optional<String> o = Optional.empty();" +
|
||||
" while (!o.isPresent()){" +
|
||||
" o = Optional.of(\"\");" +
|
||||
" }" +
|
||||
" o.get();" +
|
||||
" }" +
|
||||
"}");
|
||||
}
|
||||
|
||||
public void testWhile2() {
|
||||
doTest("import java.util.Optional;" +
|
||||
"class X {" +
|
||||
" void m() {" +
|
||||
" Optional<String> o = Optional.empty();" +
|
||||
" while (o.isPresent()) {" +
|
||||
" o.get();" +
|
||||
" }" +
|
||||
" }" +
|
||||
"}");
|
||||
}
|
||||
|
||||
public void testOptionalGetWithoutIsPresent() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user