refactor(mut_reference): replace match with if-let to reduce nesting (#14890)

One downside to this is that, since the patterns matched against (on the
left) are quite long, it's a bit difficult to see what's even getting
matched. Using `matches!()` could help with that

changelog: none
This commit is contained in:
Timo 2025-05-26 10:13:33 +00:00 committed by GitHub
commit 32a3744efc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -79,25 +79,19 @@ fn check_arguments<'tcx>(
name: &str,
fn_kind: &str,
) {
match type_definition.kind() {
ty::FnDef(..) | ty::FnPtr(..) => {
let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs();
for (argument, parameter) in iter::zip(arguments, parameters) {
match parameter.kind() {
ty::Ref(_, _, Mutability::Not) | ty::RawPtr(_, Mutability::Not) => {
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, _) = argument.kind {
span_lint(
cx,
UNNECESSARY_MUT_PASSED,
argument.span,
format!("the {fn_kind} `{name}` doesn't need a mutable reference"),
);
}
},
_ => (),
}
if let ty::FnDef(..) | ty::FnPtr(..) = type_definition.kind() {
let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs();
for (argument, parameter) in iter::zip(arguments, parameters) {
if let ty::Ref(_, _, Mutability::Not) | ty::RawPtr(_, Mutability::Not) = parameter.kind()
&& let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, _) = argument.kind
{
span_lint(
cx,
UNNECESSARY_MUT_PASSED,
argument.span,
format!("the {fn_kind} `{name}` doesn't need a mutable reference"),
);
}
},
_ => (),
}
}
}