@@ -11,6 +11,7 @@ mod float_cmp;
1111mod float_equality_without_abs;
1212mod identity_op;
1313mod integer_division;
14+ mod manual_is_multiple_of;
1415mod manual_midpoint;
1516mod misrefactored_assign_op;
1617mod modulo_arithmetic;
@@ -830,12 +831,42 @@ declare_clippy_lint! {
830831 "manual implementation of `midpoint` which can overflow"
831832}
832833
834+ declare_clippy_lint ! {
835+ /// ### What it does
836+ /// Checks for manual implementation of `.is_multiple_of()` on
837+ /// unsigned integer types.
838+ ///
839+ /// ### Why is this bad?
840+ /// `a.is_multiple_of(b)` is a clearer way to check for divisibility
841+ /// of `a` by `b`. This expression can never panic.
842+ ///
843+ /// ### Example
844+ /// ```no_run
845+ /// # let (a, b) = (3u64, 4u64);
846+ /// if a % b == 0 {
847+ /// println!("{a} is divisible by {b}");
848+ /// }
849+ /// ```
850+ /// Use instead:
851+ /// ```no_run
852+ /// # let (a, b) = (3u64, 4u64);
853+ /// if a.is_multiple_of(b) {
854+ /// println!("{a} is divisible by {b}");
855+ /// }
856+ /// ```
857+ #[ clippy:: version = "1.88.0" ]
858+ pub MANUAL_IS_MULTIPLE_OF ,
859+ complexity,
860+ "manual implementation of `.is_multiple_of()`"
861+ }
862+
833863pub struct Operators {
834864 arithmetic_context : numeric_arithmetic:: Context ,
835865 verbose_bit_mask_threshold : u64 ,
836866 modulo_arithmetic_allow_comparison_to_zero : bool ,
837867 msrv : Msrv ,
838868}
869+
839870impl Operators {
840871 pub fn new ( conf : & ' static Conf ) -> Self {
841872 Self {
@@ -874,6 +905,7 @@ impl_lint_pass!(Operators => [
874905 NEEDLESS_BITWISE_BOOL ,
875906 SELF_ASSIGNMENT ,
876907 MANUAL_MIDPOINT ,
908+ MANUAL_IS_MULTIPLE_OF ,
877909] ) ;
878910
879911impl < ' tcx > LateLintPass < ' tcx > for Operators {
@@ -891,6 +923,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
891923 identity_op:: check ( cx, e, op. node , lhs, rhs) ;
892924 needless_bitwise_bool:: check ( cx, e, op. node , lhs, rhs) ;
893925 manual_midpoint:: check ( cx, e, op. node , lhs, rhs, self . msrv ) ;
926+ manual_is_multiple_of:: check ( cx, e, op. node , lhs, rhs, self . msrv ) ;
894927 }
895928 self . arithmetic_context . check_binary ( cx, e, op. node , lhs, rhs) ;
896929 bit_mask:: check ( cx, e, op. node , lhs, rhs) ;
0 commit comments