java redundant cast: check for inference problems when test against expected by parent type (IDEA-215022)

GitOrigin-RevId: a33a36e76da7fc49284e93903158e9257b66a97c
This commit is contained in:
Anna Kozlova
2020-05-04 20:49:32 +00:00
committed by intellij-monorepo-bot
parent 02d7164d3b
commit 026e917bb9
3 changed files with 44 additions and 0 deletions
@@ -611,6 +611,13 @@ public class RedundantCastUtil {
LOG.assertTrue(initializer != null, operand.getText());
opType = initializer.getType();
if (initializer instanceof PsiMethodCallExpression) {
JavaResolveResult newResult = ((PsiMethodCallExpression)initializer).resolveMethodGenerics();
if (newResult instanceof MethodCandidateInfo && ((MethodCandidateInfo)newResult).getInferenceErrorMessage() != null) {
return;
}
}
if (opType != null) {
if (operand instanceof PsiConditionalExpression) {
if (!isApplicableForConditionalBranch(opType, ((PsiConditionalExpression)operand).getThenExpression())) return;
@@ -0,0 +1,36 @@
import java.util.*;
import java.util.function.Predicate;
class MyTest<A> {
private final Set<A> set;
public MyTest() {
set = (Set<A>) newSet();
}
private static <A extends Comparable<A>> Set<A> newSet() {
return null;
}
static <T> Predicate<T> eq(Predicate<? super T> predicate) {
return predicate::test;
}
static <T> void test(Predicate<? super T> predicate) {
class X {
<TT> void test2() {
Predicate<? super TT> not = (Predicate<? super TT>)eq(predicate);
}
}
}
}
class Repro {
public interface MembersInjector<T> { }
static <T> MembersInjector<T> create(Class<T> cls) { return null; }
public static void main(String[] args) {
Class<?>[] parameterTypes = new Class<?>[1];
MembersInjector<Object> injector = (MembersInjector<Object>) create(parameterTypes[0]);
}
}
@@ -47,4 +47,5 @@ public class RedundantCast18Test extends LightDaemonAnalyzerTestCase {
public void testFieldInitializer() { doTest();}
public void testDiamondWithUpperBounds() { doTest();}
public void testBinaryConversions() { doTest();}
public void testInferenceIncompatibilityWithoutCast() { doTest();}
}