|
| 1 | +use clippy_utils::consts::{constant, Constant}; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 3 | +use clippy_utils::get_parent_expr; |
| 4 | +use clippy_utils::source::snippet_with_applicability; |
| 5 | +use if_chain::if_chain; |
| 6 | +use rustc_ast::ast::{LitIntType, LitKind}; |
| 7 | +use rustc_errors::Applicability; |
| 8 | +use rustc_hir::{BinOpKind, Block, Expr, ExprKind, Stmt, StmtKind}; |
| 9 | +use rustc_lint::{LateContext, LateLintPass}; |
| 10 | +use rustc_middle::ty::{Int, IntTy, Ty, Uint, UintTy}; |
| 11 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Checks for implicit saturating addition. |
| 16 | + /// |
| 17 | + /// ### Why is this bad? |
| 18 | + /// The built-in function is more readable and may be faster. |
| 19 | + /// |
| 20 | + /// ### Example |
| 21 | + /// ```rust |
| 22 | + ///let mut u:u32 = 7000; |
| 23 | + /// |
| 24 | + /// if u != u32::MAX { |
| 25 | + /// u += 1; |
| 26 | + /// } |
| 27 | + /// ``` |
| 28 | + /// Use instead: |
| 29 | + /// ```rust |
| 30 | + ///let mut u:u32 = 7000; |
| 31 | + /// |
| 32 | + /// u = u.saturating_add(1); |
| 33 | + /// ``` |
| 34 | + #[clippy::version = "1.65.0"] |
| 35 | + pub IMPLICIT_SATURATING_ADD, |
| 36 | + style, |
| 37 | + "Perform saturating addition instead of implicitly checking max bound of data type" |
| 38 | +} |
| 39 | +declare_lint_pass!(ImplicitSaturatingAdd => [IMPLICIT_SATURATING_ADD]); |
| 40 | + |
| 41 | +impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingAdd { |
| 42 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 43 | + if_chain! { |
| 44 | + if let ExprKind::If(cond, then, None) = expr.kind; |
| 45 | + if let ExprKind::DropTemps(expr1) = cond.kind; |
| 46 | + if let Some((c, op_node, l)) = get_const(cx, expr1); |
| 47 | + if let BinOpKind::Ne | BinOpKind::Lt = op_node; |
| 48 | + if let ExprKind::Block(block, None) = then.kind; |
| 49 | + if let Block { |
| 50 | + stmts: |
| 51 | + [Stmt |
| 52 | + { kind: StmtKind::Expr(ex) | StmtKind::Semi(ex), .. }], |
| 53 | + expr: None, ..} | |
| 54 | + Block { stmts: [], expr: Some(ex), ..} = block; |
| 55 | + if let ExprKind::AssignOp(op1, target, value) = ex.kind; |
| 56 | + let ty = cx.typeck_results().expr_ty(target); |
| 57 | + if Some(c) == get_int_max(ty); |
| 58 | + if clippy_utils::SpanlessEq::new(cx).eq_expr(l, target); |
| 59 | + if BinOpKind::Add == op1.node; |
| 60 | + if let ExprKind::Lit(ref lit) = value.kind; |
| 61 | + if let LitKind::Int(1, LitIntType::Unsuffixed) = lit.node; |
| 62 | + if block.expr.is_none(); |
| 63 | + then { |
| 64 | + let mut app = Applicability::MachineApplicable; |
| 65 | + let code = snippet_with_applicability(cx, target.span, "_", &mut app); |
| 66 | + let sugg = if let Some(parent) = get_parent_expr(cx, expr) && let ExprKind::If(_cond, _then, Some(else_)) = parent.kind && else_.hir_id == expr.hir_id {format!("{{{code} = {code}.saturating_add(1); }}")} else {format!("{code} = {code}.saturating_add(1);")}; |
| 67 | + span_lint_and_sugg(cx, IMPLICIT_SATURATING_ADD, expr.span, "manual saturating add detected", "use instead", sugg, app); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +fn get_int_max(ty: Ty<'_>) -> Option<u128> { |
| 74 | + match ty.peel_refs().kind() { |
| 75 | + Int(IntTy::I8) => i8::max_value().try_into().ok(), |
| 76 | + Int(IntTy::I16) => i16::max_value().try_into().ok(), |
| 77 | + Int(IntTy::I32) => i32::max_value().try_into().ok(), |
| 78 | + Int(IntTy::I64) => i64::max_value().try_into().ok(), |
| 79 | + Int(IntTy::I128) => i128::max_value().try_into().ok(), |
| 80 | + Int(IntTy::Isize) => isize::max_value().try_into().ok(), |
| 81 | + Uint(UintTy::U8) => u8::max_value().try_into().ok(), |
| 82 | + Uint(UintTy::U16) => u16::max_value().try_into().ok(), |
| 83 | + Uint(UintTy::U32) => u32::max_value().try_into().ok(), |
| 84 | + Uint(UintTy::U64) => u64::max_value().try_into().ok(), |
| 85 | + Uint(UintTy::U128) => Some(u128::max_value()), |
| 86 | + Uint(UintTy::Usize) => usize::max_value().try_into().ok(), |
| 87 | + _ => None, |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +fn get_const<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> Option<(u128, BinOpKind, &'tcx Expr<'tcx>)> { |
| 92 | + if let ExprKind::Binary(op, l, r) = expr.kind { |
| 93 | + let tr = cx.typeck_results(); |
| 94 | + if let Some((Constant::Int(c), _)) = constant(cx, tr, r) { |
| 95 | + return Some((c, op.node, l)); |
| 96 | + }; |
| 97 | + if let Some((Constant::Int(c), _)) = constant(cx, tr, l) { |
| 98 | + return Some((c, invert_op(op.node)?, r)); |
| 99 | + } |
| 100 | + } |
| 101 | + None |
| 102 | +} |
| 103 | + |
| 104 | +fn invert_op(op: BinOpKind) -> Option<BinOpKind> { |
| 105 | + use rustc_hir::BinOpKind::{Ge, Gt, Le, Lt, Ne}; |
| 106 | + match op { |
| 107 | + Lt => Some(Gt), |
| 108 | + Le => Some(Ge), |
| 109 | + Ne => Some(Ne), |
| 110 | + Ge => Some(Le), |
| 111 | + Gt => Some(Lt), |
| 112 | + _ => None, |
| 113 | + } |
| 114 | +} |
0 commit comments