fix: manual_rotate wrongly unmangled macros

This commit is contained in:
Linshu Yang
2026-01-05 17:58:10 +00:00
committed by linshuy2
parent ac86fd103d
commit d3ca65d04a
4 changed files with 73 additions and 9 deletions
+14 -8
View File
@@ -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,
);
},
);
}
}
+23
View File
@@ -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);
}
+23
View File
@@ -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);
}
+13 -1
View File
@@ -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