mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-177306 False Positive: reporting "useless" Optional map
This commit is contained in:
@@ -30,6 +30,7 @@ import com.siyeh.ig.psiutils.MethodCallUtils;
|
||||
import com.siyeh.ig.psiutils.StreamApiUtil;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author Tagir Valeev
|
||||
@@ -108,10 +109,14 @@ public class OptionalUtil {
|
||||
* @return an expression text which will unwrap an {@code Optional}.
|
||||
*/
|
||||
public static String generateOptionalUnwrap(String qualifier, PsiVariable var,
|
||||
PsiExpression trueExpression, PsiExpression falseExpression,
|
||||
PsiType targetType, boolean useOrElseGet) {
|
||||
PsiExpression trueExpression, PsiExpression falseExpression,
|
||||
@Nullable PsiType targetType, boolean useOrElseGet) {
|
||||
PsiExpression stripped = PsiUtil.skipParenthesizedExprDown(trueExpression);
|
||||
if (!ExpressionUtils.isReferenceTo(trueExpression, var)) {
|
||||
boolean trivialMap = ExpressionUtils.isReferenceTo(trueExpression, var) &&
|
||||
targetType != null &&
|
||||
trueExpression.getType() != null &&
|
||||
trueExpression.getType().isAssignableFrom(targetType);
|
||||
if (!trivialMap) {
|
||||
if (stripped instanceof PsiTypeCastExpression && ExpressionUtils.isNullLiteral(falseExpression)) {
|
||||
PsiTypeCastExpression castExpression = (PsiTypeCastExpression)stripped;
|
||||
PsiTypeElement castType = castExpression.getCastType();
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Fix all 'Simplify Optional call chains' problems in file" "false"
|
||||
import java.util.Optional;
|
||||
|
||||
public class Tests {
|
||||
static <T> void test(Optional<? extends T> opt, T defaultT) {
|
||||
T t = opt.<T>map(x -> x).orEl<caret>se(defaultT);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Fix all 'Simplify Optional call chains' problems in file" "false"
|
||||
import java.util.Optional;
|
||||
|
||||
public class Tests {
|
||||
static void test(Optional<Double> opt, Integer defaultT) {
|
||||
Number t = opt.<Number>map(x -> x).orEl<caret>se(defaultT);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user