]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/iter_on_single_items.rs
Rollup merge of #102092 - kxxt:patch-1, r=joshtriplett
[rust.git] / src / tools / clippy / tests / ui / iter_on_single_items.rs
1 // run-rustfix
2 #![warn(clippy::iter_on_single_items)]
3 #![allow(clippy::iter_next_slice, clippy::redundant_clone)]
4
5 fn array() {
6     assert_eq!([123].into_iter().next(), Some(123));
7     assert_eq!([123].iter_mut().next(), Some(&mut 123));
8     assert_eq!([123].iter().next(), Some(&123));
9     assert_eq!(Some(123).into_iter().next(), Some(123));
10     assert_eq!(Some(123).iter_mut().next(), Some(&mut 123));
11     assert_eq!(Some(123).iter().next(), Some(&123));
12
13     // Don't trigger on non-iter methods
14     let _: Option<String> = Some("test".to_string()).clone();
15     let _: [String; 1] = ["test".to_string()].clone();
16
17     // Don't trigger on match or if branches
18     let _ = match 123 {
19         123 => [].iter(),
20         _ => ["test"].iter(),
21     };
22
23     let _ = if false { ["test"].iter() } else { [].iter() };
24 }
25
26 macro_rules! in_macros {
27     () => {
28         assert_eq!([123].into_iter().next(), Some(123));
29         assert_eq!([123].iter_mut().next(), Some(&mut 123));
30         assert_eq!([123].iter().next(), Some(&123));
31         assert_eq!(Some(123).into_iter().next(), Some(123));
32         assert_eq!(Some(123).iter_mut().next(), Some(&mut 123));
33         assert_eq!(Some(123).iter().next(), Some(&123));
34     };
35 }
36
37 // Don't trigger on a `Some` that isn't std's option
38 mod custom_option {
39     #[allow(unused)]
40     enum CustomOption {
41         Some(i32),
42         None,
43     }
44
45     impl CustomOption {
46         fn iter(&self) {}
47         fn iter_mut(&mut self) {}
48         fn into_iter(self) {}
49     }
50     use CustomOption::*;
51
52     pub fn custom_option() {
53         Some(3).iter();
54         Some(3).iter_mut();
55         Some(3).into_iter();
56     }
57 }
58
59 fn main() {
60     array();
61     custom_option::custom_option();
62     in_macros!();
63 }