@@ -41,7 +41,37 @@ declare_clippy_lint! {
4141 "Perform saturating subtraction instead of implicitly checking lower bound of data type"
4242}
4343
44- declare_lint_pass ! ( ImplicitSaturatingSub => [ IMPLICIT_SATURATING_SUB ] ) ;
44+ declare_clippy_lint ! {
45+ /// ### What it does
46+ /// Checks for comparisons between integers, followed by subtracting the greater value from the
47+ /// lower one.
48+ ///
49+ /// ### Why is this bad?
50+ /// This could result in an underflow and is most likely not what the user wants. If this was
51+ /// intended to be a saturated subtraction, consider using the `saturating_sub` method directly.
52+ ///
53+ /// ### Example
54+ /// ```no_run
55+ /// let a = 12u32;
56+ /// let b = 13u32;
57+ ///
58+ /// let result = if a > b { b - a } else { 0 };
59+ /// ```
60+ ///
61+ /// Use instead:
62+ /// ```no_run
63+ /// let a = 12u32;
64+ /// let b = 13u32;
65+ ///
66+ /// let result = a.saturating_sub(b);
67+ /// ```
68+ #[ clippy:: version = "1.44.0" ]
69+ pub INVERTED_SATURATING_SUB ,
70+ correctness,
71+ "Check if a variable is smaller than another one and still subtract from it even if smaller"
72+ }
73+
74+ declare_lint_pass ! ( ImplicitSaturatingSub => [ IMPLICIT_SATURATING_SUB , INVERTED_SATURATING_SUB ] ) ;
4575
4676impl < ' tcx > LateLintPass < ' tcx > for ImplicitSaturatingSub {
4777 fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > ) {
@@ -182,7 +212,7 @@ fn check_subtraction(
182212 {
183213 span_lint_and_then (
184214 cx,
185- IMPLICIT_SATURATING_SUB ,
215+ INVERTED_SATURATING_SUB ,
186216 condition_span,
187217 "inverted arithmetic check before subtraction" ,
188218 |diag| {
0 commit comments