2020-09-14 04:11:35 +00:00
|
|
|
#![warn(clippy::manual_strip)]
|
2023-07-27 11:40:22 +00:00
|
|
|
//@no-rustfix
|
2020-09-14 04:11:35 +00:00
|
|
|
fn main() {
|
|
|
|
let s = "abc";
|
|
|
|
|
|
|
|
if s.starts_with("ab") {
|
|
|
|
str::to_string(&s["ab".len()..]);
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: stripping a prefix manually
|
2020-09-14 04:11:35 +00:00
|
|
|
s["ab".len()..].to_string();
|
|
|
|
|
|
|
|
str::to_string(&s[2..]);
|
|
|
|
s[2..].to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.ends_with("bc") {
|
|
|
|
str::to_string(&s[..s.len() - "bc".len()]);
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: stripping a suffix manually
|
2020-09-14 04:11:35 +00:00
|
|
|
s[..s.len() - "bc".len()].to_string();
|
|
|
|
|
|
|
|
str::to_string(&s[..s.len() - 2]);
|
|
|
|
s[..s.len() - 2].to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Character patterns
|
|
|
|
if s.starts_with('a') {
|
|
|
|
str::to_string(&s[1..]);
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: stripping a prefix manually
|
2020-09-14 04:11:35 +00:00
|
|
|
s[1..].to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variable prefix
|
|
|
|
let prefix = "ab";
|
|
|
|
if s.starts_with(prefix) {
|
|
|
|
str::to_string(&s[prefix.len()..]);
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: stripping a prefix manually
|
2020-09-14 04:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Constant prefix
|
|
|
|
const PREFIX: &str = "ab";
|
|
|
|
if s.starts_with(PREFIX) {
|
|
|
|
str::to_string(&s[PREFIX.len()..]);
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: stripping a prefix manually
|
2020-09-14 04:11:35 +00:00
|
|
|
str::to_string(&s[2..]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constant target
|
|
|
|
const TARGET: &str = "abc";
|
|
|
|
if TARGET.starts_with(prefix) {
|
|
|
|
str::to_string(&TARGET[prefix.len()..]);
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: stripping a prefix manually
|
2020-09-14 04:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// String target - not mutated.
|
|
|
|
let s1: String = "abc".into();
|
|
|
|
if s1.starts_with("ab") {
|
|
|
|
s1[2..].to_uppercase();
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: stripping a prefix manually
|
2020-09-14 04:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// String target - mutated. (Don't lint.)
|
|
|
|
let mut s2: String = "abc".into();
|
|
|
|
if s2.starts_with("ab") {
|
|
|
|
s2.push('d');
|
|
|
|
s2[2..].to_uppercase();
|
|
|
|
}
|
2020-09-15 19:30:01 +00:00
|
|
|
|
|
|
|
// Target not stripped. (Don't lint.)
|
|
|
|
let s3 = String::from("abcd");
|
|
|
|
let s4 = String::from("efgh");
|
|
|
|
if s3.starts_with("ab") {
|
|
|
|
s4[2..].to_string();
|
|
|
|
}
|
2020-09-14 04:11:35 +00:00
|
|
|
}
|
2022-10-21 21:35:39 +00:00
|
|
|
|
2022-11-19 12:50:02 +00:00
|
|
|
#[clippy::msrv = "1.44"]
|
2022-10-21 21:35:39 +00:00
|
|
|
fn msrv_1_44() {
|
|
|
|
let s = "abc";
|
|
|
|
if s.starts_with('a') {
|
|
|
|
s[1..].to_string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 12:50:02 +00:00
|
|
|
#[clippy::msrv = "1.45"]
|
2022-10-21 21:35:39 +00:00
|
|
|
fn msrv_1_45() {
|
|
|
|
let s = "abc";
|
|
|
|
if s.starts_with('a') {
|
|
|
|
s[1..].to_string();
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: stripping a prefix manually
|
2022-10-21 21:35:39 +00:00
|
|
|
}
|
|
|
|
}
|