mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 13:31:28 +07:00
[java-intentions] Do not suggest to add a cast to new expression (except) upcast
Part of IDEA-356767 GitOrigin-RevId: e9f9e6c1a46ade85c14f20811444299e8aff0c17
This commit is contained in:
committed by
intellij-monorepo-bot
parent
d2cf90784f
commit
1fe9c2e631
@@ -234,7 +234,7 @@ final class AdaptExpressionTypeFixUtil {
|
||||
info.registerFix(action2, null, null, null, null);
|
||||
var action1 = new WrapExpressionFix(expectedType, expression, role);
|
||||
info.registerFix(action1, null, null, null, null);
|
||||
PsiType castToType = suggestCastTo(expectedType, actualType);
|
||||
PsiType castToType = suggestCastTo(expression, expectedType, actualType);
|
||||
if (castToType != null) {
|
||||
ModCommandAction action = new AddTypeCastFix(castToType, expression, role);
|
||||
info.registerFix(action, null, null, null, null);
|
||||
@@ -413,10 +413,13 @@ final class AdaptExpressionTypeFixUtil {
|
||||
return tryCast(((PsiClassType)parameter).resolve(), PsiTypeParameter.class);
|
||||
}
|
||||
|
||||
static @Nullable PsiType suggestCastTo(@Nullable PsiType expectedTypeByParent, @Nullable PsiType actualType) {
|
||||
static @Nullable PsiType suggestCastTo(@NotNull PsiExpression expression,
|
||||
@Nullable PsiType expectedTypeByParent, @Nullable PsiType actualType) {
|
||||
if (expectedTypeByParent == null || actualType == null) return null;
|
||||
if (TypeConversionUtil.isAssignable(expectedTypeByParent, actualType)) return null;
|
||||
if (TypeConversionUtil.areTypesConvertible(actualType, expectedTypeByParent)) return expectedTypeByParent;
|
||||
boolean convertible = expression instanceof PsiNewExpression ? expectedTypeByParent.isAssignableFrom(actualType) :
|
||||
TypeConversionUtil.areTypesConvertible(actualType, expectedTypeByParent);
|
||||
if (convertible) return expectedTypeByParent;
|
||||
if (actualType instanceof PsiPrimitiveType) {
|
||||
PsiPrimitiveType unboxedType = PsiPrimitiveType.getUnboxedType(expectedTypeByParent);
|
||||
if (unboxedType != null && TypeConversionUtil.areTypesConvertible(actualType, unboxedType)) {
|
||||
|
||||
@@ -465,7 +465,9 @@ public final class HighlightUtil {
|
||||
if (expression == null) return false;
|
||||
PsiType rType = expression.getType();
|
||||
PsiType castType = GenericsUtil.getVariableTypeByExpressionType(toType);
|
||||
return rType != null && toType != null && TypeConversionUtil.areTypesConvertible(rType, toType) && toType.isAssignableFrom(castType);
|
||||
if (rType == null || toType == null) return false;
|
||||
boolean convertible = expression instanceof PsiNewExpression ? toType.isAssignableFrom(rType) : toType.isConvertibleFrom(rType);
|
||||
return convertible && toType.isAssignableFrom(castType);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Cast expression to 'Scratch.Y'" "false"
|
||||
class Scratch {
|
||||
|
||||
public static class X { }
|
||||
|
||||
public static class Y extends X { }
|
||||
|
||||
public static void main(String[] args) {
|
||||
Y y = new <caret>X();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user