@@ -41,7 +41,36 @@ declare_clippy_lint! {
41
41
"Perform saturating subtraction instead of implicitly checking lower bound of data type"
42
42
}
43
43
44
- declare_lint_pass ! ( ImplicitSaturatingSub => [ IMPLICIT_SATURATING_SUB ] ) ;
44
+ declare_clippy_lint ! {
45
+ /// ### What it does
46
+ /// Checks for inverted subtraction operations after check.
47
+ ///
48
+ /// ### Why is this bad?
49
+ /// If a variable is smaller than another one, but the other one is still subtracted, then
50
+ /// it will end in an underflow.
51
+ ///
52
+ /// ### Example
53
+ /// ```no_run
54
+ /// let a = 12u32;
55
+ /// let b = 13u32;
56
+ ///
57
+ /// let result = if a > b { b - a } else { 0 };
58
+ /// ```
59
+ ///
60
+ /// Use instead:
61
+ /// ```no_run
62
+ /// let a = 12u32;
63
+ /// let b = 13u32;
64
+ ///
65
+ /// let result = a.saturating_sub(b);
66
+ /// ```
67
+ #[ clippy:: version = "1.44.0" ]
68
+ pub INVERTED_SATURATING_SUB ,
69
+ correctness,
70
+ "Check if a variable is smaller than another one and still subtract from it even if smaller"
71
+ }
72
+
73
+ declare_lint_pass ! ( ImplicitSaturatingSub => [ IMPLICIT_SATURATING_SUB , INVERTED_SATURATING_SUB ] ) ;
45
74
46
75
impl < ' tcx > LateLintPass < ' tcx > for ImplicitSaturatingSub {
47
76
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > ) {
@@ -182,7 +211,7 @@ fn check_subtraction(
182
211
{
183
212
span_lint_and_then (
184
213
cx,
185
- IMPLICIT_SATURATING_SUB ,
214
+ INVERTED_SATURATING_SUB ,
186
215
condition_span,
187
216
"inverted arithmetic check before subtraction" ,
188
217
|diag| {
0 commit comments