Auto merge of #7292 - Jarcho:suspicious_splitn, r=flip1995

Add lint `suspicious_splitn`

fixes: #7245
changelog: Add lint `suspicious_splitn`
This commit is contained in:
bors 2021-05-30 20:32:22 +00:00
commit d1308aecaf
9 changed files with 197 additions and 7 deletions

View file

@ -25,8 +25,8 @@ fn main() {
x.rsplit('x');
x.split_terminator('x');
x.rsplit_terminator('x');
x.splitn(0, 'x');
x.rsplitn(0, 'x');
x.splitn(2, 'x');
x.rsplitn(2, 'x');
x.matches('x');
x.rmatches('x');
x.match_indices('x');

View file

@ -25,8 +25,8 @@ fn main() {
x.rsplit("x");
x.split_terminator("x");
x.rsplit_terminator("x");
x.splitn(0, "x");
x.rsplitn(0, "x");
x.splitn(2, "x");
x.rsplitn(2, "x");
x.matches("x");
x.rmatches("x");
x.match_indices("x");

View file

@ -75,13 +75,13 @@ LL | x.rsplit_terminator("x");
error: single-character string constant used as pattern
--> $DIR/single_char_pattern.rs:28:17
|
LL | x.splitn(0, "x");
LL | x.splitn(2, "x");
| ^^^ help: try using a `char` instead: `'x'`
error: single-character string constant used as pattern
--> $DIR/single_char_pattern.rs:29:18
|
LL | x.rsplitn(0, "x");
LL | x.rsplitn(2, "x");
| ^^^ help: try using a `char` instead: `'x'`
error: single-character string constant used as pattern

View file

@ -0,0 +1,20 @@
#![warn(clippy::suspicious_splitn)]
fn main() {
let _ = "a,b,c".splitn(3, ',');
let _ = [0, 1, 2, 1, 3].splitn(3, |&x| x == 1);
let _ = "".splitn(0, ',');
let _ = [].splitn(0, |&x: &u32| x == 1);
let _ = "a,b".splitn(0, ',');
let _ = "a,b".rsplitn(0, ',');
let _ = "a,b".splitn(1, ',');
let _ = [0, 1, 2].splitn(0, |&x| x == 1);
let _ = [0, 1, 2].splitn_mut(0, |&x| x == 1);
let _ = [0, 1, 2].splitn(1, |&x| x == 1);
let _ = [0, 1, 2].rsplitn_mut(1, |&x| x == 1);
const X: usize = 0;
let _ = "a,b".splitn(X + 1, ',');
let _ = "a,b".splitn(X, ',');
}

View file

@ -0,0 +1,75 @@
error: `splitn` called with `0` splits
--> $DIR/suspicious_splitn.rs:9:13
|
LL | let _ = "a,b".splitn(0, ',');
| ^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::suspicious-splitn` implied by `-D warnings`
= note: the resulting iterator will always return `None`
error: `rsplitn` called with `0` splits
--> $DIR/suspicious_splitn.rs:10:13
|
LL | let _ = "a,b".rsplitn(0, ',');
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: the resulting iterator will always return `None`
error: `splitn` called with `1` split
--> $DIR/suspicious_splitn.rs:11:13
|
LL | let _ = "a,b".splitn(1, ',');
| ^^^^^^^^^^^^^^^^^^^^
|
= note: the resulting iterator will always return the entire string followed by `None`
error: `splitn` called with `0` splits
--> $DIR/suspicious_splitn.rs:12:13
|
LL | let _ = [0, 1, 2].splitn(0, |&x| x == 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the resulting iterator will always return `None`
error: `splitn_mut` called with `0` splits
--> $DIR/suspicious_splitn.rs:13:13
|
LL | let _ = [0, 1, 2].splitn_mut(0, |&x| x == 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the resulting iterator will always return `None`
error: `splitn` called with `1` split
--> $DIR/suspicious_splitn.rs:14:13
|
LL | let _ = [0, 1, 2].splitn(1, |&x| x == 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the resulting iterator will always return the entire slice followed by `None`
error: `rsplitn_mut` called with `1` split
--> $DIR/suspicious_splitn.rs:15:13
|
LL | let _ = [0, 1, 2].rsplitn_mut(1, |&x| x == 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the resulting iterator will always return the entire slice followed by `None`
error: `splitn` called with `1` split
--> $DIR/suspicious_splitn.rs:18:13
|
LL | let _ = "a,b".splitn(X + 1, ',');
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the resulting iterator will always return the entire string followed by `None`
error: `splitn` called with `0` splits
--> $DIR/suspicious_splitn.rs:19:13
|
LL | let _ = "a,b".splitn(X, ',');
| ^^^^^^^^^^^^^^^^^^^^
|
= note: the resulting iterator will always return `None`
error: aborting due to 9 previous errors