about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-09-22 21:07:46 +0000
committerbors <bors@rust-lang.org>2023-09-22 21:07:46 +0000
commit50139e6ad282b153439dd2ae6bf380484f60dfbf (patch)
tree07faebae11d9422bd6c8edca1566bc126e32cd37
parent33f084ef78f1424241c59a69959d5a7bad0e4700 (diff)
parentab51f66ec087bfa0159ae19c699cb65049e48efa (diff)
downloadrust-50139e6ad282b153439dd2ae6bf380484f60dfbf.tar.gz
rust-50139e6ad282b153439dd2ae6bf380484f60dfbf.zip
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
-rw-r--r--clippy_lints/src/needless_pass_by_ref_mut.rs7
-rw-r--r--tests/ui/needless_pass_by_ref_mut.rs12
-rw-r--r--tests/ui/needless_pass_by_ref_mut.stderr16
3 files changed, 30 insertions, 5 deletions
diff --git a/clippy_lints/src/needless_pass_by_ref_mut.rs b/clippy_lints/src/needless_pass_by_ref_mut.rs
index 36ca61c0b44..57652e5ff54 100644
--- a/clippy_lints/src/needless_pass_by_ref_mut.rs
+++ b/clippy_lints/src/needless_pass_by_ref_mut.rs
@@ -336,7 +336,12 @@ impl<'tcx> euv::Delegate<'tcx> for MutablyUsedVariablesCtxt<'tcx> {
     fn borrow(&mut self, cmt: &euv::PlaceWithHirId<'tcx>, _id: HirId, borrow: ty::BorrowKind) {
         self.prev_bind = None;
         if let euv::Place {
-            base: euv::PlaceBase::Local(vid),
+            base:
+                euv::PlaceBase::Local(vid)
+                | euv::PlaceBase::Upvar(UpvarId {
+                    var_path: UpvarPath { hir_id: vid },
+                    ..
+                }),
             base_ty,
             ..
         } = &cmt.place
diff --git a/tests/ui/needless_pass_by_ref_mut.rs b/tests/ui/needless_pass_by_ref_mut.rs
index da2a72caacb..9cddcb3df23 100644
--- a/tests/ui/needless_pass_by_ref_mut.rs
+++ b/tests/ui/needless_pass_by_ref_mut.rs
@@ -230,6 +230,18 @@ async fn async_vec(b: &mut Vec<bool>) {
 async fn async_vec2(b: &mut Vec<bool>) {
     b.push(true);
 }
+fn non_mut(n: &str) {}
+//Should warn
+pub async fn call_in_closure1(n: &mut str) {
+    (|| non_mut(n))()
+}
+fn str_mut(str: &mut String) -> bool {
+    str.pop().is_some()
+}
+//Should not warn
+pub async fn call_in_closure2(str: &mut String) {
+    (|| str_mut(str))();
+}
 
 // Should not warn.
 pub async fn closure(n: &mut usize) -> impl '_ + FnMut() {
diff --git a/tests/ui/needless_pass_by_ref_mut.stderr b/tests/ui/needless_pass_by_ref_mut.stderr
index 0fb9458d794..0c7fbd5df6d 100644
--- a/tests/ui/needless_pass_by_ref_mut.stderr
+++ b/tests/ui/needless_pass_by_ref_mut.stderr
@@ -108,7 +108,15 @@ LL | async fn inner_async3(x: &mut i32, y: &mut u32) {
    |                          ^^^^^^^^ help: consider changing to: `&i32`
 
 error: this argument is a mutable reference, but not used mutably
-  --> $DIR/needless_pass_by_ref_mut.rs:235:25
+  --> $DIR/needless_pass_by_ref_mut.rs:235:34
+   |
+LL | pub async fn call_in_closure1(n: &mut str) {
+   |                                  ^^^^^^^^ help: consider changing to: `&str`
+   |
+   = warning: changing this function will impact semver compatibility
+
+error: this argument is a mutable reference, but not used mutably
+  --> $DIR/needless_pass_by_ref_mut.rs:247:25
    |
 LL | pub async fn closure(n: &mut usize) -> impl '_ + FnMut() {
    |                         ^^^^^^^^^^ help: consider changing to: `&usize`
@@ -116,7 +124,7 @@ LL | pub async fn closure(n: &mut usize) -> impl '_ + FnMut() {
    = warning: changing this function will impact semver compatibility
 
 error: this argument is a mutable reference, but not used mutably
-  --> $DIR/needless_pass_by_ref_mut.rs:242:20
+  --> $DIR/needless_pass_by_ref_mut.rs:254:20
    |
 LL | pub fn closure2(n: &mut usize) -> impl '_ + FnMut() -> usize {
    |                    ^^^^^^^^^^ help: consider changing to: `&usize`
@@ -124,12 +132,12 @@ LL | pub fn closure2(n: &mut usize) -> impl '_ + FnMut() -> usize {
    = warning: changing this function will impact semver compatibility
 
 error: this argument is a mutable reference, but not used mutably
-  --> $DIR/needless_pass_by_ref_mut.rs:253:26
+  --> $DIR/needless_pass_by_ref_mut.rs:265:26
    |
 LL | pub async fn closure4(n: &mut usize) {
    |                          ^^^^^^^^^^ help: consider changing to: `&usize`
    |
    = warning: changing this function will impact semver compatibility
 
-error: aborting due to 20 previous errors
+error: aborting due to 21 previous errors