From 89698b9613179a1c025ce61fc17334a3ff315598 Mon Sep 17 00:00:00 2001 From: Nahua Kang Date: Thu, 4 Aug 2022 23:46:41 +0200 Subject: [PATCH] Extend and improve initial test cases for collapsible_str_replace --- tests/ui/collapsible_str_replace.rs | 54 +++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/tests/ui/collapsible_str_replace.rs b/tests/ui/collapsible_str_replace.rs index c91809bc840..0f4bdf09480 100644 --- a/tests/ui/collapsible_str_replace.rs +++ b/tests/ui/collapsible_str_replace.rs @@ -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");