From d3ca65d04a88c48665b07c83d03ee1fd0249dbc9 Mon Sep 17 00:00:00 2001 From: Linshu Yang Date: Mon, 5 Jan 2026 17:58:10 +0000 Subject: [PATCH] fix: `manual_rotate` wrongly unmangled macros --- clippy_lints/src/manual_rotate.rs | 22 ++++++++++++++-------- tests/ui/manual_rotate.fixed | 23 +++++++++++++++++++++++ tests/ui/manual_rotate.rs | 23 +++++++++++++++++++++++ tests/ui/manual_rotate.stderr | 14 +++++++++++++- 4 files changed, 73 insertions(+), 9 deletions(-) diff --git a/clippy_lints/src/manual_rotate.rs b/clippy_lints/src/manual_rotate.rs index e8db44698d9c..c371e5b47df8 100644 --- a/clippy_lints/src/manual_rotate.rs +++ b/clippy_lints/src/manual_rotate.rs @@ -1,7 +1,7 @@ use std::fmt::Display; use clippy_utils::consts::{ConstEvalCtxt, Constant}; -use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::sugg; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind}; @@ -116,17 +116,23 @@ fn check_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { } }; - let mut applicability = Applicability::MachineApplicable; - let expr_sugg = sugg::Sugg::hir_with_applicability(cx, l_expr, "_", &mut applicability).maybe_paren(); - let amount = sugg::Sugg::hir_with_applicability(cx, amount, "_", &mut applicability); - span_lint_and_sugg( + span_lint_and_then( cx, MANUAL_ROTATE, expr.span, "there is no need to manually implement bit rotation", - "this expression can be rewritten as", - format!("{expr_sugg}.{shift_function}({amount})"), - Applicability::MachineApplicable, + |diag| { + let mut applicability = Applicability::MachineApplicable; + let expr_sugg = sugg::Sugg::hir_with_context(cx, l_expr, expr.span.ctxt(), "_", &mut applicability) + .maybe_paren(); + let amount = sugg::Sugg::hir_with_context(cx, amount, expr.span.ctxt(), "_", &mut applicability); + diag.span_suggestion( + expr.span, + "this expression can be rewritten as", + format!("{expr_sugg}.{shift_function}({amount})"), + applicability, + ); + }, ); } } diff --git a/tests/ui/manual_rotate.fixed b/tests/ui/manual_rotate.fixed index 1012ffc1aa2d..17fb4a3122f3 100644 --- a/tests/ui/manual_rotate.fixed +++ b/tests/ui/manual_rotate.fixed @@ -61,3 +61,26 @@ fn issue13028() { // don't lint, because `s` and `u` are different variables, albeit with the same value let _ = (x << s) | (x >> (32 - u)); } + +fn wrongly_unmangled_macros() { + macro_rules! test_expr { + ($val:expr) => { + $val.inner + }; + } + + struct Wrapper { + inner: u32, + } + + let x = Wrapper { inner: 42 }; + let _ = test_expr!(x).rotate_left(3); + //~^ manual_rotate + + let y = Wrapper { inner: 100 }; + + let _ = x.inner.rotate_left(test_expr!(y)); + //~^ manual_rotate + + let _ = (test_expr!(x) << 3) | (x.inner >> 29); +} diff --git a/tests/ui/manual_rotate.rs b/tests/ui/manual_rotate.rs index 3cdc79673c81..5899166f51f7 100644 --- a/tests/ui/manual_rotate.rs +++ b/tests/ui/manual_rotate.rs @@ -61,3 +61,26 @@ fn issue13028() { // don't lint, because `s` and `u` are different variables, albeit with the same value let _ = (x << s) | (x >> (32 - u)); } + +fn wrongly_unmangled_macros() { + macro_rules! test_expr { + ($val:expr) => { + $val.inner + }; + } + + struct Wrapper { + inner: u32, + } + + let x = Wrapper { inner: 42 }; + let _ = (test_expr!(x) << 3) | (test_expr!(x) >> 29); + //~^ manual_rotate + + let y = Wrapper { inner: 100 }; + + let _ = (x.inner << test_expr!(y)) | (x.inner >> (32 - test_expr!(y))); + //~^ manual_rotate + + let _ = (test_expr!(x) << 3) | (x.inner >> 29); +} diff --git a/tests/ui/manual_rotate.stderr b/tests/ui/manual_rotate.stderr index ea04ee028db6..76af443ae89e 100644 --- a/tests/ui/manual_rotate.stderr +++ b/tests/ui/manual_rotate.stderr @@ -91,5 +91,17 @@ error: there is no need to manually implement bit rotation LL | let _ = (x >> 9) | (x << (32 - 9)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x.rotate_right(9)` -error: aborting due to 15 previous errors +error: there is no need to manually implement bit rotation + --> tests/ui/manual_rotate.rs:77:13 + | +LL | let _ = (test_expr!(x) << 3) | (test_expr!(x) >> 29); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `test_expr!(x).rotate_left(3)` + +error: there is no need to manually implement bit rotation + --> tests/ui/manual_rotate.rs:82:13 + | +LL | let _ = (x.inner << test_expr!(y)) | (x.inner >> (32 - test_expr!(y))); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: this expression can be rewritten as: `x.inner.rotate_left(test_expr!(y))` + +error: aborting due to 17 previous errors