-
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.
- Loading branch information
Showing
9 changed files
with
240 additions
and
2 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,58 @@ | ||
// run-rustfix | ||
// aux-build:option_helpers.rs | ||
|
||
#![warn(clippy::iter_count)] | ||
#![allow(unused_variables)] | ||
#![allow(unused_mut)] | ||
|
||
extern crate option_helpers; | ||
|
||
use option_helpers::IteratorFalsePositives; | ||
use std::collections::{HashSet, VecDeque}; | ||
|
||
/// Struct to generate false positives for things with `.iter()`. | ||
#[derive(Copy, Clone)] | ||
struct HasIter; | ||
|
||
impl HasIter { | ||
fn iter(self) -> IteratorFalsePositives { | ||
IteratorFalsePositives { foo: 0 } | ||
} | ||
|
||
fn iter_mut(self) -> IteratorFalsePositives { | ||
IteratorFalsePositives { foo: 0 } | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut some_vec = vec![0, 1, 2, 3]; | ||
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]); | ||
let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect(); | ||
let mut some_hash_set = HashSet::new(); | ||
some_hash_set.insert(1); | ||
|
||
{ | ||
// Make sure we lint `.iter()` for relevant types. | ||
let bad_vec = some_vec.len(); | ||
let bad_slice = &some_vec[..].len(); | ||
let bad_boxed_slice = boxed_slice.len(); | ||
let bad_vec_deque = some_vec_deque.len(); | ||
let bad_hash_set = some_hash_set.len(); | ||
} | ||
|
||
{ | ||
// Make sure we lint `.iter_mut()` for relevant types. | ||
let bad_vec = some_vec.len(); | ||
} | ||
{ | ||
let bad_slice = &some_vec[..].len(); | ||
} | ||
{ | ||
let bad_vec_deque = some_vec_deque.len(); | ||
} | ||
|
||
// Make sure we don't lint for non-relevant types. | ||
let false_positive = HasIter; | ||
let ok = false_positive.iter().count(); | ||
let ok_mut = false_positive.iter_mut().count(); | ||
} |
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,58 @@ | ||
// run-rustfix | ||
// aux-build:option_helpers.rs | ||
|
||
#![warn(clippy::iter_count)] | ||
#![allow(unused_variables)] | ||
#![allow(unused_mut)] | ||
|
||
extern crate option_helpers; | ||
|
||
use option_helpers::IteratorFalsePositives; | ||
use std::collections::{HashSet, VecDeque}; | ||
|
||
/// Struct to generate false positives for things with `.iter()`. | ||
#[derive(Copy, Clone)] | ||
struct HasIter; | ||
|
||
impl HasIter { | ||
fn iter(self) -> IteratorFalsePositives { | ||
IteratorFalsePositives { foo: 0 } | ||
} | ||
|
||
fn iter_mut(self) -> IteratorFalsePositives { | ||
IteratorFalsePositives { foo: 0 } | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut some_vec = vec![0, 1, 2, 3]; | ||
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]); | ||
let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect(); | ||
let mut some_hash_set = HashSet::new(); | ||
some_hash_set.insert(1); | ||
|
||
{ | ||
// Make sure we lint `.iter()` for relevant types. | ||
let bad_vec = some_vec.iter().count(); | ||
let bad_slice = &some_vec[..].iter().count(); | ||
let bad_boxed_slice = boxed_slice.iter().count(); | ||
let bad_vec_deque = some_vec_deque.iter().count(); | ||
let bad_hash_set = some_hash_set.iter().count(); | ||
} | ||
|
||
{ | ||
// Make sure we lint `.iter_mut()` for relevant types. | ||
let bad_vec = some_vec.iter_mut().count(); | ||
} | ||
{ | ||
let bad_slice = &some_vec[..].iter_mut().count(); | ||
} | ||
{ | ||
let bad_vec_deque = some_vec_deque.iter_mut().count(); | ||
} | ||
|
||
// Make sure we don't lint for non-relevant types. | ||
let false_positive = HasIter; | ||
let ok = false_positive.iter().count(); | ||
let ok_mut = false_positive.iter_mut().count(); | ||
} |
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,52 @@ | ||
error: called `.iter().count()` on a `Vec` | ||
--> $DIR/iter_count.rs:36:23 | ||
| | ||
LL | let bad_vec = some_vec.iter().count(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec.len()` | ||
| | ||
= note: `-D clippy::iter-count` implied by `-D warnings` | ||
|
||
error: called `.iter().count()` on a `slice` | ||
--> $DIR/iter_count.rs:37:26 | ||
| | ||
LL | let bad_slice = &some_vec[..].iter().count(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec[..].len()` | ||
|
||
error: called `.iter().count()` on a `slice` | ||
--> $DIR/iter_count.rs:38:31 | ||
| | ||
LL | let bad_boxed_slice = boxed_slice.iter().count(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `boxed_slice.len()` | ||
|
||
error: called `.iter().count()` on a `VecDeque` | ||
--> $DIR/iter_count.rs:39:29 | ||
| | ||
LL | let bad_vec_deque = some_vec_deque.iter().count(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec_deque.len()` | ||
|
||
error: called `.iter().count()` on a `std::iter::Iterator` | ||
--> $DIR/iter_count.rs:40:28 | ||
| | ||
LL | let bad_hash_set = some_hash_set.iter().count(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_hash_set.len()` | ||
|
||
error: called `.iter_mut().count()` on a `Vec` | ||
--> $DIR/iter_count.rs:45:23 | ||
| | ||
LL | let bad_vec = some_vec.iter_mut().count(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec.len()` | ||
|
||
error: called `.iter_mut().count()` on a `slice` | ||
--> $DIR/iter_count.rs:48:26 | ||
| | ||
LL | let bad_slice = &some_vec[..].iter_mut().count(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec[..].len()` | ||
|
||
error: called `.iter_mut().count()` on a `VecDeque` | ||
--> $DIR/iter_count.rs:51:29 | ||
| | ||
LL | let bad_vec_deque = some_vec_deque.iter_mut().count(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `some_vec_deque.len()` | ||
|
||
error: aborting due to 8 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
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