redundant cast: process instanceof (IDEA-128493)

This commit is contained in:
Anna Kozlova
2014-08-12 19:35:38 +04:00
parent 598c572c83
commit 328808bebb
3 changed files with 25 additions and 2 deletions
@@ -541,8 +541,17 @@ public class RedundantCastUtil {
}
}
}
if (parent instanceof PsiInstanceOfExpression || (TypeConversionUtil.isAssignable(castTo, opType, false) &&
(expectedTypeByParent == null || TypeConversionUtil.isAssignable(expectedTypeByParent, opType, false)))) {
if (parent instanceof PsiInstanceOfExpression) {
//15.20.2. Type Comparison Operator instanceof:
//If a cast (§15.16) of the RelationalExpression to the ReferenceType would be rejected as a compile-time error,
//then the instanceof relational expression likewise produces a compile-time error.
final PsiTypeElement checkTypeElement = ((PsiInstanceOfExpression)parent).getCheckType();
if (checkTypeElement != null && TypeConversionUtil.areTypesConvertible(opType, checkTypeElement.getType())) {
addToResults(typeCast);
}
}
else if (TypeConversionUtil.isAssignable(castTo, opType, false) &&
(expectedTypeByParent == null || TypeConversionUtil.isAssignable(expectedTypeByParent, opType, false))) {
addToResults(typeCast);
}
}
@@ -0,0 +1,13 @@
@SuppressWarnings({"UnusedDeclaration"})
class C {
boolean foo(final ConfigurableField<String> nameField) {
return (Formatter<?>)nameField.getFormatter() instanceof DefaultFormatter;
}
}
@SuppressWarnings({"UnusedDeclaration"})
interface Formatter<V>{}
class DefaultFormatter implements Formatter<Object>{}
interface ConfigurableField<V> {
Formatter<V> getFormatter();
}
@@ -145,6 +145,7 @@ public class LightAdvHighlightingJdk7Test extends LightDaemonAnalyzerTestCase {
public void testPreciseRethrow() { doTest(false, false); }
public void testImprovedCatchAnalysis() { doTest(true, false); }
public void testPolymorphicTypeCast() { doTest(true, false); }
public void testTypeCastInInstanceof() { doTest(true, false); }
public void testErasureClashConfusion() { doTest(true, false, UnusedDeclarationInspection.class); }
public void testUnused() { doTest(true, false, UnusedDeclarationInspection.class); }
public void testSuperBound() { doTest(false, false); }