rust-lang / rust-lang/rust-clippy
`ManuallyDrop<T>::clone` should trigger a lint
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
ManuallyDrop implements Clone when T: Clone, but I've found that this is often not what I want to do. For example, I may have code that looks like this:
struct MyStruct {
state: Rc<State>,
}
...
fn spawn_state_task(data: &MyStuct) {
let state = data.state.clone();
spawn(async move { state.do_something() });
}
In the future, I change state to ManuallyDrop<Rc<X>> and update the Drop impl correctly, but neglect to update spawn_state_task(). The code continues to compile, but we've got a memory leak.
If you wish to clone the value inside of a ManuallyDrop and continue controlling the drop lifetime manually, you should do it via the explicit ManuallyDrop::clone call rather than .clone() or Clone::clone, both of which may mask a memory leak. If you wish to clone the object and return to automatic drop lifetime management, you should use (*value).clone() instead to deref the ManuallyDrop first.
Advantage
- Explicit detection of potential memory leak sites when switching
TtoManuallyDrop<T>whereT: Clone - Clear display of sites where a new
ManuallyDropstruct is creates from a clone, and require an additional drop call
Drawbacks
- This is more verbose in the case where you actually want to clone the
ManuallyDrop
Example
struct MyStruct {
state: Rc<State>,
}
fn spawn_state_task(data: &MyStuct) {
let state = data.state.clone();
spawn(async move { state.do_something() });
}
Could be written as:
struct MyStruct {
state: Rc<State>,
}
fn spawn_state_task(data: &MyStuct) {
let state = (*data.state).clone();
spawn(async move { state.do_something() });
}
or
struct MyStruct {
state: Rc<State>,
}
fn spawn_state_task(data: &MyStuct) {
let state = ManuallyDrop::clone(data.state);
spawn(async move { state.do_something() });
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Use the issue's ManuallyDrop::clone examples as the behavioral reference, and inspect Clippy's existing lint conventions before choosing an implementation location. Done means the relevant implicit clone forms trigger a lint while explicit ManuallyDrop::clone and dereferenced cloning remain valid, with coverage for the examples described here.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100