-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #6119 - rsulli55:find_is_some_on_strs, r=flip1995
Add a case to `lint_search_is_some` to handle searching strings Fixes: #6010 This adds a lint which recommends using `contains()` instead of `find()` followed by `is_some()` on strings as suggested in #6010. This was added as an additional case to https://github.com/rust-lang/rust-clippy/blob/5af88e3c2d8cc4fb74a0e455381669930ee3a31a/clippy_lints/src/methods/mod.rs#L3037 I would really appreciate any comments/suggestions for my code! changelog: Added case to `lint_search_is_some` to handle searching strings
- Loading branch information
Showing
9 changed files
with
280 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// aux-build:option_helpers.rs | ||
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]; | ||
let y = &&42; | ||
|
||
|
||
// Check `find().is_some()`, multi-line case. | ||
let _ = v.iter().find(|&x| { | ||
*x < 0 | ||
} | ||
).is_some(); | ||
|
||
// Check `position().is_some()`, multi-line case. | ||
let _ = v.iter().position(|&x| { | ||
x < 0 | ||
} | ||
).is_some(); | ||
|
||
// Check `rposition().is_some()`, multi-line case. | ||
let _ = v.iter().rposition(|&x| { | ||
x < 0 | ||
} | ||
).is_some(); | ||
|
||
// Check that we don't lint if the caller is not an `Iterator` or string | ||
let falsepos = IteratorFalsePositives { foo: 0 }; | ||
let _ = falsepos.find().is_some(); | ||
let _ = falsepos.position().is_some(); | ||
let _ = falsepos.rposition().is_some(); | ||
// 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_some(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
error: called `is_some()` after searching an `Iterator` with `find` | ||
--> $DIR/search_is_some.rs:13:13 | ||
| | ||
LL | let _ = v.iter().find(|&x| { | ||
| _____________^ | ||
LL | | *x < 0 | ||
LL | | } | ||
LL | | ).is_some(); | ||
| |______________________________^ | ||
| | ||
= note: `-D clippy::search-is-some` implied by `-D warnings` | ||
= 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 | ||
| | ||
LL | let _ = v.iter().position(|&x| { | ||
| _____________^ | ||
LL | | x < 0 | ||
LL | | } | ||
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 | ||
| | ||
LL | let _ = v.iter().rposition(|&x| { | ||
| _____________^ | ||
LL | | x < 0 | ||
LL | | } | ||
LL | | ).is_some(); | ||
| |______________________________^ | ||
| | ||
= help: this is more succinctly expressed by calling `any()` | ||
|
||
error: aborting due to 3 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::search_is_some)] | ||
|
||
fn main() { | ||
let v = vec![3, 2, 1, 0, -1, -2, -3]; | ||
let y = &&42; | ||
|
||
// Check `find().is_some()`, 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_some()`, single-line case. | ||
let _ = v.iter().any(|&x| x < 0); | ||
|
||
// Check `rposition().is_some()`, 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..]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::search_is_some)] | ||
|
||
fn main() { | ||
let v = vec![3, 2, 1, 0, -1, -2, -3]; | ||
let y = &&42; | ||
|
||
// Check `find().is_some()`, single-line case. | ||
let _ = v.iter().find(|&x| *x < 0).is_some(); | ||
let _ = (0..1).find(|x| **y == *x).is_some(); // one dereference less | ||
let _ = (0..1).find(|x| *x == 0).is_some(); | ||
let _ = v.iter().find(|x| **x == 0).is_some(); | ||
|
||
// Check `position().is_some()`, single-line case. | ||
let _ = v.iter().position(|&x| x < 0).is_some(); | ||
|
||
// Check `rposition().is_some()`, single-line case. | ||
let _ = v.iter().rposition(|&x| x < 0).is_some(); | ||
|
||
let s1 = String::from("hello world"); | ||
let s2 = String::from("world"); | ||
// caller of `find()` is a `&`static str` | ||
let _ = "hello world".find("world").is_some(); | ||
let _ = "hello world".find(&s2).is_some(); | ||
let _ = "hello world".find(&s2[2..]).is_some(); | ||
// caller of `find()` is a `String` | ||
let _ = s1.find("world").is_some(); | ||
let _ = s1.find(&s2).is_some(); | ||
let _ = s1.find(&s2[2..]).is_some(); | ||
// caller of `find()` is slice of `String` | ||
let _ = s1[2..].find("world").is_some(); | ||
let _ = s1[2..].find(&s2).is_some(); | ||
let _ = s1[2..].find(&s2[2..]).is_some(); | ||
} |
Oops, something went wrong.