Commit 7906dd0d authored by Tagir Valeev's avatar Tagir Valeev
Browse files

RedundantCastUtil: fix isCastRedundant (avoid checking another cast)

parent 4d844c7a
Showing with 25 additions and 21 deletions
+25 -21
......@@ -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();
}
}
......
// "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) {}
}
\ No newline at end of file
// "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) {}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment