rust-lang / rust-lang/rust-clippy
redundant_clone: suggestion breaks code if cloned value would be dropped immediately (not bound to a var)
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Lint name: redundant_clone
Inside rust analyzer I found code like this:
pub fn bad() -> String {
let s = String::new();
s.to_string();
s
}
warning: redundant clone
--> src/main.rs:7:6
|
7 | s.to_string();
| ^^^^^^^^^^^^ help: remove this
|
= note: `#[warn(clippy::redundant_clone)]` on by default
note: cloned value is neither consumed nor mutated
--> src/main.rs:7:5
|
7 | s.to_string();
| ^^^^^^^^^^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
Clippy correctly detects the redundant clone and suggests removing it, but it only suggests removing the clone()ing.
The problem is we do not bind the cloned value to anything here and replacing s.to_string(); with s; drops s and it can no longer be returned. 😆
pub fn bad() -> String {
let s = String::new();
s;
s
}
In such a case (clone that is neither bound to a variable nor returned) I think we can remove the entire statement, can't we?
Meta
clippy 0.1.55 (798baeb 2021-07-02)
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 by locating the redundant_clone lint and reproducing the provided s.to_string(); example. Check the suggested replacement when the cloned value is an unbound expression, and verify that the resulting code still returns s without dropping it prematurely.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100