mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
RedundantCastUtil: fix isCastRedundant (avoid checking another cast)
This commit is contained in:
@@ -41,7 +41,7 @@ public class RedundantCastUtil {
|
||||
return new ArrayList<>(visitor.myFoundCasts);
|
||||
}
|
||||
|
||||
public static boolean isCastRedundant (PsiTypeCastExpression typeCast) {
|
||||
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;
|
||||
@@ -49,9 +49,9 @@ public class RedundantCastUtil {
|
||||
if (parent instanceof PsiExpressionList) parent = parent.getParent();
|
||||
if (parent instanceof PsiReferenceExpression) parent = parent.getParent();
|
||||
if (parent instanceof PsiAnonymousClass) parent = parent.getParent();
|
||||
MyIsRedundantVisitor visitor = new MyIsRedundantVisitor(true);
|
||||
MyIsRedundantVisitor visitor = new MyIsRedundantVisitor();
|
||||
parent.accept(visitor);
|
||||
return visitor.isRedundant;
|
||||
return visitor.foundRedundantCast == typeCast;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -63,10 +63,6 @@ public class RedundantCastUtil {
|
||||
private static class MyCollectingVisitor extends MyIsRedundantVisitor {
|
||||
private final Set<PsiTypeCastExpression> myFoundCasts = new HashSet<>();
|
||||
|
||||
private MyCollectingVisitor() {
|
||||
super(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitClass(PsiClass aClass) {
|
||||
// avoid multiple visit
|
||||
@@ -90,24 +86,14 @@ public class RedundantCastUtil {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnsafeReturnStatementVisitor")
|
||||
private static class MyIsRedundantVisitor extends JavaRecursiveElementWalkingVisitor {
|
||||
private boolean isRedundant;
|
||||
private final boolean myRecursive;
|
||||
|
||||
private MyIsRedundantVisitor(final boolean recursive) {
|
||||
myRecursive = recursive;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitElement(final PsiElement element) {
|
||||
if (myRecursive) {
|
||||
super.visitElement(element);
|
||||
}
|
||||
}
|
||||
private PsiTypeCastExpression foundRedundantCast;
|
||||
|
||||
protected void addToResults(@NotNull PsiTypeCastExpression typeCast){
|
||||
if (!isTypeCastSemantic(typeCast)) {
|
||||
isRedundant = true;
|
||||
foundRedundantCast = typeCast;
|
||||
stopWalking();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Simplify" "true"
|
||||
class Test {
|
||||
public static void main(String[] args) {
|
||||
foo((String) null, (int) 0);
|
||||
}
|
||||
|
||||
static void foo(String s, int i) {}
|
||||
static void foo(Number n, int i) {}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Simplify" "true"
|
||||
class Test {
|
||||
public static void main(String[] args) {
|
||||
foo(false ? <caret>"" : null, (int) 0);
|
||||
}
|
||||
|
||||
static void foo(String s, int i) {}
|
||||
static void foo(Number n, int i) {}
|
||||
}
|
||||
Reference in New Issue
Block a user