Add lint manual_split_once
This commit is contained in:
parent
8cf6dae0ca
commit
a7f376fbd3
14 changed files with 459 additions and 26 deletions
50
tests/ui/manual_split_once.fixed
Normal file
50
tests/ui/manual_split_once.fixed
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
// run-rustfix
|
||||
|
||||
#![feature(custom_inner_attributes)]
|
||||
#![warn(clippy::manual_split_once)]
|
||||
#![allow(clippy::iter_skip_next, clippy::iter_nth_zero)]
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
50
tests/ui/manual_split_once.rs
Normal file
50
tests/ui/manual_split_once.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
// run-rustfix
|
||||
|
||||
#![feature(custom_inner_attributes)]
|
||||
#![warn(clippy::manual_split_once)]
|
||||
#![allow(clippy::iter_skip_next, clippy::iter_nth_zero)]
|
||||
|
||||
extern crate itertools;
|
||||
|
||||
#[allow(unused_imports)]
|
||||
use itertools::Itertools;
|
||||
|
||||
fn main() {
|
||||
let _ = "key=value".splitn(2, '=').next();
|
||||
let _ = "key=value".splitn(2, '=').nth(2);
|
||||
let _ = "key=value".splitn(2, '=').next().unwrap();
|
||||
let _ = "key=value".splitn(2, '=').nth(0).unwrap();
|
||||
let _ = "key=value".splitn(2, '=').nth(1).unwrap();
|
||||
let _ = "key=value".splitn(2, '=').skip(1).next().unwrap();
|
||||
let (_, _) = "key=value".splitn(2, '=').next_tuple().unwrap();
|
||||
|
||||
let s = String::from("key=value");
|
||||
let _ = s.splitn(2, '=').next().unwrap();
|
||||
|
||||
let s = Box::<str>::from("key=value");
|
||||
let _ = s.splitn(2, '=').nth(0).unwrap();
|
||||
|
||||
let s = &"key=value";
|
||||
let _ = s.splitn(2, '=').skip(0).next().unwrap();
|
||||
|
||||
fn _f(s: &str) -> Option<&str> {
|
||||
let _ = s.splitn(2, "key=value").next()?;
|
||||
let _ = s.splitn(2, "key=value").nth(1)?;
|
||||
let _ = s.splitn(2, "key=value").skip(1).next()?;
|
||||
None
|
||||
}
|
||||
|
||||
// Don't lint, slices don't have `split_once`
|
||||
let _ = [0, 1, 2].splitn(2, |&x| x == 1).nth(1).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".splitn(2, '=').nth(1).unwrap();
|
||||
}
|
||||
82
tests/ui/manual_split_once.stderr
Normal file
82
tests/ui/manual_split_once.stderr
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
error: manual implementation of `split_once`
|
||||
--> $DIR/manual_split_once.rs:13:13
|
||||
|
|
||||
LL | let _ = "key=value".splitn(2, '=').next();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `Some("key=value".split_once('=').map_or("key=value", |x| x.0))`
|
||||
|
|
||||
= note: `-D clippy::manual-split-once` implied by `-D warnings`
|
||||
|
||||
error: manual implementation of `split_once`
|
||||
--> $DIR/manual_split_once.rs:15:13
|
||||
|
|
||||
LL | let _ = "key=value".splitn(2, '=').next().unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"key=value".split_once('=').map_or("key=value", |x| x.0)`
|
||||
|
||||
error: manual implementation of `split_once`
|
||||
--> $DIR/manual_split_once.rs:16:13
|
||||
|
|
||||
LL | let _ = "key=value".splitn(2, '=').nth(0).unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"key=value".split_once('=').map_or("key=value", |x| x.0)`
|
||||
|
||||
error: manual implementation of `split_once`
|
||||
--> $DIR/manual_split_once.rs:17:13
|
||||
|
|
||||
LL | let _ = "key=value".splitn(2, '=').nth(1).unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"key=value".split_once('=').unwrap().1`
|
||||
|
||||
error: manual implementation of `split_once`
|
||||
--> $DIR/manual_split_once.rs:18:13
|
||||
|
|
||||
LL | let _ = "key=value".splitn(2, '=').skip(1).next().unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"key=value".split_once('=').unwrap().1`
|
||||
|
||||
error: manual implementation of `split_once`
|
||||
--> $DIR/manual_split_once.rs:19:18
|
||||
|
|
||||
LL | let (_, _) = "key=value".splitn(2, '=').next_tuple().unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"key=value".split_once('=')`
|
||||
|
||||
error: manual implementation of `split_once`
|
||||
--> $DIR/manual_split_once.rs:22:13
|
||||
|
|
||||
LL | let _ = s.splitn(2, '=').next().unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.split_once('=').map_or(&*s, |x| x.0)`
|
||||
|
||||
error: manual implementation of `split_once`
|
||||
--> $DIR/manual_split_once.rs:25:13
|
||||
|
|
||||
LL | let _ = s.splitn(2, '=').nth(0).unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.split_once('=').map_or(&*s, |x| x.0)`
|
||||
|
||||
error: manual implementation of `split_once`
|
||||
--> $DIR/manual_split_once.rs:28:13
|
||||
|
|
||||
LL | let _ = s.splitn(2, '=').skip(0).next().unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.split_once('=').map_or(*s, |x| x.0)`
|
||||
|
||||
error: manual implementation of `split_once`
|
||||
--> $DIR/manual_split_once.rs:31:17
|
||||
|
|
||||
LL | let _ = s.splitn(2, "key=value").next()?;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.split_once("key=value").map_or(s, |x| x.0)`
|
||||
|
||||
error: manual implementation of `split_once`
|
||||
--> $DIR/manual_split_once.rs:32:17
|
||||
|
|
||||
LL | let _ = s.splitn(2, "key=value").nth(1)?;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.split_once("key=value")?.1`
|
||||
|
||||
error: manual implementation of `split_once`
|
||||
--> $DIR/manual_split_once.rs:33:17
|
||||
|
|
||||
LL | let _ = s.splitn(2, "key=value").skip(1).next()?;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s.split_once("key=value")?.1`
|
||||
|
||||
error: manual implementation of `split_once`
|
||||
--> $DIR/manual_split_once.rs:49:13
|
||||
|
|
||||
LL | let _ = "key=value".splitn(2, '=').nth(1).unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"key=value".split_once('=').unwrap().1`
|
||||
|
||||
error: aborting due to 13 previous errors
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue