1 use clippy_utils::consts::{constant_full_int, FullInt};
2 use clippy_utils::diagnostics::span_lint_and_sugg;
3 use clippy_utils::source::snippet_with_applicability;
4 use clippy_utils::{in_constant, meets_msrv, msrvs, path_to_local};
5 use rustc_errors::Applicability;
6 use rustc_hir::{BinOpKind, Expr, ExprKind, Node, TyKind};
7 use rustc_lint::{LateContext, LateLintPass, LintContext};
8 use rustc_middle::lint::in_external_macro;
9 use rustc_semver::RustcVersion;
10 use rustc_session::{declare_tool_lint, impl_lint_pass};
12 declare_clippy_lint! {
14 /// Checks for an expression like `((x % 4) + 4) % 4` which is a common manual reimplementation
15 /// of `x.rem_euclid(4)`.
17 /// ### Why is this bad?
18 /// It's simpler and more readable.
23 /// let rem = ((x % 4) + 4) % 4;
28 /// let rem = x.rem_euclid(4);
30 #[clippy::version = "1.63.0"]
31 pub MANUAL_REM_EUCLID,
33 "manually reimplementing `rem_euclid`"
36 pub struct ManualRemEuclid {
37 msrv: Option<RustcVersion>,
40 impl ManualRemEuclid {
42 pub fn new(msrv: Option<RustcVersion>) -> Self {
47 impl_lint_pass!(ManualRemEuclid => [MANUAL_REM_EUCLID]);
49 impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid {
50 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
51 if !meets_msrv(self.msrv, msrvs::REM_EUCLID) {
55 if in_constant(cx, expr.hir_id) && !meets_msrv(self.msrv, msrvs::REM_EUCLID_CONST) {
59 if in_external_macro(cx.sess(), expr.span) {
63 if let ExprKind::Binary(op1, expr1, right) = expr.kind
64 && op1.node == BinOpKind::Rem
65 && let Some(const1) = check_for_unsigned_int_constant(cx, right)
66 && let ExprKind::Binary(op2, left, right) = expr1.kind
67 && op2.node == BinOpKind::Add
68 && let Some((const2, expr2)) = check_for_either_unsigned_int_constant(cx, left, right)
69 && let ExprKind::Binary(op3, expr3, right) = expr2.kind
70 && op3.node == BinOpKind::Rem
71 && let Some(const3) = check_for_unsigned_int_constant(cx, right)
72 // Also ensures the const is nonzero since zero can't be a divisor
73 && const1 == const2 && const2 == const3
74 && let Some(hir_id) = path_to_local(expr3)
75 && let Some(Node::Pat(_)) = cx.tcx.hir().find(hir_id) {
76 // Apply only to params or locals with annotated types
77 match cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
78 Some(Node::Param(..)) => (),
79 Some(Node::Local(local)) => {
80 let Some(ty) = local.ty else { return };
81 if matches!(ty.kind, TyKind::Infer) {
88 let mut app = Applicability::MachineApplicable;
89 let rem_of = snippet_with_applicability(cx, expr3.span, "_", &mut app);
94 "manual `rem_euclid` implementation",
96 format!("{rem_of}.rem_euclid({const1})"),
102 extract_msrv_attr!(LateContext);
105 // Checks if either the left or right expressions can be an unsigned int constant and returns that
106 // constant along with the other expression unchanged if so
107 fn check_for_either_unsigned_int_constant<'a>(
108 cx: &'a LateContext<'_>,
111 ) -> Option<(u128, &'a Expr<'a>)> {
112 check_for_unsigned_int_constant(cx, left)
113 .map(|int_const| (int_const, right))
114 .or_else(|| check_for_unsigned_int_constant(cx, right).map(|int_const| (int_const, left)))
117 fn check_for_unsigned_int_constant<'a>(cx: &'a LateContext<'_>, expr: &'a Expr<'_>) -> Option<u128> {
118 let Some(int_const) = constant_full_int(cx, cx.typeck_results(), expr) else { return None };
120 FullInt::S(s) => s.try_into().ok(),
121 FullInt::U(u) => Some(u),