From ec2b8ab83a5260013fd491255f60b806d3fe2084 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 6 Oct 2023 11:11:57 +0200 Subject: [PATCH 1/3] Fix invalid warning for closure in non-async function for `needless_pass_by_ref_mut` lint --- clippy_lints/src/needless_pass_by_ref_mut.rs | 27 ++++++++++---------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/clippy_lints/src/needless_pass_by_ref_mut.rs b/clippy_lints/src/needless_pass_by_ref_mut.rs index e7424ba7c..1d5874f6f 100644 --- a/clippy_lints/src/needless_pass_by_ref_mut.rs +++ b/clippy_lints/src/needless_pass_by_ref_mut.rs @@ -197,20 +197,21 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut<'tcx> { }; let infcx = cx.tcx.infer_ctxt().build(); euv::ExprUseVisitor::new(&mut ctx, &infcx, fn_def_id, cx.param_env, cx.typeck_results()).consume_body(body); + + let mut checked_closures = FxHashSet::default(); + + // We retrieve all the closures declared in the function because they will not be found + // by `euv::Delegate`. + let mut closures: FxHashSet = FxHashSet::default(); + for_each_expr_with_closures(cx, body, |expr| { + if let ExprKind::Closure(closure) = expr.kind { + closures.insert(closure.def_id); + } + ControlFlow::<()>::Continue(()) + }); + check_closures(&mut ctx, cx, &infcx, &mut checked_closures, closures); + if is_async { - let mut checked_closures = FxHashSet::default(); - - // We retrieve all the closures declared in the async function because they will - // not be found by `euv::Delegate`. - let mut closures: FxHashSet = FxHashSet::default(); - for_each_expr_with_closures(cx, body, |expr| { - if let ExprKind::Closure(closure) = expr.kind { - closures.insert(closure.def_id); - } - ControlFlow::<()>::Continue(()) - }); - check_closures(&mut ctx, cx, &infcx, &mut checked_closures, closures); - while !ctx.async_closures.is_empty() { let async_closures = ctx.async_closures.clone(); ctx.async_closures.clear(); From 3b4b07c5f87865b5a15286ac8f8dfaf05b92df1e Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 6 Oct 2023 11:12:37 +0200 Subject: [PATCH 2/3] Add test for closure in non-async function for `needless_pass_by_ref_mut` lint --- tests/ui/needless_pass_by_ref_mut.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/ui/needless_pass_by_ref_mut.rs b/tests/ui/needless_pass_by_ref_mut.rs index 7f642e53d..95fe1385c 100644 --- a/tests/ui/needless_pass_by_ref_mut.rs +++ b/tests/ui/needless_pass_by_ref_mut.rs @@ -288,6 +288,15 @@ fn get_mut_unchecked2(ptr: &mut NonNull>) -> &mut T { unsafe { &mut (*ptr.as_ptr()).value } } +fn set_true(b: &mut bool) { + *b = true; +} + +// Should not warn. +fn true_setter(b: &mut bool) -> impl FnOnce() + '_ { + move || set_true(b) +} + fn main() { let mut u = 0; let mut v = vec![0]; From 3e6db95e308593fac0b9f132454db44295a94b08 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 6 Oct 2023 11:24:35 +0200 Subject: [PATCH 3/3] Add regression test for #11561 --- tests/ui/needless_pass_by_ref_mut.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/ui/needless_pass_by_ref_mut.rs b/tests/ui/needless_pass_by_ref_mut.rs index 95fe1385c..ea5e74c4c 100644 --- a/tests/ui/needless_pass_by_ref_mut.rs +++ b/tests/ui/needless_pass_by_ref_mut.rs @@ -297,6 +297,11 @@ fn true_setter(b: &mut bool) -> impl FnOnce() + '_ { move || set_true(b) } +// Should not warn. +fn filter_copy(predicate: &mut impl FnMut(T) -> bool) -> impl FnMut(&T) -> bool + '_ { + move |&item| predicate(item) +} + fn main() { let mut u = 0; let mut v = vec![0];