Skip to content

New lint: collapsible_str_replace #6651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
camsteffen opened this issue Jan 29, 2021 · 3 comments · Fixed by #9269
Closed

New lint: collapsible_str_replace #6651

camsteffen opened this issue Jan 29, 2021 · 3 comments · Fixed by #9269
Assignees
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-perf Lint: Belongs in the perf lint group T-middle Type: Probably requires verifiying types

Comments

@camsteffen
Copy link
Contributor

camsteffen commented Jan 29, 2021

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.

@camsteffen camsteffen added A-lint Area: New lints L-perf Lint: Belongs in the perf lint group good-first-issue These issues are a good way to get started with Clippy T-middle Type: Probably requires verifiying types labels Jan 29, 2021
@nahuakang
Copy link
Contributor

This actually looks really fun. I'd like to move on to this issue 🙌

@xFrednet
Copy link
Member

xFrednet commented Feb 8, 2021

@nahuakang could you maybe assign your self by commenting @rustbot claim? This makes it easier to see that someone is working on this issue. You can unassign yourself at anytime if you want to stop working on this 🙃. It's nice to see you around, keep up the good work 👍

@nahuakang
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-perf Lint: Belongs in the perf lint group T-middle Type: Probably requires verifiying types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants