1 use clippy_utils::consts::{constant, Constant};
2 use clippy_utils::diagnostics::span_lint;
3 use clippy_utils::{method_chain_args, sext};
4 use if_chain::if_chain;
5 use rustc_hir::{Expr, ExprKind};
6 use rustc_lint::LateContext;
7 use rustc_middle::ty::{self, Ty};
9 use super::CAST_SIGN_LOSS;
11 pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_op: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
12 if should_lint(cx, cast_op, cast_from, cast_to) {
17 &format!("casting `{cast_from}` to `{cast_to}` may lose the sign of the value"),
22 fn should_lint(cx: &LateContext<'_>, cast_op: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) -> bool {
23 match (cast_from.is_integral(), cast_to.is_integral()) {
25 if !cast_from.is_signed() || cast_to.is_signed() {
29 // Don't lint for positive constants.
30 let const_val = constant(cx, cx.typeck_results(), cast_op);
32 if let Some((Constant::Int(n), _)) = const_val;
33 if let ty::Int(ity) = *cast_from.kind();
34 if sext(cx.tcx, n, ity) >= 0;
40 // Don't lint for the result of methods that always return non-negative values.
41 if let ExprKind::MethodCall(path, ..) = cast_op.kind {
42 let mut method_name = path.ident.name.as_str();
43 let allowed_methods = ["abs", "checked_abs", "rem_euclid", "checked_rem_euclid"];
46 if method_name == "unwrap";
47 if let Some(arglist) = method_chain_args(cast_op, &["unwrap"]);
48 if let ExprKind::MethodCall(inner_path, ..) = &arglist[0].0.kind;
50 method_name = inner_path.ident.name.as_str();
54 if allowed_methods.iter().any(|&name| method_name == name) {
62 (false, true) => !cast_to.is_signed(),