RedundantCastUtil#isCastRedundant: skip further checks if cast operand type equals to cast type

Fixes IDEA-194278 "Too weak variable type leads to unnecessary cast" does not detect some cases
This commit is contained in:
Tagir Valeev
2018-06-22 09:56:12 +07:00
parent 72289ad90e
commit 7bc6c05c41
3 changed files with 20 additions and 0 deletions
@@ -46,6 +46,8 @@ public class RedundantCastUtil {
public static boolean isCastRedundant (PsiTypeCastExpression typeCast) {
PsiElement parent = typeCast.getParent();
PsiExpression operand = typeCast.getOperand();
if (operand != null && operand.getType() != null && operand.getType().equals(typeCast.getType())) return true;
while(parent instanceof PsiParenthesizedExpression) parent = parent.getParent();
if (parent instanceof PsiExpressionList) parent = parent.getParent();
if (parent instanceof PsiReferenceExpression) parent = parent.getParent();
@@ -0,0 +1,9 @@
// "Change type of 'list' to 'ArrayList<String>' and remove cast" "true"
import java.util.*;
class Test {
void test() {
ArrayList<String> list = new ArrayList<>();
list.ensureCapacity();
}
}
@@ -0,0 +1,9 @@
// "Change type of 'list' to 'ArrayList<String>' and remove cast" "true"
import java.util.*;
class Test {
void test() {
List<String> list = new ArrayList<>();
((ArrayList<caret><String>) list).ensureCapacity();
}
}