Skip to content

Commit

Permalink
Extract IteratorFalsePositives into option_helpers.rs
Browse files Browse the repository at this point in the history
This was previously duplicated in rust-lang#3605
  • Loading branch information
phansch committed Jan 2, 2019
1 parent eaaee23 commit 0c54913
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 103 deletions.
36 changes: 36 additions & 0 deletions tests/auxiliary/option_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,39 @@
macro_rules! opt_map {
($opt:expr, $map:expr) => {($opt).map($map)};
}

/// Struct to generate false positive for Iterator-based lints
#[derive(Copy, Clone)]
struct IteratorFalsePositives {
foo: u32,
}

impl IteratorFalsePositives {
fn filter(self) -> IteratorFalsePositives {
self
}

fn next(self) -> IteratorFalsePositives {
self
}

fn find(self) -> Option<u32> {
Some(self.foo)
}

fn position(self) -> Option<u32> {
Some(self.foo)
}

fn rposition(self) -> Option<u32> {
Some(self.foo)
}

fn nth(self, n: usize) -> Option<u32> {
Some(self.foo)
}

fn skip(self, _: usize) -> IteratorFalsePositives {
self
}
}
36 changes: 1 addition & 35 deletions tests/ui/iter_skip_next.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,7 @@
#![warn(clippy::iter_skip_next)]
#![allow(clippy::blacklisted_name)]

/// Struct to generate false positive for Iterator-based lints
#[derive(Copy, Clone)]
struct IteratorFalsePositives {
foo: u32,
}

impl IteratorFalsePositives {
fn filter(self) -> IteratorFalsePositives {
self
}

fn next(self) -> IteratorFalsePositives {
self
}

fn find(self) -> Option<u32> {
Some(self.foo)
}

fn position(self) -> Option<u32> {
Some(self.foo)
}

fn rposition(self) -> Option<u32> {
Some(self.foo)
}

fn nth(self, n: usize) -> Option<u32> {
Some(self.foo)
}

fn skip(self, _: usize) -> IteratorFalsePositives {
self
}
}
include!("../auxiliary/option_helpers.rs");

