Extend and improve initial test cases for collapsible_str_replace

This commit is contained in:
Nahua Kang 2022-08-04 23:46:41 +02:00
parent a4413f75bf
commit 89698b9613

View File

@ -1,5 +1,9 @@
#![warn(clippy::collapsible_str_replace)]
fn get_filter() -> &'static str {
"u"
}
fn main() {
let misspelled = "hesuo worpd";
@ -7,11 +11,45 @@ fn main() {
let s = 's';
let u = 'u';
// LINT CASES
// If the first argument to a single `str::replace` call is a slice and none of the chars
// are variables, recommend `collapsible_str_replace`
let replacement = misspelled.replace(&['s', 'u', 'p'], "l");
println!("{replacement}");
// If there are consecutive calls to `str::replace` and none of the chars are variables,
// recommend `collapsible_str_replace`
let replacement = misspelled.replace('s', "l").replace('u', "l");
println!("{replacement}");
let replacement = misspelled.replace('s', "l").replace('u', "l").replace('p', "l");
println!("{replacement}");
// NO LINT CASES
// If there is a single call to `str::replace` and the first argument is a char or a variable,
// do not recommend `collapsible_str_replace`
let replacement = misspelled.replace('s', "l");
println!("{replacement}");
let replacement = misspelled.replace(s, "l");
println!("{replacement}");
// If the `from` argument is of kind other than a slice or a char, do not lint
let replacement = misspelled.replace(&get_filter(), "l");
// NO LINT TIL IMPROVEMENT
// If multiple `str::replace` calls contain slices and none of the chars are variables,
// the first iteration does not recommend `collapsible_str_replace`
let replacement = misspelled.replace(&['s', 'u', 'p'], "l").replace(&['s', 'p'], "l");
println!("{replacement}");
// If a mixture of `str::replace` calls with slice and char arguments are used for `from` arg,
// the first iteration does not recommend `collapsible_str_replace`
let replacement = misspelled.replace(&['s', 'u'], "l").replace('p', "l");
println!("replacement");
let replacement = misspelled.replace('p', "l").replace(&['s', 'u'], "l");
// The first iteration of `collapsible_str_replace` will not create lint if the first argument to
// a single `str::replace` call is a slice and one or more of its chars are variables
let replacement = misspelled.replace(&['s', u, 'p'], "l");
@ -23,22 +61,10 @@ fn main() {
let replacement = misspelled.replace(&[s, u, p], "l");
println!("{replacement}");
// If there is a single call to `str::replace` and the first argument is a char or a variable, don't
// not recommend `collapsible_str_replace`
let replacement = misspelled.replace('s', "l");
println!("{replacement}");
let replacement = misspelled.replace(s, "l");
println!("{replacement}");
// If there are consecutive calls to `str::replace` and none of the chars are variables,
// recommend `collapsible_str_replace`
let replacement = misspelled.replace('s', "l").replace('u', "l");
println!("{replacement}");
let replacement = misspelled.replace('s', "l").replace('u', "l").replace('p', "l");
let replacement = misspelled.replace(&[s, u], "l").replace(&[u, p], "l");
println!("{replacement}");
// 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 replacement = misspelled.replace(s, "l").replace('u', "l");