diff --git a/clippy_lints/src/manual_rem_euclid.rs b/clippy_lints/src/manual_rem_euclid.rs index 3ffd9a07376..c338447686f 100644 --- a/clippy_lints/src/manual_rem_euclid.rs +++ b/clippy_lints/src/manual_rem_euclid.rs @@ -61,12 +61,23 @@ impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid { && op3.node == BinOpKind::Rem && let Some((const3, expr3)) = check_for_positive_int_constant(cx, expr2, false) && const1 == const2 && const2 == const3 - // Only apply if we see an explicit type annotation on the local. && let Some(hir_id) = path_to_local(expr3) - && let Some(Node::Binding(_)) = cx.tcx.hir().find(hir_id) - && let Some(Node::Local(local)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) - && let Some(ty) = local.ty - && !matches!(ty.kind, TyKind::Infer) { + && let Some(Node::Binding(_)) = cx.tcx.hir().find(hir_id) { + // Apply only to params or locals with annotated types + match cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) { + Some(Node::Param(..)) => (), + Some(Node::Local(local)) => { + if let Some(ty) = local.ty { + if matches!(ty.kind, TyKind::Infer) { + return; + } + } else { + return; + } + } + _ => return, + }; + let mut app = Applicability::MachineApplicable; let rem_of = snippet_with_applicability(cx, expr3.span, "_", &mut app); span_lint_and_sugg( diff --git a/tests/ui/manual_rem_euclid.fixed b/tests/ui/manual_rem_euclid.fixed index d0aa2398811..7dcd78f4633 100644 --- a/tests/ui/manual_rem_euclid.fixed +++ b/tests/ui/manual_rem_euclid.fixed @@ -26,3 +26,8 @@ fn main() { let _: i32 = 4 % ((value % 4) + 4); let _: i32 = ((4 % value) + 4) % 4; } + +// Should lint for params too +pub fn rem_euclid_4(num: i32) -> i32 { + num.rem_euclid(4) +} diff --git a/tests/ui/manual_rem_euclid.rs b/tests/ui/manual_rem_euclid.rs index ff87d6fab52..f5d88ed6dfc 100644 --- a/tests/ui/manual_rem_euclid.rs +++ b/tests/ui/manual_rem_euclid.rs @@ -26,3 +26,8 @@ fn main() { let _: i32 = 4 % ((value % 4) + 4); let _: i32 = ((4 % value) + 4) % 4; } + +// Should lint for params too +pub fn rem_euclid_4(num: i32) -> i32 { + ((num % 4) + 4) % 4 +} diff --git a/tests/ui/manual_rem_euclid.stderr b/tests/ui/manual_rem_euclid.stderr index 7134759dcc9..bad6e66beef 100644 --- a/tests/ui/manual_rem_euclid.stderr +++ b/tests/ui/manual_rem_euclid.stderr @@ -30,5 +30,11 @@ error: manual `rem_euclid` implementation LL | let _: i32 = 1 + (4 + value % 4) % 4; | ^^^^^^^^^^^^^^^^^^^ help: consider using: `value.rem_euclid(4)` -error: aborting due to 5 previous errors +error: manual `rem_euclid` implementation + --> $DIR/manual_rem_euclid.rs:32:5 + | +LL | ((num % 4) + 4) % 4 + | ^^^^^^^^^^^^^^^^^^^ help: consider using: `num.rem_euclid(4)` + +error: aborting due to 6 previous errors