@@ -20,6 +20,7 @@ mod needless_bitwise_bool;
20
20
mod numeric_arithmetic;
21
21
mod op_ref;
22
22
mod self_assignment;
23
+ mod unsigned_subtraction_gt_zero;
23
24
mod verbose_bit_mask;
24
25
25
26
pub ( crate ) mod arithmetic_side_effects;
@@ -62,6 +63,34 @@ declare_clippy_lint! {
62
63
"a comparison with a maximum or minimum value that is always true or false"
63
64
}
64
65
66
+ declare_clippy_lint ! {
67
+ /// ### What it does
68
+ /// Lints comparisons of an unsigned integer subtraction against zero, like `(a - b) > 0`.
69
+ ///
70
+ /// ### Why is this bad?
71
+ /// If `a < b`, `a - b` will panic in debug builds and wrap in release builds, making the
72
+ /// comparison effectively behave like `a != b`. This is likely unintended; `a > b` is clearer
73
+ /// and avoids potential panics and wraparound.
74
+ ///
75
+ /// ### Example
76
+ /// ```no_run
77
+ /// let (a, b): (u32, u32) = (1, 2);
78
+ /// if a - b > 0 {}
79
+ /// ```
80
+ ///
81
+ /// Use instead:
82
+ /// ```no_run
83
+ /// let (a, b): (u32, u32) = (1, 2);
84
+ /// if a > b {}
85
+ /// // Or, if you meant inequality:
86
+ /// if a != b {}
87
+ /// ```
88
+ #[ clippy:: version = "1.92.0" ]
89
+ pub UNSIGNED_SUBTRACTION_GT_ZERO ,
90
+ correctness,
91
+ "suspicious comparison of unsigned subtraction to zero"
92
+ }
93
+
65
94
declare_clippy_lint ! {
66
95
/// ### What it does
67
96
/// Checks any kind of arithmetic operation of any type.
@@ -906,6 +935,7 @@ impl_lint_pass!(Operators => [
906
935
SELF_ASSIGNMENT ,
907
936
MANUAL_MIDPOINT ,
908
937
MANUAL_IS_MULTIPLE_OF ,
938
+ UNSIGNED_SUBTRACTION_GT_ZERO ,
909
939
] ) ;
910
940
911
941
impl < ' tcx > LateLintPass < ' tcx > for Operators {
@@ -932,6 +962,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
932
962
const_comparisons:: check ( cx, op, lhs, rhs, e. span ) ;
933
963
duration_subsec:: check ( cx, e, op. node , lhs, rhs) ;
934
964
float_equality_without_abs:: check ( cx, e, op. node , lhs, rhs) ;
965
+ unsigned_subtraction_gt_zero:: check ( cx, e, op. node , lhs, rhs) ;
935
966
integer_division:: check ( cx, e, op. node , lhs, rhs) ;
936
967
cmp_owned:: check ( cx, op. node , lhs, rhs) ;
937
968
float_cmp:: check ( cx, e, op. node , lhs, rhs) ;
0 commit comments