1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::sugg::Sugg;
3 use clippy_utils::{meets_msrv, msrvs};
4 use rustc_errors::Applicability;
5 use rustc_hir::{Expr, ExprKind};
6 use rustc_lint::LateContext;
7 use rustc_middle::ty::{self, Ty};
8 use rustc_semver::RustcVersion;
10 use super::CAST_ABS_TO_UNSIGNED;
18 msrv: Option<RustcVersion>,
20 if meets_msrv(msrv, msrvs::UNSIGNED_ABS)
21 && let ty::Int(from) = cast_from.kind()
22 && let ty::Uint(to) = cast_to.kind()
23 && let ExprKind::MethodCall(method_path, receiver, ..) = cast_expr.kind
24 && method_path.ident.name.as_str() == "abs"
26 let span = if from.bit_width() == to.bit_width() {
29 // if the result of `.unsigned_abs` would be a different type, keep the cast
30 // e.g. `i64 -> usize`, `i16 -> u8`
38 &format!("casting the result of `{cast_from}::abs()` to {cast_to}"),
40 format!("{}.unsigned_abs()", Sugg::hir(cx, receiver, "..").maybe_par()),
41 Applicability::MachineApplicable,