search_is_some: check also when search is none

This commit is contained in:
Mateusz Gacek 2021-03-20 14:30:45 +01:00
parent 1d3c539fbb
commit 2ffee89b75
7 changed files with 359 additions and 48 deletions

View file

@ -1,8 +1,9 @@
// aux-build:option_helpers.rs
#![warn(clippy::search_is_some)]
#![allow(dead_code)]
extern crate option_helpers;
use option_helpers::IteratorFalsePositives;
#[warn(clippy::search_is_some)]
#[rustfmt::skip]
fn main() {
let v = vec![3, 2, 1, 0, -1, -2, -3];
@ -36,3 +37,37 @@ fn main() {
// `Pattern` that is not a string
let _ = "hello world".find(|c: char| c == 'o' || c == 'l').is_some();
}
#[rustfmt::skip]
fn is_none() {
let v = vec![3, 2, 1, 0, -1, -2, -3];
let y = &&42;
// Check `find().is_none()`, multi-line case.
let _ = v.iter().find(|&x| {
*x < 0
}
).is_none();
// Check `position().is_none()`, multi-line case.
let _ = v.iter().position(|&x| {
x < 0
}
).is_none();
// Check `rposition().is_none()`, multi-line case.
let _ = v.iter().rposition(|&x| {
x < 0
}
).is_none();
// Check that we don't lint if the caller is not an `Iterator` or string
let falsepos = IteratorFalsePositives { foo: 0 };
let _ = falsepos.find().is_none();
let _ = falsepos.position().is_none();
let _ = falsepos.rposition().is_none();
// check that we don't lint if `find()` is called with
// `Pattern` that is not a string
let _ = "hello world".find(|c: char| c == 'o' || c == 'l').is_none();
}

View file

@ -1,5 +1,5 @@
error: called `is_some()` after searching an `Iterator` with `find`
--> $DIR/search_is_some.rs:13:13
--> $DIR/search_is_some.rs:14:13
|
LL | let _ = v.iter().find(|&x| {
| _____________^
@ -12,7 +12,7 @@ LL | | ).is_some();
= help: this is more succinctly expressed by calling `any()`
error: called `is_some()` after searching an `Iterator` with `position`
--> $DIR/search_is_some.rs:19:13
--> $DIR/search_is_some.rs:20:13
|
LL | let _ = v.iter().position(|&x| {
| _____________^
@ -24,7 +24,7 @@ LL | | ).is_some();
= help: this is more succinctly expressed by calling `any()`
error: called `is_some()` after searching an `Iterator` with `rposition`
--> $DIR/search_is_some.rs:25:13
--> $DIR/search_is_some.rs:26:13
|
LL | let _ = v.iter().rposition(|&x| {
| _____________^
@ -35,5 +35,41 @@ LL | | ).is_some();
|
= help: this is more succinctly expressed by calling `any()`
error: aborting due to 3 previous errors
error: called `is_none()` after searching an `Iterator` with `find`
--> $DIR/search_is_some.rs:48:13
|
LL | let _ = v.iter().find(|&x| {
| _____________^
LL | | *x < 0
LL | | }
LL | | ).is_none();
| |______________________________^
|
= help: this is more succinctly expressed by calling `any()` with negation
error: called `is_none()` after searching an `Iterator` with `position`
--> $DIR/search_is_some.rs:54:13
|
LL | let _ = v.iter().position(|&x| {
| _____________^
LL | | x < 0
LL | | }
LL | | ).is_none();
| |______________________________^
|
= help: this is more succinctly expressed by calling `any()` with negation
error: called `is_none()` after searching an `Iterator` with `rposition`
--> $DIR/search_is_some.rs:60:13
|
LL | let _ = v.iter().rposition(|&x| {
| _____________^
LL | | x < 0
LL | | }
LL | | ).is_none();
| |______________________________^
|
= help: this is more succinctly expressed by calling `any()` with negation
error: aborting due to 6 previous errors

View file

@ -1,5 +1,5 @@
// run-rustfix
#![allow(dead_code)]
#![warn(clippy::search_is_some)]
fn main() {
@ -33,3 +33,36 @@ fn main() {
let _ = s1[2..].contains(&s2);
let _ = s1[2..].contains(&s2[2..]);
}
fn is_none() {
let v = vec![3, 2, 1, 0, -1, -2, -3];
let y = &&42;
// Check `find().is_none()`, single-line case.
let _ = !v.iter().any(|x| *x < 0);
let _ = !(0..1).any(|x| **y == x); // one dereference less
let _ = !(0..1).any(|x| x == 0);
let _ = !v.iter().any(|x| *x == 0);
// Check `position().is_none()`, single-line case.
let _ = !v.iter().any(|&x| x < 0);
// Check `rposition().is_none()`, single-line case.
let _ = !v.iter().any(|&x| x < 0);
let s1 = String::from("hello world");
let s2 = String::from("world");
// caller of `find()` is a `&`static str`
let _ = !"hello world".contains("world");
let _ = !"hello world".contains(&s2);
let _ = !"hello world".contains(&s2[2..]);
// caller of `find()` is a `String`
let _ = !s1.contains("world");
let _ = !s1.contains(&s2);
let _ = !s1.contains(&s2[2..]);
// caller of `find()` is slice of `String`
let _ = !s1[2..].contains("world");
let _ = !s1[2..].contains(&s2);
let _ = !s1[2..].contains(&s2[2..]);
}

View file

@ -1,5 +1,5 @@
// run-rustfix
#![allow(dead_code)]
#![warn(clippy::search_is_some)]
fn main() {
@ -33,3 +33,36 @@ fn main() {
let _ = s1[2..].find(&s2).is_some();
let _ = s1[2..].find(&s2[2..]).is_some();
}
fn is_none() {
let v = vec![3, 2, 1, 0, -1, -2, -3];
let y = &&42;
// Check `find().is_none()`, single-line case.
let _ = v.iter().find(|&x| *x < 0).is_none();
let _ = (0..1).find(|x| **y == *x).is_none(); // one dereference less
let _ = (0..1).find(|x| *x == 0).is_none();
let _ = v.iter().find(|x| **x == 0).is_none();
// Check `position().is_none()`, single-line case.
let _ = v.iter().position(|&x| x < 0).is_none();
// Check `rposition().is_none()`, single-line case.
let _ = v.iter().rposition(|&x| x < 0).is_none();
let s1 = String::from("hello world");
let s2 = String::from("world");
// caller of `find()` is a `&`static str`
let _ = "hello world".find("world").is_none();
let _ = "hello world".find(&s2).is_none();
let _ = "hello world".find(&s2[2..]).is_none();
// caller of `find()` is a `String`
let _ = s1.find("world").is_none();
let _ = s1.find(&s2).is_none();
let _ = s1.find(&s2[2..]).is_none();
// caller of `find()` is slice of `String`
let _ = s1[2..].find("world").is_none();
let _ = s1[2..].find(&s2).is_none();
let _ = s1[2..].find(&s2[2..]).is_none();
}

View file

@ -90,5 +90,95 @@ error: called `is_some()` after calling `find()` on a string
LL | let _ = s1[2..].find(&s2[2..]).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use `contains()` instead: `contains(&s2[2..])`
error: aborting due to 15 previous errors
error: called `is_none()` after searching an `Iterator` with `find`
--> $DIR/search_is_some_fixable.rs:42:13
|
LL | let _ = v.iter().find(|&x| *x < 0).is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|x| *x < 0)`
error: called `is_none()` after searching an `Iterator` with `find`
--> $DIR/search_is_some_fixable.rs:43:13
|
LL | let _ = (0..1).find(|x| **y == *x).is_none(); // one dereference less
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!(0..1).any(|x| **y == x)`
error: called `is_none()` after searching an `Iterator` with `find`
--> $DIR/search_is_some_fixable.rs:44:13
|
LL | let _ = (0..1).find(|x| *x == 0).is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!(0..1).any(|x| x == 0)`
error: called `is_none()` after searching an `Iterator` with `find`
--> $DIR/search_is_some_fixable.rs:45:13
|
LL | let _ = v.iter().find(|x| **x == 0).is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|x| *x == 0)`
error: called `is_none()` after searching an `Iterator` with `position`
--> $DIR/search_is_some_fixable.rs:48:13
|
LL | let _ = v.iter().position(|&x| x < 0).is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|&x| x < 0)`
error: called `is_none()` after searching an `Iterator` with `rposition`
--> $DIR/search_is_some_fixable.rs:51:13
|
LL | let _ = v.iter().rposition(|&x| x < 0).is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.any()` instead: `!v.iter().any(|&x| x < 0)`
error: called `is_none()` after calling `find()` on a string
--> $DIR/search_is_some_fixable.rs:57:13
|
LL | let _ = "hello world".find("world").is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!"hello world".contains("world")`
error: called `is_none()` after calling `find()` on a string
--> $DIR/search_is_some_fixable.rs:58:13
|
LL | let _ = "hello world".find(&s2).is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!"hello world".contains(&s2)`
error: called `is_none()` after calling `find()` on a string
--> $DIR/search_is_some_fixable.rs:59:13
|
LL | let _ = "hello world".find(&s2[2..]).is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!"hello world".contains(&s2[2..])`
error: called `is_none()` after calling `find()` on a string
--> $DIR/search_is_some_fixable.rs:61:13
|
LL | let _ = s1.find("world").is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1.contains("world")`
error: called `is_none()` after calling `find()` on a string
--> $DIR/search_is_some_fixable.rs:62:13
|
LL | let _ = s1.find(&s2).is_none();
| ^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1.contains(&s2)`
error: called `is_none()` after calling `find()` on a string
--> $DIR/search_is_some_fixable.rs:63:13
|
LL | let _ = s1.find(&s2[2..]).is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1.contains(&s2[2..])`
error: called `is_none()` after calling `find()` on a string
--> $DIR/search_is_some_fixable.rs:65:13
|
LL | let _ = s1[2..].find("world").is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1[2..].contains("world")`
error: called `is_none()` after calling `find()` on a string
--> $DIR/search_is_some_fixable.rs:66:13
|
LL | let _ = s1[2..].find(&s2).is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1[2..].contains(&s2)`
error: called `is_none()` after calling `find()` on a string
--> $DIR/search_is_some_fixable.rs:67:13
|
LL | let _ = s1[2..].find(&s2[2..]).is_none();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `!_.contains()` instead: `!s1[2..].contains(&s2[2..])`
error: aborting due to 30 previous errors