rust-lang / rust-lang/rust-clippy
assigning_clones: try to use dropped value
Open
Nobody has claimed this yet.
C-bug
I-false-positive
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
Consider we have a mut x and want to do x = y.to_owned()/x = y.clone(), but x is temporarily dropped, then we can't use clone_into/clone_from.
Lint Name
assigning_clones
Reproducer
I tried this code:
#![allow(dead_code)]
#![warn(clippy::assigning_clones)]
fn f(mut x: String, y: &str) -> String {
drop(x);
x = y.to_owned();
x
}
fn main() {}
I saw this happen:
warning: assigning the result of `ToOwned::to_owned()` may be inefficient
--> 1.rs:6:2
|
6 | x = y.to_owned();
| ^^^^^^^^^^^^^^^^ help: use `clone_into()`: `y.clone_into(&mut x)`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#assigning_clones
note: the lint level is defined here
--> 1.rs:2:9
|
2 | #![warn(clippy::assigning_clones)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
warning: 1 warning emitted
I expected to see this happen:
<nothing>
Version
rustc 1.81.0-nightly (0c81f94b9 2024-07-10)
binary: rustc
commit-hash: 0c81f94b9a6207fb1fc080caa83584dea2d71fc6
commit-date: 2024-07-10
host: aarch64-apple-darwin
release: 1.81.0-nightly
LLVM version: 18.1.7
Additional Labels
No response
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
Start with the assigning_clones lint and reproduce the warning using the Rust example in the issue. Trace how the lint handles an assignment after x is dropped, then add or update coverage for this case. Done means the reproducer no longer emits the lint warning while valid clone assignments remain covered.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100