rust-lang / rust-lang/rust-clippy
Lint for needless use of `Cow`
Open
Nobody has claimed this yet.
A-lint
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
When a Cow's Borrowed variant is only ever used for empty strings, clippy should suggest to remove the Cow and use String::new() instead.
Advantage
String::new()allocates no memory, and is essentially the same speed: https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=93a8649afa1b55fc84cef2829920a932 (Less instructions doesn't mean faster, but imo, even if it's slower it's worth the ergonomics)- It's simpler.
Drawbacks
- It may actually end up being ever so slightly slower. In some cases this may be undesirable.
Example
if let Some(x) = x {
x.to_string().into()
} else if let Some(y) = y {
Cow::Borrowed("")
} else {
"".into()
}
Could be written as:
if let Some(x) = x {
x.to_string()
} else if let Some(y) = y {
String::new()
} else {
String::new()
}
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
The issue names no files or tests. Start by searching rust-lang/rust-clippy for existing Cow-related lints and their tests, then compare the proposed examples with current lint behavior. Done means the described needless Cow cases are recognized and the suggested String::new() replacements are covered by regression tests.
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