From 04b43842f951ff433ae015fec1b6b90333fc57d1 Mon Sep 17 00:00:00 2001 From: Eugene Vigdorchik Date: Sat, 5 Mar 2005 21:19:37 +0300 Subject: [PATCH] IDEADEV-522 --- .../redundantCast/RedundantCastUtil.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/source/com/intellij/codeInspection/redundantCast/RedundantCastUtil.java b/source/com/intellij/codeInspection/redundantCast/RedundantCastUtil.java index 878cc7dd98e5..9c7eafde4369 100644 --- a/source/com/intellij/codeInspection/redundantCast/RedundantCastUtil.java +++ b/source/com/intellij/codeInspection/redundantCast/RedundantCastUtil.java @@ -12,6 +12,7 @@ import com.intellij.psi.*; import com.intellij.psi.search.PsiBaseElementProcessor; import com.intellij.psi.search.PsiElementProcessor; import com.intellij.util.IncorrectOperationException; +import com.intellij.openapi.diagnostic.Logger; import java.util.ArrayList; import java.util.HashSet; @@ -19,6 +20,7 @@ import java.util.List; import java.util.Set; public class RedundantCastUtil { + private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.redundantCast.RedundantCastUtil"); public static List getRedundantCasts(PsiElement where) { final ArrayList result = new ArrayList(); PsiElementProcessor processor = new PsiBaseElementProcessor() { @@ -47,10 +49,6 @@ public class RedundantCastUtil { } } - public void visitReferenceExpression(PsiReferenceExpression expression) { - visitElement(expression); - } - public void visitConditionalExpression(PsiConditionalExpression expression) { // Do not go inside conditional expression because branches are required to be exactly the same type, not assignable. } @@ -296,14 +294,15 @@ public class RedundantCastUtil { if (fromType == null || toType == null) return; if (parent instanceof PsiReferenceExpression) { if (toType instanceof PsiClassType && fromType instanceof PsiPrimitiveType) return; //explicit boxing - //Check accessibility if (fromType instanceof PsiClassType) { - PsiElement element = ((PsiReferenceExpression)parent).resolve(); + final PsiReferenceExpression refExpression = ((PsiReferenceExpression)parent); + PsiElement element = refExpression.resolve(); if (!(element instanceof PsiMember)) return; PsiClass accessClass = ((PsiClassType)fromType).resolve(); if (accessClass == null) return; if (!parent.getManager().getResolveHelper().isAccessible((PsiMember)element, typeCast, accessClass)) return; + if (!isCastRedundantInRefExpression(refExpression, typeCast.getOperand())) return; } } @@ -313,6 +312,21 @@ public class RedundantCastUtil { } } + private static boolean isCastRedundantInRefExpression (PsiReferenceExpression refExpression, PsiExpression castOperand) { + PsiElement resolved = refExpression.resolve(); + final PsiReferenceExpression copy = (PsiReferenceExpression)refExpression.copy(); + try { + copy.getQualifierExpression().replace(castOperand); + if (copy.resolve() != resolved) return false; + } + catch (IncorrectOperationException e) { + LOG.error(e); + return false; + } + + return true; + } + public static boolean isTypeCastSemantical(PsiTypeCastExpression typeCast) { PsiExpression operand = typeCast.getOperand(); if (operand != null) {