cleanup anonymous type when asked for variable type

check that effective cast type will help (IDEA-190473)
This commit is contained in:
Anna.Kozlova
2018-04-19 13:23:22 +02:00
parent bce05603fa
commit 300ba0e9bb
5 changed files with 27 additions and 5 deletions
@@ -385,7 +385,8 @@ public class HighlightUtil extends HighlightUtilBase {
}
if (expression == null) return false;
PsiType rType = expression.getType();
return rType != null && toType != null && TypeConversionUtil.areTypesConvertible(rType, toType);
PsiType castType = GenericsUtil.getVariableTypeByExpressionType(toType);
return rType != null && toType != null && TypeConversionUtil.areTypesConvertible(rType, toType) && toType.isAssignableFrom(castType);
}
@@ -303,6 +303,10 @@ public class GenericsUtil {
@Contract("null, _ -> null")
public static PsiType getVariableTypeByExpressionType(@Nullable PsiType type, final boolean openCaptured) {
if (type == null) return null;
PsiClass refClass = PsiUtil.resolveClassInType(type);
if (refClass instanceof PsiAnonymousClass) {
type = ((PsiAnonymousClass)refClass).getBaseClassType();
}
if (type instanceof PsiCapturedWildcardType) {
type = ((PsiCapturedWildcardType)type).getUpperBound();
}
@@ -46,10 +46,6 @@ public class RefactoringChangeUtil {
}
return null;
}
PsiClass refClass = PsiUtil.resolveClassInType(type);
if (refClass instanceof PsiAnonymousClass) {
type = ((PsiAnonymousClass)refClass).getBaseClassType();
}
return GenericsUtil.getVariableTypeByExpressionType(type);
}
@@ -0,0 +1,11 @@
// "Cast to 'java.lang.Object'" "false"
class Scope<T> {
T val;
void f() {
var y = new Object();
var x = new Object() {
int a = 12;
};
x =<caret> val;
}
}
@@ -0,0 +1,10 @@
// "Cast to 'java.lang.Object'" "false"
class A {
void f() {
var y = new Object();
var x = new Object() {
int a = 12;
};
x = <caret>y;
}
}