diff --git a/java/java-tests/testData/inspection/redundantCast/generics/BoxingInConditional/expected.xml b/java/java-tests/testData/inspection/redundantCast/generics/BoxingInConditional/expected.xml new file mode 100644 index 000000000000..3bf1252e596b --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/BoxingInConditional/expected.xml @@ -0,0 +1,9 @@ + + + + Test.java + 8 + Redundant type cast + Casting <code>2</code> to <code>Integer</code> is redundant + + diff --git a/java/java-tests/testData/inspection/redundantCast/generics/BoxingInConditional/src/Test.java b/java/java-tests/testData/inspection/redundantCast/generics/BoxingInConditional/src/Test.java new file mode 100644 index 000000000000..995072a79416 --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/generics/BoxingInConditional/src/Test.java @@ -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; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast15Test.java b/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast15Test.java index db20eca0956a..0fe49b1a5435 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast15Test.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast15Test.java @@ -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(); } diff --git a/java/openapi/src/com/intellij/psi/util/RedundantCastUtil.java b/java/openapi/src/com/intellij/psi/util/RedundantCastUtil.java index 32021974bbe1..2852584db636 100644 --- a/java/openapi/src/com/intellij/psi/util/RedundantCastUtil.java +++ b/java/openapi/src/com/intellij/psi/util/RedundantCastUtil.java @@ -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; }