redundant cast: don't mark casts inside array index access expressions acceptable (IDEA-153661)

This commit is contained in:
Anna.Kozlova
2016-04-01 11:38:39 +02:00
parent 1449ccff22
commit f8e479a020
3 changed files with 18 additions and 4 deletions
@@ -642,7 +642,7 @@ public class RedundantCastUtil {
}
}
if (arrayAccessAtTheLeftSideOfAssignment(parent)) {
if (arrayAccessAtTheLeftSideOfAssignment(parent, typeCast)) {
if (TypeConversionUtil.isAssignable(opType, castTo, false) && opType.getArrayDimensions() == castTo.getArrayDimensions()) {
addToResults(typeCast);
}
@@ -715,11 +715,13 @@ public class RedundantCastUtil {
return true;
}
private static boolean arrayAccessAtTheLeftSideOfAssignment(PsiElement element) {
PsiAssignmentExpression assignment = PsiTreeUtil.getParentOfType(element, PsiAssignmentExpression.class, false, PsiMember.class);
private static boolean arrayAccessAtTheLeftSideOfAssignment(PsiElement parent, PsiElement element) {
PsiAssignmentExpression assignment = PsiTreeUtil.getParentOfType(parent, PsiAssignmentExpression.class, false, PsiMember.class);
if (assignment == null) return false;
PsiExpression lExpression = assignment.getLExpression();
return PsiTreeUtil.isAncestor(lExpression, element, false) && lExpression instanceof PsiArrayAccessExpression;
return lExpression instanceof PsiArrayAccessExpression &&
PsiTreeUtil.isAncestor(lExpression, parent, false) &&
!PsiTreeUtil.isAncestor(((PsiArrayAccessExpression)lExpression).getIndexExpression(), element, false);
}
}
@@ -5,5 +5,10 @@
<line>3</line>
<description>Casting 'array' to String[] is redundant</description>
</problem>
<problem>
<file>A.java</file>
<line>20</line>
<description>Casting '1' to 'Integer' is redundant</description>
</problem>
</problems>
@@ -12,4 +12,11 @@ public class Test {
t.obj = new Object[1];
((Object[])t.obj)[0] = val;
}
public static void arrayDims() {
int[] ints = new int[] {};
Object foo = 1;
ints[(Integer) foo] = 0;
ints[(Integer) 1] = 0;
}
}