fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
2.7 KiB80 linesraw
1#![allow(unused, clippy::needless_if, clippy::suspicious_map, clippy::iter_count)]
2
3use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList};
4
5#[warn(clippy::needless_collect)]
6#[allow(unused_variables, clippy::iter_cloned_collect, clippy::iter_next_slice)]
7fn main() {
8    let sample = [1; 5];
9    let len = sample.iter().count();
10    if sample.iter().next().is_none() {
11        // Empty
12    }
13    sample.iter().cloned().any(|x| x == 1);
14    // #7164 HashMap's and BTreeMap's `len` usage should not be linted
15    sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
16    sample.iter().map(|x| (x, x)).collect::<BTreeMap<_, _>>().len();
17
18    sample.iter().map(|x| (x, x)).next().is_none();
19    sample.iter().map(|x| (x, x)).next().is_none();
20
21    // Notice the `HashSet`--this should not be linted
22    sample.iter().collect::<HashSet<_>>().len();
23    // Neither should this
24    sample.iter().collect::<BTreeSet<_>>().len();
25
26    sample.iter().count();
27    sample.iter().next().is_none();
28    sample.iter().cloned().any(|x| x == 1);
29    sample.iter().any(|x| x == &1);
30
31    // `BinaryHeap` doesn't have `contains` method
32    sample.iter().count();
33    sample.iter().next().is_none();
34
35    // Don't lint string from str
36    let _ = ["", ""].into_iter().collect::<String>().is_empty();
37
38    let _ = sample.iter().next().is_none();
39    let _ = sample.iter().any(|x| x == &0);
40
41    struct VecWrapper<T>(Vec<T>);
42    impl<T> core::ops::Deref for VecWrapper<T> {
43        type Target = Vec<T>;
44        fn deref(&self) -> &Self::Target {
45            &self.0
46        }
47    }
48    impl<T> IntoIterator for VecWrapper<T> {
49        type IntoIter = <Vec<T> as IntoIterator>::IntoIter;
50        type Item = <Vec<T> as IntoIterator>::Item;
51        fn into_iter(self) -> Self::IntoIter {
52            self.0.into_iter()
53        }
54    }
55    impl<T> FromIterator<T> for VecWrapper<T> {
56        fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
57            Self(Vec::from_iter(iter))
58        }
59    }
60
61    let _ = sample.iter().next().is_none();
62    let _ = sample.iter().any(|x| x == &0);
63
64    #[allow(clippy::double_parens)]
65    {
66        Vec::<u8>::new().extend((0..10));
67        foo((0..10));
68        bar((0..10).collect::<Vec<_>>(), (0..10));
69        baz((0..10), (), ('a'..='z'))
70    }
71
72    let values = [1, 2, 3, 4];
73    let mut out = vec![];
74    values.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>();
75    let _y = values.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
76}
77
78fn foo(_: impl IntoIterator<Item = usize>) {}
79fn bar<I: IntoIterator<Item = usize>>(_: Vec<usize>, _: I) {}
80fn baz<I: IntoIterator<Item = usize>>(_: I, _: (), _: impl IntoIterator<Item = char>) {}