rust-lang / rust-lang/rust-clippy

Lint for needless use of `Cow`

Open
#11,328 0 comments 0 reactions 0 assignees View on GitHub

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
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.