Skip to content

New lint: collapsible_str_replace #6651

Closed
@camsteffen

Description

@camsteffen

What it does

Checks for consecutive calls to str::replace (2 or more) that can be collapsed into a single call.

Categories (optional)

  • Kind: perf

What is the advantage of the recommended code over the original code

Faster since the string is only scanned once. Also less repetitive code.

Drawbacks

None.

Example

"hesuo worpd"
    .replace('s', "l")
    .replace("u", "l")
    .replace('p', "l")

Could be written as:

"hesuo worpd".replace(|c| matches!(c, 's' | 'u' | 'p'), "l")

Using matches! is potentially faster than a slice of chars (replace(&['s', 'u', 'p'], "l")). But if any of the chars are variables, the lint can fall back to suggesting a slice of chars.

Metadata

Metadata

Assignees

Labels

A-lintArea: New lintsL-perfLint: Belongs in the perf lint groupT-middleType: Probably requires verifiying typesgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions