accept casts as unchecked from generic types with type parameter with upper bounds as type argument: check parameter bounds first

This commit is contained in:
Anna Kozlova
2015-05-12 17:07:05 +02:00
parent 1dd5090df8
commit 1d6a1bf27e
3 changed files with 27 additions and 10 deletions
@@ -101,6 +101,18 @@ public class TypesDistinctProver {
final PsiClassType.ClassResolveResult classResolveResult1 = PsiUtil.resolveGenericsClassInType(type1);
final PsiClassType.ClassResolveResult classResolveResult2 = PsiUtil.resolveGenericsClassInType(type2);
final PsiClass boundClass1 = classResolveResult1.getElement();
final PsiClass boundClass2 = classResolveResult2.getElement();
if (boundClass1 instanceof PsiTypeParameter && level < 2) {
if (!distinguishFromTypeParam((PsiTypeParameter)boundClass1, boundClass2, type1)) return false;
}
if (boundClass2 instanceof PsiTypeParameter && level < 2) {
if (!distinguishFromTypeParam((PsiTypeParameter)boundClass2, boundClass1, type2)) return false;
}
if (Comparing.equal(TypeConversionUtil.erasure(type1), TypeConversionUtil.erasure(type2))) {
final PsiSubstitutor substitutor1 = classResolveResult1.getSubstitutor();
final PsiSubstitutor substitutor2 = classResolveResult2.getSubstitutor();
@@ -124,16 +136,6 @@ public class TypesDistinctProver {
if (level < 2) return false;
}
final PsiClass boundClass1 = classResolveResult1.getElement();
final PsiClass boundClass2 = classResolveResult2.getElement();
if (boundClass1 instanceof PsiTypeParameter && level < 2) {
if (!distinguishFromTypeParam((PsiTypeParameter)boundClass1, boundClass2, type1)) return false;
}
if (boundClass2 instanceof PsiTypeParameter && level < 2) {
if (!distinguishFromTypeParam((PsiTypeParameter)boundClass2, boundClass1, type2)) return false;
}
return type2 != null && type1 != null && !type1.equals(type2) &&
(!InheritanceUtil.isInheritorOrSelf(boundClass1, boundClass2, true) ||
!InheritanceUtil.isInheritorOrSelf(boundClass2, boundClass1, true));
@@ -0,0 +1,11 @@
import java.util.Map;
class Test {
public static <M extends Map<String, Integer>> void groupingBy(Supplier<M> mapFactory) {
Supplier<Map<String, Integer>> mangledFactory = <warning descr="Unchecked cast: 'Test.Supplier<M>' to 'Test.Supplier<java.util.Map<java.lang.String,java.lang.Integer>>'">(Supplier<Map<String, Integer>>) mapFactory</warning>;
System.out.println(mangledFactory);
}
interface Supplier<<warning descr="Type parameter 'G' is never used">G</warning>> {}
}
@@ -484,6 +484,10 @@ public class GenericsHighlightingTest extends LightDaemonAnalyzerTestCase {
doTest(LanguageLevel.JDK_1_7, JavaSdkVersion.JDK_1_7, false);
}
public void testCastFromGenericTypeWithTypeParameterWithExtendsAsArgument() throws Exception {
doTest(LanguageLevel.JDK_1_7, JavaSdkVersion.JDK_1_7, true);
}
public void testJavaUtilCollections_NoVerify() throws Exception {
PsiClass collectionsClass = getJavaFacade().findClass("java.util.Collections", GlobalSearchScope.moduleWithLibrariesScope(getModule()));
assertNotNull(collectionsClass);