@@ -41,7 +41,37 @@ 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 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 ] ) ;
45
75
46
76
impl < ' tcx > LateLintPass < ' tcx > for ImplicitSaturatingSub {
47
77
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > ) {
@@ -182,7 +212,7 @@ fn check_subtraction(
182
212
{
183
213
span_lint_and_then (
184
214
cx,
185
- IMPLICIT_SATURATING_SUB ,
215
+ INVERTED_SATURATING_SUB ,
186
216
condition_span,
187
217
"inverted arithmetic check before subtraction" ,
188
218
|diag| {
0 commit comments