redundant cast: check binary comparison assignability (IDEA-209944)

GitOrigin-RevId: 4276128fb33224de60afc7661e478ca172205f51
This commit is contained in:
Anna Kozlova
2019-04-28 19:24:39 +03:00
committed by intellij-monorepo-bot
parent 2ceebfedf1
commit 7f603ae6b1
3 changed files with 18 additions and 0 deletions
@@ -697,6 +697,14 @@ public class RedundantCastUtil {
addToResults(typeCast);
}
}
else if (parent instanceof PsiBinaryExpression) {
PsiExpression lOperand = ((PsiBinaryExpression)parent).getLOperand();
PsiExpression rOperand = ((PsiBinaryExpression)parent).getROperand();
PsiType oppositeType = lOperand == typeCast ? rOperand != null ? rOperand.getType() : null : lOperand.getType();
if (oppositeType != null && TypeConversionUtil.areTypesConvertible(opType, oppositeType)) {
addToResults(typeCast);
}
}
else if (TypeConversionUtil.isAssignable(castTo, opType, false) &&
(expectedTypeByParent == null || TypeConversionUtil.isAssignable(expectedTypeByParent, opType, false))) {
addToResults(typeCast);
@@ -0,0 +1,9 @@
class MyClass {
public <O> Class<O> getValueClass() {
return null;
}
public boolean isBooleanClass() {
return (Class<?>) getValueClass() == Boolean.class;
}
}
@@ -55,6 +55,7 @@ public class RedundantCast15Test extends LightCodeInsightFixtureTestCase {
public void testForEachValueIDEA126166() { doTest();}
public void testCaseThrowable() { doTest();}
public void testSafeTempVarName() { doTest();}
public void testBinaryComparison() { doTest();}
public void testTypeParameterAccessChecksJava7() {
doTest();