Skip to content

Commit cd477d4

Browse files
committed
Auto merge of rust-lang#11621 - GuillaumeGomez:needless_pass_by_ref_mut-closure-non-async-fn, r=blyxyas
Needless pass by ref mut closure non async fn Fixes rust-lang/rust-clippy#11620. Fixes rust-lang/rust-clippy#11561. changelog: [`needless_pass_by_ref_mut`]: Correctly handle arguments moved into closure in non-async functions. r? `@Centri3`
2 parents 9574d28 + 3e6db95 commit cd477d4

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

clippy_lints/src/needless_pass_by_ref_mut.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,21 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut<'tcx> {
197197
};
198198
let infcx = cx.tcx.infer_ctxt().build();
199199
euv::ExprUseVisitor::new(&mut ctx, &infcx, fn_def_id, cx.param_env, cx.typeck_results()).consume_body(body);
200-
if is_async {
201-
let mut checked_closures = FxHashSet::default();
202-
203-
// We retrieve all the closures declared in the async function because they will
204-
// not be found by `euv::Delegate`.
205-
let mut closures: FxHashSet<LocalDefId> = FxHashSet::default();
206-
for_each_expr_with_closures(cx, body, |expr| {
207-
if let ExprKind::Closure(closure) = expr.kind {
208-
closures.insert(closure.def_id);
209-
}
210-
ControlFlow::<()>::Continue(())
211-
});
212-
check_closures(&mut ctx, cx, &infcx, &mut checked_closures, closures);
213200

201+
let mut checked_closures = FxHashSet::default();
202+
203+
// We retrieve all the closures declared in the function because they will not be found
204+
// by `euv::Delegate`.
205+
let mut closures: FxHashSet<LocalDefId> = FxHashSet::default();
206+
for_each_expr_with_closures(cx, body, |expr| {
207+
if let ExprKind::Closure(closure) = expr.kind {
208+
closures.insert(closure.def_id);
209+
}
210+
ControlFlow::<()>::Continue(())
211+
});
212+
check_closures(&mut ctx, cx, &infcx, &mut checked_closures, closures);
213+
214+
if is_async {
214215
while !ctx.async_closures.is_empty() {
215216
let async_closures = ctx.async_closures.clone();
216217
ctx.async_closures.clear();

tests/ui/needless_pass_by_ref_mut.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,20 @@ fn get_mut_unchecked2<T>(ptr: &mut NonNull<Data<T>>) -> &mut T {
288288
unsafe { &mut (*ptr.as_ptr()).value }
289289
}
290290

291+
fn set_true(b: &mut bool) {
292+
*b = true;
293+
}
294+
295+
// Should not warn.
296+
fn true_setter(b: &mut bool) -> impl FnOnce() + '_ {
297+
move || set_true(b)
298+
}
299+
300+
// Should not warn.
301+
fn filter_copy<T: Copy>(predicate: &mut impl FnMut(T) -> bool) -> impl FnMut(&T) -> bool + '_ {
302+
move |&item| predicate(item)
303+
}
304+
291305
fn main() {
292306
let mut u = 0;
293307
let mut v = vec![0];

0 commit comments

Comments
 (0)