Skip to content

Commit 50139e6

Browse files
committed
Auto merge of #11551 - Meczka:fix-fp-needless-pass-by-ref-mut, r=xFrednet
fixed fp caused by moving &mut reference inside of a closure changelog: [`needless_pass_by_ref mut`]: fixes false positive caused by not covering mutable references passed to a closure inside of a fuction fixes #11545
2 parents 33f084e + ab51f66 commit 50139e6

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

clippy_lints/src/needless_pass_by_ref_mut.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,12 @@ impl<'tcx> euv::Delegate<'tcx> for MutablyUsedVariablesCtxt<'tcx> {
336336
fn borrow(&mut self, cmt: &euv::PlaceWithHirId<'tcx>, _id: HirId, borrow: ty::BorrowKind) {
337337
self.prev_bind = None;
338338
if let euv::Place {
339-
base: euv::PlaceBase::Local(vid),
339+
base:
340+
euv::PlaceBase::Local(vid)
341+
| euv::PlaceBase::Upvar(UpvarId {
342+
var_path: UpvarPath { hir_id: vid },
343+
..
344+
}),
340345
base_ty,
341346
..
342347
} = &cmt.place

tests/ui/needless_pass_by_ref_mut.rs

+12
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,18 @@ async fn async_vec(b: &mut Vec<bool>) {
230230
async fn async_vec2(b: &mut Vec<bool>) {
231231
b.push(true);
232232
}
233+
fn non_mut(n: &str) {}
234+
//Should warn
235+
pub async fn call_in_closure1(n: &mut str) {
236+
(|| non_mut(n))()
237+
}
238+
fn str_mut(str: &mut String) -> bool {
239+
str.pop().is_some()
240+
}
241+
//Should not warn
242+
pub async fn call_in_closure2(str: &mut String) {
243+
(|| str_mut(str))();
244+
}
233245

234246
// Should not warn.
235247
pub async fn closure(n: &mut usize) -> impl '_ + FnMut() {

tests/ui/needless_pass_by_ref_mut.stderr

+12-4
Original file line numberDiff line numberDiff line change
@@ -108,28 +108,36 @@ LL | async fn inner_async3(x: &mut i32, y: &mut u32) {
108108
| ^^^^^^^^ help: consider changing to: `&i32`
109109

110110
error: this argument is a mutable reference, but not used mutably
111-
--> $DIR/needless_pass_by_ref_mut.rs:235:25
111+
--> $DIR/needless_pass_by_ref_mut.rs:235:34
112+
|
113+
LL | pub async fn call_in_closure1(n: &mut str) {
114+
| ^^^^^^^^ help: consider changing to: `&str`
115+
|
116+
= warning: changing this function will impact semver compatibility
117+
118+
error: this argument is a mutable reference, but not used mutably
119+
--> $DIR/needless_pass_by_ref_mut.rs:247:25
112120
|
113121
LL | pub async fn closure(n: &mut usize) -> impl '_ + FnMut() {
114122
| ^^^^^^^^^^ help: consider changing to: `&usize`
115123
|
116124
= warning: changing this function will impact semver compatibility
117125

118126
error: this argument is a mutable reference, but not used mutably
119-
--> $DIR/needless_pass_by_ref_mut.rs:242:20
127+
--> $DIR/needless_pass_by_ref_mut.rs:254:20
120128
|
121129
LL | pub fn closure2(n: &mut usize) -> impl '_ + FnMut() -> usize {
122130
| ^^^^^^^^^^ help: consider changing to: `&usize`
123131
|
124132
= warning: changing this function will impact semver compatibility
125133

126134
error: this argument is a mutable reference, but not used mutably
127-
--> $DIR/needless_pass_by_ref_mut.rs:253:26
135+
--> $DIR/needless_pass_by_ref_mut.rs:265:26
128136
|
129137
LL | pub async fn closure4(n: &mut usize) {
130138
| ^^^^^^^^^^ help: consider changing to: `&usize`
131139
|
132140
= warning: changing this function will impact semver compatibility
133141

134-
error: aborting due to 20 previous errors
142+
error: aborting due to 21 previous errors
135143

0 commit comments

Comments
 (0)