Consecutive replace calls sharing one replacement
ID |
rust.performance.collapsible_str_replace |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Inefficient Api Usage |
Language |
Rust |
Description
Each replace walks the whole string and allocates a fresh String. When consecutive calls substitute the same text, pass the patterns together as a slice — replace(['<', '>'], "") — so the string is scanned and allocated once.
Rationale
Each replace walks the whole string and allocates a fresh String. When consecutive calls substitute the same text, pass the patterns together as a slice — replace(['<', '>'], "") — so the string is scanned and allocated once.
The following code illustrates the pattern detected by this rule:
fn sanitize(input: &str) -> String {
// FLAGGED: Consecutive replace calls sharing one replacement
input.replace('<', "").replace('>', "")
}