From bff811bfdf117ac737d44452c8aa645dd516f8a0 Mon Sep 17 00:00:00 2001 From: TennyZhuang Date: Sun, 2 Oct 2022 18:41:13 +0800 Subject: [PATCH] extract common codes Signed-off-by: TennyZhuang --- clippy_lints/src/casts/unnecessary_cast.rs | 28 ++++++++-------------- 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/clippy_lints/src/casts/unnecessary_cast.rs b/clippy_lints/src/casts/unnecessary_cast.rs index 071fab1165d4..610e8f712f49 100644 --- a/clippy_lints/src/casts/unnecessary_cast.rs +++ b/clippy_lints/src/casts/unnecessary_cast.rs @@ -34,7 +34,7 @@ pub(super) fn check<'tcx>( let cast_str = snippet_opt(cx, cast_expr.span).unwrap_or_default(); if let Some(lit) = get_numeric_literal(cast_expr) { - let literal_str = cast_str; + let literal_str = &cast_str; if_chain! { if let LitKind::Int(n, _) = lit.node; @@ -52,10 +52,12 @@ pub(super) fn check<'tcx>( match lit.node { LitKind::Int(_, LitIntType::Unsuffixed) if cast_to.is_integral() => { - lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to); + lint_unnecessary_cast(cx, expr, literal_str, cast_from, cast_to); + return true; }, LitKind::Float(_, LitFloatType::Unsuffixed) if cast_to.is_floating_point() => { - lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to); + lint_unnecessary_cast(cx, expr, literal_str, cast_from, cast_to); + return true; }, LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {}, LitKind::Int(_, LitIntType::Signed(_) | LitIntType::Unsigned(_)) @@ -65,25 +67,15 @@ pub(super) fn check<'tcx>( if let Some(src) = snippet_opt(cx, cast_expr.span) { if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node) { lint_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to); + return true; } } }, - _ => { - if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) { - span_lint_and_sugg( - cx, - UNNECESSARY_CAST, - expr.span, - &format!("casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)"), - "try", - literal_str, - Applicability::MachineApplicable, - ); - return true; - } - }, + _ => {}, } - } else if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) { + } + + if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) { span_lint_and_sugg( cx, UNNECESSARY_CAST,