unchecked casts: ignore extends wildcards with the same bound as corresponding type parameter bound (IDEA-71629)

This commit is contained in:
anna
2011-07-12 17:43:36 +04:00
parent f9a3dca568
commit 49d4914f57
2 changed files with 27 additions and 3 deletions
@@ -55,4 +55,18 @@ class M {
public static <T extends K> L f(T t) {
return (L) t; //this should NOT generate unchecked cast
}
}
}
class UncheckedCastFalsePositive {
public static void method(Object something) {
if (something instanceof NumberList) {
NumberList<? extends Number> <warning descr="Variable 'numberList' is never used">numberList</warning> = (NumberList<? extends Number>) something;
}
}
public static class NumberList<E extends Number> extends ArrayList<E> {
}
}
@@ -119,7 +119,7 @@ public abstract class PsiClassType extends PsiType {
}
/**
* Checks if the class type has any parameters which are not unbounded wildcards
* Checks if the class type has any parameters which are not unbounded wildcards (and not extends-wildcard with the same bound as corresponding type parameter bound)
* and do not have substituted arguments.
*
* @return true if the class type has nontrivial non-substituted parameters, false otherwise
@@ -131,7 +131,17 @@ public abstract class PsiClassType extends PsiType {
for (PsiTypeParameter parameter : PsiUtil.typeParametersIterable(aClass)) {
PsiType type = resolveResult.getSubstitutor().substitute(parameter);
if (type != null) {
if (!(type instanceof PsiWildcardType) || ((PsiWildcardType)type).getBound() != null) {
if (!(type instanceof PsiWildcardType)) {
return true;
}
final PsiType bound = ((PsiWildcardType)type).getBound();
if (bound != null) {
if (((PsiWildcardType)type).isExtends()) {
final PsiClass superClass = parameter.getSuperClass();
if (superClass != null && PsiUtil.resolveClassInType(bound) == superClass) {
continue;
}
}
return true;
}
}