Skip to content

Commit

Permalink
Update unnecessary_cast.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Centri3 committed Jul 19, 2023
1 parent dcfc6a2 commit e02f7cd
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions clippy_lints/src/casts/unnecessary_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ pub(super) fn check<'tcx>(
}
}

// skip cast of fn call that returns type alias
if let ExprKind::Cast(inner, ..) = expr.kind && is_cast_from_ty_alias(cx, inner, cast_from) {
return false;
}

// skip cast to non-primitive type
if_chain! {
if let ExprKind::Cast(_, cast_to) = expr.kind;
Expand All @@ -101,6 +96,11 @@ pub(super) fn check<'tcx>(
}
}

// skip cast of fn call that returns type alias
if let ExprKind::Cast(inner, ..) = expr.kind && is_cast_from_ty_alias(cx, inner, cast_from) {
return false;
}

if let Some(lit) = get_numeric_literal(cast_expr) {
let literal_str = &cast_str;

Expand Down Expand Up @@ -259,16 +259,7 @@ fn is_cast_from_ty_alias<'tcx>(cx: &LateContext<'tcx>, expr: impl Visitable<'tcx
if !snippet
.split("->")
.skip(1)
.map(|s| {
s.trim() == cast_from.to_string()
|| s.trim().contains(&format!("::{cast_from}"))
|| s.split("where").any(|ty| {
ty.trim() == cast_from.to_string()
|| ty.trim() == cast_from.to_string()
// Fully qualified path, or something silly like `::u32`
|| s.trim().contains(&format!("::{cast_from}"))
})
})
.map(|s| snippet_eq_ty(s, cast_from) || s.split("where").any(|ty| snippet_eq_ty(ty, cast_from)))
.any(|a| a)
{
return ControlFlow::Break(());
Expand All @@ -295,3 +286,7 @@ fn is_cast_from_ty_alias<'tcx>(cx: &LateContext<'tcx>, expr: impl Visitable<'tcx
})
.is_some()
}

fn snippet_eq_ty(snippet: &str, ty: Ty<'_>) -> bool {
snippet.trim() == ty.to_string() || snippet.trim().contains(&format!("::{ty}"))
}

0 comments on commit e02f7cd

Please sign in to comment.