@@ -11,6 +11,7 @@ mod float_cmp;
11
11
mod float_equality_without_abs;
12
12
mod identity_op;
13
13
mod integer_division;
14
+ mod manual_is_multiple_of;
14
15
mod manual_midpoint;
15
16
mod misrefactored_assign_op;
16
17
mod modulo_arithmetic;
@@ -830,12 +831,42 @@ declare_clippy_lint! {
830
831
"manual implementation of `midpoint` which can overflow"
831
832
}
832
833
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.89.0" ]
858
+ pub MANUAL_IS_MULTIPLE_OF ,
859
+ complexity,
860
+ "manual implementation of `.is_multiple_of()`"
861
+ }
862
+
833
863
pub struct Operators {
834
864
arithmetic_context : numeric_arithmetic:: Context ,
835
865
verbose_bit_mask_threshold : u64 ,
836
866
modulo_arithmetic_allow_comparison_to_zero : bool ,
837
867
msrv : Msrv ,
838
868
}
869
+
839
870
impl Operators {
840
871
pub fn new ( conf : & ' static Conf ) -> Self {
841
872
Self {
@@ -874,6 +905,7 @@ impl_lint_pass!(Operators => [
874
905
NEEDLESS_BITWISE_BOOL ,
875
906
SELF_ASSIGNMENT ,
876
907
MANUAL_MIDPOINT ,
908
+ MANUAL_IS_MULTIPLE_OF ,
877
909
] ) ;
878
910
879
911
impl < ' tcx > LateLintPass < ' tcx > for Operators {
@@ -891,6 +923,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
891
923
identity_op:: check ( cx, e, op. node , lhs, rhs) ;
892
924
needless_bitwise_bool:: check ( cx, e, op. node , lhs, rhs) ;
893
925
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 ) ;
894
927
}
895
928
self . arithmetic_context . check_binary ( cx, e, op. node , lhs, rhs) ;
896
929
bit_mask:: check ( cx, e, op. node , lhs, rhs) ;
0 commit comments