redundant cast: do not report redundant cast if dbl boxing/unboxing is prevented (IDEA-105323)

This commit is contained in:
anna
2013-04-24 19:16:27 +02:00
parent 0dc7262c45
commit 7c0df8e26f
4 changed files with 30 additions and 1 deletions
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>Test.java</file>
<line>8</line>
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Redundant type cast</problem_class>
<description>Casting &lt;code&gt;2&lt;/code&gt; to &lt;code&gt;Integer&lt;/code&gt; is redundant</description>
</problem>
</problems>
@@ -0,0 +1,10 @@
//This is a test for JDK_15 LanguageLevel
public class Test {
private Integer foo(String s, Integer i) {
return s == null ? i : (Integer)2;
}
private int foo1(String s, Integer i) {
return s == null ? i : (Integer)2;
}
}
@@ -24,6 +24,8 @@ public class RedundantCast15Test extends InspectionTestCase {
public void testBoxingInRef() throws Exception { doTest(); }
public void testBoxingInConditional() throws Exception { doTest(); }
public void testInference1() throws Exception { doTest(); }
public void testInference2() throws Exception { doTest(); }
@@ -383,7 +383,9 @@ public class RedundantCastUtil {
PsiElement parent = typeCast.getParent();
if (parent instanceof PsiConditionalExpression) {
//branches need to be of the same type
if (!Comparing.equal(operand.getType(), ((PsiConditionalExpression)parent).getType())) {
final PsiType operandType = operand.getType();
final PsiType conditionalType = ((PsiConditionalExpression)parent).getType();
if (!Comparing.equal(operandType, conditionalType)) {
if (!PsiUtil.isLanguageLevel5OrHigher(typeCast)) {
return;
}
@@ -578,6 +580,12 @@ public class RedundantCastUtil {
if (firstOperand != null && otherOperand != null && wrapperCastChangeSemantics(firstOperand, otherOperand, operand)) {
return true;
}
} else if (parent instanceof PsiConditionalExpression) {
if (opType instanceof PsiPrimitiveType && !(((PsiConditionalExpression)parent).getType() instanceof PsiPrimitiveType)) {
if (PsiPrimitiveType.getUnboxedType(PsiTypesUtil.getExpectedTypeByParent((PsiExpression)parent)) != null) {
return true;
}
}
}
return false;
}