1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::is_qpath_def_path;
3 use clippy_utils::source::snippet_with_applicability;
4 use if_chain::if_chain;
6 use rustc_errors::Applicability;
8 use rustc_lint::LateContext;
9 use rustc_target::abi::LayoutOf;
14 arith_lhs: &hir::Expr<'_>,
15 arith_rhs: &hir::Expr<'_>,
16 unwrap_arg: &hir::Expr<'_>,
19 let ty = cx.typeck_results().expr_ty(arith_lhs);
20 if !ty.is_integral() {
24 let mm = if let Some(mm) = is_min_or_max(cx, unwrap_arg) {
36 let sign = if let Some(sign) = lit_sign(arith_rhs) {
42 match (arith, sign, mm) {
43 ("add", Pos, Max) | ("add", Neg, Min) | ("sub", Neg, Max) | ("sub", Pos, Min) => (),
44 // "mul" is omitted because lhs can be negative.
49 (MinMax::Max, "add" | "mul") | (MinMax::Min, "sub") => (),
54 let mut applicability = Applicability::MachineApplicable;
57 super::MANUAL_SATURATING_ARITHMETIC,
59 "manual saturating arithmetic",
60 &format!("try using `saturating_{}`", arith),
62 "{}.saturating_{}({})",
63 snippet_with_applicability(cx, arith_lhs.span, "..", &mut applicability),
65 snippet_with_applicability(cx, arith_rhs.span, "..", &mut applicability),
71 #[derive(PartialEq, Eq)]
77 fn is_min_or_max<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>) -> Option<MinMax> {
78 // `T::max_value()` `T::min_value()` inherent methods
80 if let hir::ExprKind::Call(func, args) = &expr.kind;
82 if let hir::ExprKind::Path(hir::QPath::TypeRelative(_, segment)) = &func.kind;
84 match &*segment.ident.as_str() {
85 "max_value" => return Some(MinMax::Max),
86 "min_value" => return Some(MinMax::Min),
92 let ty = cx.typeck_results().expr_ty(expr);
93 let ty_str = ty.to_string();
95 // `std::T::MAX` `std::T::MIN` constants
96 if let hir::ExprKind::Path(path) = &expr.kind {
97 if is_qpath_def_path(cx, path, expr.hir_id, &["core", &ty_str, "MAX"][..]) {
98 return Some(MinMax::Max);
101 if is_qpath_def_path(cx, path, expr.hir_id, &["core", &ty_str, "MIN"][..]) {
102 return Some(MinMax::Min);
107 let bits = cx.layout_of(ty).unwrap().size.bits();
108 let (minval, maxval): (u128, u128) = if ty.is_signed() {
109 let minval = 1 << (bits - 1);
110 let mut maxval = !(1 << (bits - 1));
112 maxval &= (1 << bits) - 1;
116 (0, if bits == 128 { !0 } else { (1 << bits) - 1 })
119 let check_lit = |expr: &hir::Expr<'_>, check_min: bool| {
120 if let hir::ExprKind::Lit(lit) = &expr.kind {
121 if let ast::LitKind::Int(value, _) = lit.node {
123 return Some(MinMax::Max);
126 if check_min && value == minval {
127 return Some(MinMax::Min);
135 if let r @ Some(_) = check_lit(expr, !ty.is_signed()) {
140 if let hir::ExprKind::Unary(hir::UnOp::Neg, val) = &expr.kind {
141 return check_lit(val, true);
148 #[derive(PartialEq, Eq)]
154 fn lit_sign(expr: &hir::Expr<'_>) -> Option<Sign> {
155 if let hir::ExprKind::Unary(hir::UnOp::Neg, inner) = &expr.kind {
156 if let hir::ExprKind::Lit(..) = &inner.kind {
157 return Some(Sign::Neg);
159 } else if let hir::ExprKind::Lit(..) = &expr.kind {
160 return Some(Sign::Pos);