Skip to content

Commit

Permalink
Remove unused import in RemoveRedundantTypeCast
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Dec 19, 2023
1 parent aabf288 commit 4348159
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public J visitTypeCast(J.TypeCast typeCast, ExecutionContext ctx) {
} else if (!(targetType instanceof JavaType.Array) && TypeUtils.isOfClassType(targetType, "java.lang.Object") ||
TypeUtils.isOfType(targetType, expressionType) ||
TypeUtils.isAssignableTo(targetType, expressionType)) {
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(castType);
if (fullyQualified != null) {
maybeRemoveImport(fullyQualified.getFullyQualifiedName());
}
return visitedTypeCast.getExpression().withPrefix(visitedTypeCast.getPrefix());
}
return visitedTypeCast;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,31 @@ void method() {
)
);
}

@Test
void removeImport() {
rewriteRun(
java(
"""
import java.util.ArrayList;
import java.util.List;
class Test {
List method(List list) {
return (ArrayList) list;
}
}
""",
"""
import java.util.List;
class Test {
List method(List list) {
return list;
}
}
"""
)
);
}
}

0 comments on commit 4348159

Please sign in to comment.