Skip to content

Commit cc2b000

Browse files
committed
return when the ty doesn't have len()
1 parent 9958af4 commit cc2b000

File tree

4 files changed

+169
-26
lines changed

4 files changed

+169
-26
lines changed

clippy_lints/src/methods/iter_count.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::utils::{
2-
derefs_to_slice, is_type_diagnostic_item, match_trait_method, method_chain_args, paths, snippet_with_applicability,
2+
derefs_to_slice, is_type_diagnostic_item, match_type, method_chain_args, paths, snippet_with_applicability,
33
span_lint_and_sugg,
44
};
5+
56
use if_chain::if_chain;
67
use rustc_errors::Applicability;
78
use rustc_hir::Expr;
@@ -19,19 +20,29 @@ pub(crate) fn lints<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'
1920
} else {
2021
return;
2122
};
23+
let ty = cx.typeck_results().expr_ty(&iter_args[0]);
2224
if_chain! {
23-
let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.typeck_results().expr_ty(&iter_args[0])).is_some() {
24-
Some("slice")
25-
} else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym::vec_type) {
26-
Some("Vec")
27-
} else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym!(vecdeque_type)) {
28-
Some("VecDeque")
29-
} else if match_trait_method(cx, expr, &paths::ITERATOR) {
30-
Some("std::iter::Iterator")
25+
let caller_type = if derefs_to_slice(cx, &iter_args[0], ty).is_some() {
26+
"slice"
27+
} else if is_type_diagnostic_item(cx, ty, sym::vec_type) {
28+
"Vec"
29+
} else if is_type_diagnostic_item(cx, ty, sym!(vecdeque_type)) {
30+
"VecDeque"
31+
} else if is_type_diagnostic_item(cx, ty, sym!(hashset_type)) {
32+
"HashSet"
33+
} else if is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) {
34+
"HashMap"
35+
} else if match_type(cx, ty, &paths::BTREEMAP) {
36+
"BTreeMap"
37+
} else if match_type(cx, ty, &paths::BTREESET) {
38+
"BTreeSet"
39+
} else if match_type(cx, ty, &paths::LINKED_LIST) {
40+
"LinkedList"
41+
} else if match_type(cx, ty, &paths::BINARY_HEAP) {
42+
"BinaryHeap"
3143
} else {
32-
None
44+
return
3345
};
34-
if let Some(caller_type) = caller_type;
3546
then {
3647
let mut applicability = Applicability::MachineApplicable;
3748
span_lint_and_sugg(

tests/ui/iter_count.fixed

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
extern crate option_helpers;
1414

1515
use option_helpers::IteratorFalsePositives;
16-
use std::collections::{HashSet, VecDeque};
16+
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
1717

1818
/// Struct to generate false positives for things with `.iter()`.
1919
#[derive(Copy, Clone)]
@@ -38,21 +38,45 @@ fn main() {
3838
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
3939
let mut vec_deque: VecDeque<_> = vec.iter().cloned().collect();
4040
let mut hash_set = HashSet::new();
41+
let mut hash_map = HashMap::new();
42+
let mut b_tree_map = BTreeMap::new();
43+
let mut b_tree_set = BTreeSet::new();
44+
let mut linked_list = LinkedList::new();
45+
let mut binary_heap = BinaryHeap::new();
4146
hash_set.insert(1);
47+
hash_map.insert(1, 2);
48+
b_tree_map.insert(1, 2);
49+
b_tree_set.insert(1);
50+
linked_list.push_back(1);
51+
binary_heap.push(1);
4252

4353
&vec[..].len();
4454
vec.len();
4555
boxed_slice.len();
4656
vec_deque.len();
4757
hash_set.len();
58+
hash_map.len();
59+
b_tree_map.len();
60+
b_tree_set.len();
61+
linked_list.len();
62+
binary_heap.len();
4863

4964
vec.len();
5065
&vec[..].len();
5166
vec_deque.len();
67+
hash_map.len();
68+
b_tree_map.len();
69+
linked_list.len();
5270

5371
&vec[..].len();
5472
vec.len();
5573
vec_deque.len();
74+
hash_set.len();
75+
hash_map.len();
76+
b_tree_map.len();
77+
b_tree_set.len();
78+
linked_list.len();
79+
binary_heap.len();
5680

5781
// Make sure we don't lint for non-relevant types.
5882
let false_positive = HasIter;

tests/ui/iter_count.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
extern crate option_helpers;
1414

1515
use option_helpers::IteratorFalsePositives;
16-
use std::collections::{HashSet, VecDeque};
16+
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
1717

1818
/// Struct to generate false positives for things with `.iter()`.
1919
#[derive(Copy, Clone)]
@@ -38,21 +38,45 @@ fn main() {
3838
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
3939
let mut vec_deque: VecDeque<_> = vec.iter().cloned().collect();
4040
let mut hash_set = HashSet::new();
41+
let mut hash_map = HashMap::new();
42+
let mut b_tree_map = BTreeMap::new();
43+
let mut b_tree_set = BTreeSet::new();
44+
let mut linked_list = LinkedList::new();
45+
let mut binary_heap = BinaryHeap::new();
4146
hash_set.insert(1);
47+
hash_map.insert(1, 2);
48+
b_tree_map.insert(1, 2);
49+
b_tree_set.insert(1);
50+
linked_list.push_back(1);
51+
binary_heap.push(1);
4252

4353
&vec[..].iter().count();
4454
vec.iter().count();
4555
boxed_slice.iter().count();
4656
vec_deque.iter().count();
4757
hash_set.iter().count();
58+
hash_map.iter().count();
59+
b_tree_map.iter().count();
60+
b_tree_set.iter().count();
61+
linked_list.iter().count();
62+
binary_heap.iter().count();
4863

4964
vec.iter_mut().count();
5065
&vec[..].iter_mut().count();
5166
vec_deque.iter_mut().count();
67+
hash_map.iter_mut().count();
68+
b_tree_map.iter_mut().count();
69+
linked_list.iter_mut().count();
5270

5371
&vec[..].into_iter().count();
5472
vec.into_iter().count();
5573
vec_deque.into_iter().count();
74+
hash_set.into_iter().count();
75+
hash_map.into_iter().count();
76+
b_tree_map.into_iter().count();
77+
b_tree_set.into_iter().count();
78+
linked_list.into_iter().count();
79+
binary_heap.into_iter().count();
5680

5781
// Make sure we don't lint for non-relevant types.
5882
let false_positive = HasIter;

tests/ui/iter_count.stderr

Lines changed: 97 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,154 @@
11
error: called `.iter().count()` on a `slice`
2-
--> $DIR/iter_count.rs:43:6
2+
--> $DIR/iter_count.rs:53:6
33
|
44
LL | &vec[..].iter().count();
55
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()`
66
|
77
= note: `-D clippy::iter-count` implied by `-D warnings`
88

99
error: called `.iter().count()` on a `Vec`
10-
--> $DIR/iter_count.rs:44:5
10+
--> $DIR/iter_count.rs:54:5
1111
|
1212
LL | vec.iter().count();
1313
| ^^^^^^^^^^^^^^^^^^ help: try: `vec.len()`
1414

1515
error: called `.iter().count()` on a `slice`
16-
--> $DIR/iter_count.rs:45:5
16+
--> $DIR/iter_count.rs:55:5
1717
|
1818
LL | boxed_slice.iter().count();
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `boxed_slice.len()`
2020

2121
error: called `.iter().count()` on a `VecDeque`
22-
--> $DIR/iter_count.rs:46:5
22+
--> $DIR/iter_count.rs:56:5
2323
|
2424
LL | vec_deque.iter().count();
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec_deque.len()`
2626

27-
error: called `.iter().count()` on a `std::iter::Iterator`
28-
--> $DIR/iter_count.rs:47:5
27+
error: called `.iter().count()` on a `HashSet`
28+
--> $DIR/iter_count.rs:57:5
2929
|
3030
LL | hash_set.iter().count();
3131
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hash_set.len()`
3232

33+
error: called `.iter().count()` on a `HashMap`
34+
--> $DIR/iter_count.rs:58:5
35+
|
36+
LL | hash_map.iter().count();
37+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hash_map.len()`
38+
39+
error: called `.iter().count()` on a `BTreeMap`
40+
--> $DIR/iter_count.rs:59:5
41+
|
42+
LL | b_tree_map.iter().count();
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b_tree_map.len()`
44+
45+
error: called `.iter().count()` on a `BTreeSet`
46+
--> $DIR/iter_count.rs:60:5
47+
|
48+
LL | b_tree_set.iter().count();
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b_tree_set.len()`
50+
51+
error: called `.iter().count()` on a `LinkedList`
52+
--> $DIR/iter_count.rs:61:5
53+
|
54+
LL | linked_list.iter().count();
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `linked_list.len()`
56+
57+
error: called `.iter().count()` on a `BinaryHeap`
58+
--> $DIR/iter_count.rs:62:5
59+
|
60+
LL | binary_heap.iter().count();
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `binary_heap.len()`
62+
3363
error: called `.iter_mut().count()` on a `Vec`
34-
--> $DIR/iter_count.rs:49:5
64+
--> $DIR/iter_count.rs:64:5
3565
|
3666
LL | vec.iter_mut().count();
3767
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.len()`
3868

3969
error: called `.iter_mut().count()` on a `slice`
40-
--> $DIR/iter_count.rs:50:6
70+
--> $DIR/iter_count.rs:65:6
4171
|
4272
LL | &vec[..].iter_mut().count();
4373
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()`
4474

4575
error: called `.iter_mut().count()` on a `VecDeque`
46-
--> $DIR/iter_count.rs:51:5
76+
--> $DIR/iter_count.rs:66:5
4777
|
4878
LL | vec_deque.iter_mut().count();
4979
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec_deque.len()`
5080

81+
error: called `.iter_mut().count()` on a `HashMap`
82+
--> $DIR/iter_count.rs:67:5
83+
|
84+
LL | hash_map.iter_mut().count();
85+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hash_map.len()`
86+
87+
error: called `.iter_mut().count()` on a `BTreeMap`
88+
--> $DIR/iter_count.rs:68:5
89+
|
90+
LL | b_tree_map.iter_mut().count();
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b_tree_map.len()`
92+
93+
error: called `.iter_mut().count()` on a `LinkedList`
94+
--> $DIR/iter_count.rs:69:5
95+
|
96+
LL | linked_list.iter_mut().count();
97+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `linked_list.len()`
98+
5199
error: called `.into_iter().count()` on a `slice`
52-
--> $DIR/iter_count.rs:53:6
100+
--> $DIR/iter_count.rs:71:6
53101
|
54102
LL | &vec[..].into_iter().count();
55103
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec[..].len()`
56104

57105
error: called `.into_iter().count()` on a `Vec`
58-
--> $DIR/iter_count.rs:54:5
106+
--> $DIR/iter_count.rs:72:5
59107
|
60108
LL | vec.into_iter().count();
61109
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec.len()`
62110

63111
error: called `.into_iter().count()` on a `VecDeque`
64-
--> $DIR/iter_count.rs:55:5
112+
--> $DIR/iter_count.rs:73:5
65113
|
66114
LL | vec_deque.into_iter().count();
67115
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `vec_deque.len()`
68116

69-
error: aborting due to 11 previous errors
117+
error: called `.into_iter().count()` on a `HashSet`
118+
--> $DIR/iter_count.rs:74:5
119+
|
120+
LL | hash_set.into_iter().count();
121+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hash_set.len()`
122+
123+
error: called `.into_iter().count()` on a `HashMap`
124+
--> $DIR/iter_count.rs:75:5
125+
|
126+
LL | hash_map.into_iter().count();
127+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `hash_map.len()`
128+
129+
error: called `.into_iter().count()` on a `BTreeMap`
130+
--> $DIR/iter_count.rs:76:5
131+
|
132+
LL | b_tree_map.into_iter().count();
133+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b_tree_map.len()`
134+
135+
error: called `.into_iter().count()` on a `BTreeSet`
136+
--> $DIR/iter_count.rs:77:5
137+
|
138+
LL | b_tree_set.into_iter().count();
139+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `b_tree_set.len()`
140+
141+
error: called `.into_iter().count()` on a `LinkedList`
142+
--> $DIR/iter_count.rs:78:5
143+
|
144+
LL | linked_list.into_iter().count();
145+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `linked_list.len()`
146+
147+
error: called `.into_iter().count()` on a `BinaryHeap`
148+
--> $DIR/iter_count.rs:79:5
149+
|
150+
LL | binary_heap.into_iter().count();
151+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `binary_heap.len()`
152+
153+
error: aborting due to 25 previous errors
70154

0 commit comments

Comments
 (0)