]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/casts/cast_abs_to_unsigned.rs
Auto merge of #8823 - smoelius:unknown-field, r=xFrednet
[rust.git] / clippy_lints / src / casts / cast_abs_to_unsigned.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::sugg::Sugg;
3 use clippy_utils::{meets_msrv, msrvs};
4 use if_chain::if_chain;
5 use rustc_errors::Applicability;
6 use rustc_hir::{Expr, ExprKind};
7 use rustc_lint::LateContext;
8 use rustc_middle::ty::Ty;
9 use rustc_semver::RustcVersion;
10
11 use super::CAST_ABS_TO_UNSIGNED;
12
13 pub(super) fn check(
14     cx: &LateContext<'_>,
15     expr: &Expr<'_>,
16     cast_expr: &Expr<'_>,
17     cast_from: Ty<'_>,
18     cast_to: Ty<'_>,
19     msrv: Option<RustcVersion>,
20 ) {
21     if_chain! {
22         if meets_msrv(msrv, msrvs::UNSIGNED_ABS);
23         if cast_from.is_integral();
24         if cast_to.is_integral();
25         if cast_from.is_signed();
26         if !cast_to.is_signed();
27         if let ExprKind::MethodCall(method_path, args, _) = cast_expr.kind;
28         if let method_name = method_path.ident.name.as_str();
29         if method_name == "abs";
30         then {
31             span_lint_and_sugg(
32                 cx,
33                 CAST_ABS_TO_UNSIGNED,
34                 expr.span,
35                 &format!("casting the result of `{}::{}()` to {}", cast_from, method_name, cast_to),
36                 "replace with",
37                 format!("{}.unsigned_abs()", Sugg::hir(cx, &args[0], "..")),
38                 Applicability::MachineApplicable,
39             );
40         }
41     }
42 }