Skip to content

Commit 90f8277

Browse files
author
Evan Typanski
committed
Fix case for function params
1 parent c8df6d6 commit 90f8277

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

clippy_lints/src/manual_rem_euclid.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,23 @@ impl<'tcx> LateLintPass<'tcx> for ManualRemEuclid {
6161
&& op3.node == BinOpKind::Rem
6262
&& let Some((const3, expr3)) = check_for_positive_int_constant(cx, expr2, false)
6363
&& const1 == const2 && const2 == const3
64-
// Only apply if we see an explicit type annotation on the local.
6564
&& let Some(hir_id) = path_to_local(expr3)
66-
&& let Some(Node::Binding(_)) = cx.tcx.hir().find(hir_id)
67-
&& let Some(Node::Local(local)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id))
68-
&& let Some(ty) = local.ty
69-
&& !matches!(ty.kind, TyKind::Infer) {
65+
&& let Some(Node::Binding(_)) = cx.tcx.hir().find(hir_id) {
66+
// Apply only to params or locals with annotated types
67+
match cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
68+
Some(Node::Param(..)) => (),
69+
Some(Node::Local(local)) => {
70+
if let Some(ty) = local.ty {
71+
if matches!(ty.kind, TyKind::Infer) {
72+
return;
73+
}
74+
} else {
75+
return;
76+
}
77+
}
78+
_ => return,
79+
};
80+
7081
let mut app = Applicability::MachineApplicable;
7182
let rem_of = snippet_with_applicability(cx, expr3.span, "_", &mut app);
7283
span_lint_and_sugg(

tests/ui/manual_rem_euclid.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ fn main() {
2626
let _: i32 = 4 % ((value % 4) + 4);
2727
let _: i32 = ((4 % value) + 4) % 4;
2828
}
29+
30+
// Should lint for params too
31+
pub fn rem_euclid_4(num: i32) -> i32 {
32+
num.rem_euclid(4)
33+
}

tests/ui/manual_rem_euclid.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ fn main() {
2626
let _: i32 = 4 % ((value % 4) + 4);
2727
let _: i32 = ((4 % value) + 4) % 4;
2828
}
29+
30+
// Should lint for params too
31+
pub fn rem_euclid_4(num: i32) -> i32 {
32+
((num % 4) + 4) % 4
33+
}

tests/ui/manual_rem_euclid.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,11 @@ error: manual `rem_euclid` implementation
3030
LL | let _: i32 = 1 + (4 + value % 4) % 4;
3131
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `value.rem_euclid(4)`
3232

33-
error: aborting due to 5 previous errors
33+
error: manual `rem_euclid` implementation
34+
--> $DIR/manual_rem_euclid.rs:32:5
35+
|
36+
LL | ((num % 4) + 4) % 4
37+
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `num.rem_euclid(4)`
38+
39+
error: aborting due to 6 previous errors
3440

0 commit comments

Comments
 (0)