Fix case for function params

This commit is contained in:
Evan Typanski 2022-06-22 14:08:47 -04:00
parent c8df6d6970
commit 90f8277fe3
4 changed files with 33 additions and 6 deletions

View file

@ -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(

View file

@ -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)
}

View file

@ -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
}

View file

@ -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