From ceb3968ecb1f4548180ace01ef40a72789112bac Mon Sep 17 00:00:00 2001 From: anna Date: Thu, 25 Jul 2013 16:53:05 +0200 Subject: [PATCH] redundant cast: try to infer cast operand type from cast parent (fix testInferFromTypeCast) --- .../intellij/psi/util/RedundantCastUtil.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/java/java-psi-api/src/com/intellij/psi/util/RedundantCastUtil.java b/java/java-psi-api/src/com/intellij/psi/util/RedundantCastUtil.java index 655d5ac4c833..b8f693879bcc 100644 --- a/java/java-psi-api/src/com/intellij/psi/util/RedundantCastUtil.java +++ b/java/java-psi-api/src/com/intellij/psi/util/RedundantCastUtil.java @@ -21,6 +21,7 @@ import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.Ref; import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; +import com.intellij.psi.impl.source.resolve.DefaultParameterTypeInferencePolicy; import com.intellij.psi.tree.IElementType; import com.intellij.util.ArrayUtil; import com.intellij.util.IncorrectOperationException; @@ -459,7 +460,19 @@ public class RedundantCastUtil { PsiTypeElement typeElement = typeCast.getCastType(); if (typeElement == null) return; final PsiType castTo = typeElement.getType(); - final PsiType opType = typeCast.getOperand().getType(); + final PsiExpression operand = typeCast.getOperand(); + + PsiType opType = operand.getType(); + final PsiType expectedTypeByParent = PsiTypesUtil.getExpectedTypeByParent(typeCast); + if (expectedTypeByParent != null) { + final PsiDeclarationStatement declarationStatement = + (PsiDeclarationStatement)JavaPsiFacade.getElementFactory(operand.getProject()).createStatementFromText( + expectedTypeByParent.getCanonicalText() + " l = " + operand.getText() + ";", parent); + final PsiExpression initializer = ((PsiLocalVariable)declarationStatement.getDeclaredElements()[0]).getInitializer(); + LOG.assertTrue(initializer != null, operand.getText()); + opType = initializer.getType(); + } + if (opType == null) return; if (parent instanceof PsiReferenceExpression) { if (castTo instanceof PsiClassType && opType instanceof PsiPrimitiveType) return; //explicit boxing @@ -471,7 +484,7 @@ public class RedundantCastUtil { PsiClass accessClass = ((PsiClassType)opType).resolve(); if (accessClass == null) return; if (!JavaPsiFacade.getInstance(parent.getProject()).getResolveHelper().isAccessible((PsiMember)element, typeCast, accessClass)) return; - if (!isCastRedundantInRefExpression(refExpression, typeCast.getOperand())) return; + if (!isCastRedundantInRefExpression(refExpression, operand)) return; } }