mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
redundant cast: process instanceof (IDEA-128493)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+13
@@ -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();
|
||||
}
|
||||
+1
@@ -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); }
|
||||
|
||||
Reference in New Issue
Block a user