Closed
Description
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.