/// Checks implementation of `ITER_SKIP_NEXT` lint
fn iter_skip_next() {
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/iter_skip_next.stderr
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
--> $DIR/iter_skip_next.rs:52:13
--> $DIR/iter_skip_next.rs:18:13
|
LL | let _ = some_vec.iter().skip(42).next();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::iter-skip-next` implied by `-D warnings`

error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
--> $DIR/iter_skip_next.rs:53:13
--> $DIR/iter_skip_next.rs:19:13
|
LL | let _ = some_vec.iter().cycle().skip(42).next();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
--> $DIR/iter_skip_next.rs:54:13
--> $DIR/iter_skip_next.rs:20:13
|
LL | let _ = (1..10).skip(10).next();
| ^^^^^^^^^^^^^^^^^^^^^^^

error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
--> $DIR/iter_skip_next.rs:55:14
--> $DIR/iter_skip_next.rs:21:14
|
LL | let _ = &some_vec[..].iter().skip(3).next();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
36 changes: 0 additions & 36 deletions tests/ui/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,42 +178,6 @@ impl HasIter {
}
}

/// Struct to generate false positive for Iterator-based lints
#[derive(Copy, Clone)]
struct IteratorFalsePositives {
foo: u32,
}

impl IteratorFalsePositives {
fn filter(self) -> IteratorFalsePositives {
self
}

fn next(self) -> IteratorFalsePositives {
self
}

fn find(self) -> Option<u32> {
Some(self.foo)
}

fn position(self) -> Option<u32> {
Some(self.foo)
}

fn rposition(self) -> Option<u32> {
Some(self.foo)
}

fn nth(self, n: usize) -> Option<u32> {
Some(self.foo)
}

fn skip(self, _: usize) -> IteratorFalsePositives {
self
}
}

/// Checks implementation of `FILTER_NEXT` lint
fn filter_next() {
let v = vec![3, 2, 1, 0, -1, -2, -3];
Expand Down
56 changes: 28 additions & 28 deletions tests/ui/methods.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ LL | });
|

error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
--> $DIR/methods.rs:222:13
--> $DIR/methods.rs:186:13
|
LL | let _ = v.iter().filter(|&x| *x < 0).next();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -153,7 +153,7 @@ LL | let _ = v.iter().filter(|&x| *x < 0).next();
= note: replace `filter(|&x| *x < 0).next()` with `find(|&x| *x < 0)`

error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
--> $DIR/methods.rs:225:13
--> $DIR/methods.rs:189:13
|
LL | let _ = v.iter().filter(|&x| {
| _____________^
Expand All @@ -163,7 +163,7 @@ LL | | ).next();
| |___________________________^

error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:240:13
--> $DIR/methods.rs:204:13
|
LL | let _ = v.iter().find(|&x| *x < 0).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -172,7 +172,7 @@ LL | let _ = v.iter().find(|&x| *x < 0).is_some();
= note: replace `find(|&x| *x < 0).is_some()` with `any(|&x| *x < 0)`

error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:243:13
--> $DIR/methods.rs:207:13
|
LL | let _ = v.iter().find(|&x| {
| _____________^
Expand All @@ -182,15 +182,15 @@ LL | | ).is_some();
| |______________________________^

error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:249:13
--> $DIR/methods.rs:213:13
|
LL | let _ = v.iter().position(|&x| x < 0).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: replace `position(|&x| x < 0).is_some()` with `any(|&x| x < 0)`

error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:252:13
--> $DIR/methods.rs:216:13
|
LL | let _ = v.iter().position(|&x| {
| _____________^
Expand All @@ -200,15 +200,15 @@ LL | | ).is_some();
| |______________________________^

error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:258:13
--> $DIR/methods.rs:222:13
|
LL | let _ = v.iter().rposition(|&x| x < 0).is_some();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: replace `rposition(|&x| x < 0).is_some()` with `any(|&x| x < 0)`

error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
--> $DIR/methods.rs:261:13
--> $DIR/methods.rs:225:13
|
LL | let _ = v.iter().rposition(|&x| {
| _____________^
Expand All @@ -218,125 +218,125 @@ LL | | ).is_some();
| |______________________________^

error: use of `unwrap_or` followed by a function call
--> $DIR/methods.rs:296:22
--> $DIR/methods.rs:260:22
|
LL | with_constructor.unwrap_or(make());
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(make)`
|
= note: `-D clippy::or-fun-call` implied by `-D warnings`

error: use of `unwrap_or` followed by a call to `new`
--> $DIR/methods.rs:299:5
--> $DIR/methods.rs:263:5
|
LL | with_new.unwrap_or(Vec::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_new.unwrap_or_default()`

error: use of `unwrap_or` followed by a function call
--> $DIR/methods.rs:302:21
--> $DIR/methods.rs:266:21
|
LL | with_const_args.unwrap_or(Vec::with_capacity(12));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| Vec::with_capacity(12))`

error: use of `unwrap_or` followed by a function call
--> $DIR/methods.rs:305:14
--> $DIR/methods.rs:269:14
|
LL | with_err.unwrap_or(make());
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| make())`

error: use of `unwrap_or` followed by a function call
--> $DIR/methods.rs:308:19
--> $DIR/methods.rs:272:19
|
LL | with_err_args.unwrap_or(Vec::with_capacity(12));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| Vec::with_capacity(12))`

error: use of `unwrap_or` followed by a call to `default`
--> $DIR/methods.rs:311:5
--> $DIR/methods.rs:275:5
|
LL | with_default_trait.unwrap_or(Default::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_trait.unwrap_or_default()`

error: use of `unwrap_or` followed by a call to `default`
--> $DIR/methods.rs:314:5
--> $DIR/methods.rs:278:5
|
LL | with_default_type.unwrap_or(u64::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_type.unwrap_or_default()`

error: use of `unwrap_or` followed by a function call
--> $DIR/methods.rs:317:14
--> $DIR/methods.rs:281:14
|
LL | with_vec.unwrap_or(vec![]);
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| vec![])`

error: use of `unwrap_or` followed by a function call
--> $DIR/methods.rs:322:21
--> $DIR/methods.rs:286:21
|
LL | without_default.unwrap_or(Foo::new());
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(Foo::new)`

error: use of `or_insert` followed by a function call
--> $DIR/methods.rs:325:19
--> $DIR/methods.rs:289:19
|
LL | map.entry(42).or_insert(String::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`

error: use of `or_insert` followed by a function call
--> $DIR/methods.rs:328:21
--> $DIR/methods.rs:292:21
|
LL | btree.entry(42).or_insert(String::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`

error: use of `unwrap_or` followed by a function call
--> $DIR/methods.rs:331:21
--> $DIR/methods.rs:295:21
|
LL | let _ = stringy.unwrap_or("".to_owned());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())`

error: called `.iter().nth()` on a Vec. Calling `.get()` is both faster and more readable
--> $DIR/methods.rs:342:23
--> $DIR/methods.rs:306:23
|
LL | let bad_vec = some_vec.iter().nth(3);
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::iter-nth` implied by `-D warnings`

error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
--> $DIR/methods.rs:343:26
--> $DIR/methods.rs:307:26
|
LL | let bad_slice = &some_vec[..].iter().nth(3);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
--> $DIR/methods.rs:344:31
--> $DIR/methods.rs:308:31
|
LL | let bad_boxed_slice = boxed_slice.iter().nth(3);
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: called `.iter().nth()` on a VecDeque. Calling `.get()` is both faster and more readable
--> $DIR/methods.rs:345:29
--> $DIR/methods.rs:309:29
|
LL | let bad_vec_deque = some_vec_deque.iter().nth(3);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: called `.iter_mut().nth()` on a Vec. Calling `.get_mut()` is both faster and more readable
--> $DIR/methods.rs:350:23
--> $DIR/methods.rs:314:23
|
LL | let bad_vec = some_vec.iter_mut().nth(3);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: called `.iter_mut().nth()` on a slice. Calling `.get_mut()` is both faster and more readable
--> $DIR/methods.rs:353:26
--> $DIR/methods.rs:317:26
|
LL | let bad_slice = &some_vec[..].iter_mut().nth(3);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: called `.iter_mut().nth()` on a VecDeque. Calling `.get_mut()` is both faster and more readable
--> $DIR/methods.rs:356:29
--> $DIR/methods.rs:320:29
|
LL | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: used unwrap() on an Option value. If you don't want to handle the None case gracefully, consider using expect() to provide a better panic message
--> $DIR/methods.rs:368:13
--> $DIR/methods.rs:332:13
|
LL | let _ = opt.unwrap();
| ^^^^^^^^^^^^
Expand Down

0 comments on commit 0c54913

Please sign in to comment.