mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
redundant cast: don't mark casts inside array index access expressions acceptable (IDEA-153661)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user