Skip to content

swap_with_temporary mis-detects MutexGuards as temporaries #15166

@TheBlueMatt

Description

@TheBlueMatt

Summary

swap_with_temporary fires when you swap (the deref'd object behind) a MutexGuard with a stack variable as it incorrectly thinks the MutexGuard is a temporary.

Lint Name

swap_with_temporary

Reproducer

We have a bunch of cases like the following which now trigger the lint:

struct A {
   thing: Mutex<Vec<u8>>,
}
impl A {
   fn a(&self) {
      let new_vec = vec![42];
      std::mem::swap(&mut self.thing.lock().unwrap(), new_vec);
      for v in new_vec {
        // Do something with v
      }
   }

Version


Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-bugIssue: The suggestion compiles but changes the code to behave in an unintended way

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions