From 7c0df8e26f75ea4933ab08954bb5744eb7a053b7 Mon Sep 17 00:00:00 2001 From: anna Date: Wed, 24 Apr 2013 18:39:27 +0200 Subject: [PATCH] redundant cast: do not report redundant cast if dbl boxing/unboxing is prevented (IDEA-105323) --- .../generics/BoxingInConditional/expected.xml | 9 +++++++++ .../generics/BoxingInConditional/src/Test.java | 10 ++++++++++ .../intellij/codeInspection/RedundantCast15Test.java | 2 ++ .../src/com/intellij/psi/util/RedundantCastUtil.java | 10 +++++++++- 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/BoxingInConditional/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/generics/BoxingInConditional/src/Test.java 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; }