Auto merge of #11621 - GuillaumeGomez:needless_pass_by_ref_mut-closure-non-async-fn, r=blyxyas

Needless pass by ref mut closure non async fn

Fixes https://github.com/rust-lang/rust-clippy/issues/11620.
Fixes https://github.com/rust-lang/rust-clippy/issues/11561.

changelog: [`needless_pass_by_ref_mut`]: Correctly handle arguments moved into closure in non-async functions.

r? `@Centri3`
This commit is contained in:
bors 2023-10-19 12:04:07 +00:00
commit cd477d4b0c
2 changed files with 28 additions and 13 deletions

View File

@ -197,20 +197,21 @@ fn check_fn(
};
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<LocalDefId> = 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<LocalDefId> = 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();

View File

@ -288,6 +288,20 @@ fn get_mut_unchecked2<T>(ptr: &mut NonNull<Data<T>>) -> &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)
}
// Should not warn.
fn filter_copy<T: 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];