mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
redundant cast: do not report redundant cast if dbl boxing/unboxing is prevented (IDEA-105323)
This commit is contained in:
+9
@@ -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 <code>2</code> to <code>Integer</code> is redundant</description>
|
||||
</problem>
|
||||
</problems>
|
||||
+10
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user