rust-clippy/tests/ui/manual_split_once.fixed
surechen c051656c83 fixes: #7889
1. Fix the problem of manual_split_once changing the original behavior.
2. Add a new lint needless_splitn.

changelog: Fix the problem of manual_split_once changing the original behavior and add a new lint needless_splitn.
2021-11-17 08:38:45 +08:00

56 lines
1.8 KiB
Rust

// run-rustfix
#![feature(custom_inner_attributes)]
#![warn(clippy::manual_split_once)]
#![allow(clippy::iter_skip_next, clippy::iter_nth_zero, clippy::needless_splitn)]
extern crate itertools;
#[allow(unused_imports)]
use itertools::Itertools;
fn main() {
let _ = Some("key=value".split_once('=').map_or("key=value", |x| x.0));
let _ = "key=value".splitn(2, '=').nth(2);
let _ = "key=value".split_once('=').map_or("key=value", |x| x.0);
let _ = "key=value".split_once('=').map_or("key=value", |x| x.0);
let _ = "key=value".split_once('=').unwrap().1;
let _ = "key=value".split_once('=').unwrap().1;
let (_, _) = "key=value".split_once('=').unwrap();
let s = String::from("key=value");
let _ = s.split_once('=').map_or(&*s, |x| x.0);
let s = Box::<str>::from("key=value");
let _ = s.split_once('=').map_or(&*s, |x| x.0);
let s = &"key=value";
let _ = s.split_once('=').map_or(*s, |x| x.0);
fn _f(s: &str) -> Option<&str> {
let _ = s.split_once("key=value").map_or(s, |x| x.0);
let _ = s.split_once("key=value")?.1;
let _ = s.split_once("key=value")?.1;
None
}
// Don't lint, slices don't have `split_once`
let _ = [0, 1, 2].splitn(2, |&x| x == 1).nth(1).unwrap();
// `rsplitn` gives the results in the reverse order of `rsplit_once`
let _ = "key=value".rsplitn(2, '=').next().unwrap();
let _ = "key=value".rsplit_once('=').unwrap().0;
let _ = "key=value".rsplit_once('=').map(|x| x.1);
let (_, _) = "key=value".rsplit_once('=').map(|(x, y)| (y, x)).unwrap();
}
fn _msrv_1_51() {
#![clippy::msrv = "1.51"]
// `str::split_once` was stabilized in 1.16. Do not lint this
let _ = "key=value".splitn(2, '=').nth(1).unwrap();
}
fn _msrv_1_52() {
#![clippy::msrv = "1.52"]
let _ = "key=value".split_once('=').unwrap().1;
}