redundant cast: open capture to compare old and new result types

This commit is contained in:
Anna Kozlova
2016-03-11 10:51:37 +01:00
parent 320904a20f
commit 23df4bf2a1
3 changed files with 22 additions and 3 deletions
@@ -281,10 +281,13 @@ public class RedundantCastUtil {
final JavaResolveResult newResult = newCall.getMethodExpression().advancedResolve(false);
if (!newResult.isValidResult()) return;
final PsiMethod newTargetMethod = (PsiMethod)newResult.getElement();
final PsiType newReturnType = newCall.getType();
final PsiType oldReturnType = methodCall.getType();
PsiType newReturnType = newCall.getType(), oldReturnType = methodCall.getType();
if (newReturnType instanceof PsiCapturedWildcardType && oldReturnType instanceof PsiCapturedWildcardType) {
newReturnType = ((PsiCapturedWildcardType)newReturnType).getUpperBound();
oldReturnType = ((PsiCapturedWildcardType)oldReturnType).getUpperBound();
}
if (Comparing.equal(newReturnType, oldReturnType)) {
if (newTargetMethod.equals(targetMethod) ||
if (Comparing.equal(newTargetMethod, targetMethod) ||
(newTargetMethod.getSignature(newResult.getSubstitutor()).equals(targetMethod.getSignature(resolveResult.getSubstitutor())) &&
!(newTargetMethod.isDeprecated() && !targetMethod.isDeprecated()) && // see SCR11555, SCR14559
areThrownExceptionsCompatible(targetMethod, newTargetMethod))) {
@@ -0,0 +1,11 @@
interface Pair<A extends String> {
A get();
}
class B {
void m(final Pair<?> p) {
String v = ((<warning descr="Casting 'p' to 'Pair<?>' is redundant">Pair<?></warning>) p).get();
}
}
@@ -56,6 +56,11 @@ public class LambdaRedundantCastTest extends LightDaemonAnalyzerTestCase {
doTest();
}
public void testCastInMethodCallQualifierWithWildcardReturn() throws Exception {
doTest();
}
private void doTest() {
doTest(BASE_PATH + "/" + getTestName(false) + ".java", true, false);
}