mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-151898 (Optional.get without isPresent)
This commit is contained in:
+6
-4
@@ -62,16 +62,18 @@ public class OptionalGetWithoutIsPresentInspection extends BaseInspection {
|
||||
return;
|
||||
}
|
||||
final PsiExpression qualifier = ParenthesesUtils.stripParentheses(methodExpression.getQualifierExpression());
|
||||
if (!(qualifier instanceof PsiReferenceExpression)) {
|
||||
if (qualifier == null) {
|
||||
return;
|
||||
}
|
||||
final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)qualifier;
|
||||
final PsiType type = qualifier.getType();
|
||||
if (!TypeUtils.isOptional(type)) {
|
||||
return;
|
||||
}
|
||||
if (isSurroundedByIsPresentGuard(referenceExpression)) {
|
||||
return;
|
||||
if (qualifier instanceof PsiReferenceExpression) {
|
||||
final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)qualifier;
|
||||
if (isSurroundedByIsPresentGuard(referenceExpression)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
registerMethodCallError(expression, type);
|
||||
}
|
||||
|
||||
+15
@@ -43,6 +43,18 @@ public class OptionalGetWithoutIsPresentInspectionTest extends LightInspectionTe
|
||||
"}");
|
||||
}
|
||||
|
||||
public void testOptionalWithoutVariable() {
|
||||
doTest("import java.util.Optional;" +
|
||||
"class X {" +
|
||||
" {" +
|
||||
" System.out.println(getName()./*'Optional.get()' without 'isPresent()' check*/get/**/());" +
|
||||
" }" +
|
||||
" Optional<String> getName() {" +
|
||||
" return Optional.empty();" +
|
||||
" }" +
|
||||
"}");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected InspectionProfileEntry getInspection() {
|
||||
@@ -60,6 +72,9 @@ public class OptionalGetWithoutIsPresentInspectionTest extends LightInspectionTe
|
||||
" public boolean isPresent() {" +
|
||||
" return true;" +
|
||||
" }" +
|
||||
" public static<T> Optional<T> empty() {" +
|
||||
" return new Optional<>();" +
|
||||
" }" +
|
||||
"}",
|
||||
|
||||
"package java.util;" +
|
||||
|
||||
Reference in New Issue
Block a user