]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/iter_on_empty_collections.fixed
Auto merge of #101969 - reez12g:issue-101306, r=reez12g
[rust.git] / src / tools / clippy / tests / ui / iter_on_empty_collections.fixed
1 // run-rustfix
2 #![warn(clippy::iter_on_empty_collections)]
3 #![allow(clippy::iter_next_slice, clippy::redundant_clone)]
4
5 fn array() {
6     assert_eq!(std::iter::empty().next(), Option::<i32>::None);
7     assert_eq!(std::iter::empty().next(), Option::<&mut i32>::None);
8     assert_eq!(std::iter::empty().next(), Option::<&i32>::None);
9     assert_eq!(std::iter::empty().next(), Option::<i32>::None);
10     assert_eq!(std::iter::empty().next(), Option::<&mut i32>::None);
11     assert_eq!(std::iter::empty().next(), Option::<&i32>::None);
12
13     // Don't trigger on non-iter methods
14     let _: Option<String> = None.clone();
15     let _: [String; 0] = [].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!([].into_iter().next(), Option::<i32>::None);
29         assert_eq!([].iter_mut().next(), Option::<&mut i32>::None);
30         assert_eq!([].iter().next(), Option::<&i32>::None);
31         assert_eq!(None.into_iter().next(), Option::<i32>::None);
32         assert_eq!(None.iter_mut().next(), Option::<&mut i32>::None);
33         assert_eq!(None.iter().next(), Option::<&i32>::None);
34     };
35 }
36
37 // Don't trigger on a `None` 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         None.iter();
54         None.iter_mut();
55         None.into_iter();
56     }
57 }
58
59 fn main() {
60     array();
61     custom_option::custom_option();
62     in_macros!();
63 }