rust/tests/ui/collapsible_str_replace.fixed

66 lines
1.7 KiB
Rust
Raw Normal View History

2022-08-08 15:31:53 -05:00
// run-rustfix
#![warn(clippy::collapsible_str_replace)]
fn get_filter() -> &'static str {
"u"
}
fn main() {
let misspelled = "hesuo worpd";
let p = 'p';
let s = 's';
let u = 'u';
let l = "l";
// LINT CASES
let _ = misspelled.replace(|c| matches!(c, 'u' | 's'), "l");
let _ = misspelled.replace(|c| matches!(c, 'u' | 's'), l);
let _ = misspelled.replace(|c| matches!(c, 'p' | 'u' | 's'), "l");
let _ = misspelled
.replace(|c| matches!(c, 'd' | 'p' | 'u' | 's'), "l");
// FALLBACK CASES
// If there are consecutive calls to `str::replace` and all or any chars are variables,
// recommend the fallback `misspelled.replace(&[s, u, p], "l")`
let _ = misspelled.replace(&['u' , s], "l");
let _ = misspelled.replace(&['p' , 'u' , s], "l");
let _ = misspelled.replace(&['p' , u , s], "l");
let _ = misspelled.replace(&[p , u , s], "l");
// NO LINT CASES
let _ = misspelled.replace('s', "l");
let _ = misspelled.replace(s, "l");
// If the consecutive `str::replace` calls have different `to` arguments, do not lint
let _ = misspelled.replace('s', "l").replace('u', "p");
let _ = misspelled.replace(&get_filter(), "l");
let _ = misspelled.replace(&['s', 'u', 'p'], "l");
let _ = misspelled.replace(&['s', 'u', 'p'], l);
let _ = misspelled.replace(&['s', 'u'], "l").replace(&['u', 'p'], "l");
let _ = misspelled.replace('s', "l").replace(&['u', 'p'], "l");
let _ = misspelled.replace(&['s', 'u'], "l").replace('p', "l");
let _ = misspelled.replace(&['s', u, 'p'], "l");
let _ = misspelled.replace(&[s, u, 'p'], "l");
let _ = misspelled.replace(&[s, u, p], "l");
let _ = misspelled.replace(&[s, u], "l").replace(&[u, p], "l");
}