]> git.lizzy.rs Git - rust.git/blob - tests/ui/unused_peekable.rs
Auto merge of #10094 - EricWu2003:increment-visitor-fix, r=xFrednet
[rust.git] / tests / ui / unused_peekable.rs
1 #![warn(clippy::unused_peekable)]
2 #![allow(clippy::no_effect)]
3
4 use std::iter::Empty;
5 use std::iter::Peekable;
6
7 fn main() {
8     invalid();
9     valid();
10 }
11
12 #[allow(clippy::unused_unit)]
13 fn invalid() {
14     let peekable = std::iter::empty::<u32>().peekable();
15
16     // Only lint `new_local`
17     let old_local = std::iter::empty::<u32>().peekable();
18     let new_local = old_local;
19
20     // Behind mut ref
21     let mut by_mut_ref_test = std::iter::empty::<u32>().peekable();
22     let by_mut_ref = &mut by_mut_ref_test;
23
24     // Explicitly returns `Peekable`
25     fn returns_peekable() -> Peekable<Empty<u32>> {
26         std::iter::empty().peekable()
27     }
28
29     let peekable_from_fn = returns_peekable();
30
31     // Using a method not exclusive to `Peekable`
32     let mut peekable_using_iterator_method = std::iter::empty::<u32>().peekable();
33     peekable_using_iterator_method.next();
34
35     // Passed by ref to another function
36     fn takes_ref(_peek: &Peekable<Empty<u32>>) {}
37     let passed_along_ref = std::iter::empty::<u32>().peekable();
38     takes_ref(&passed_along_ref);
39
40     // `by_ref` without `peek`
41     let mut by_ref_test = std::iter::empty::<u32>().peekable();
42     let _by_ref = by_ref_test.by_ref();
43
44     let mut peekable_in_for_loop = std::iter::empty::<u32>().peekable();
45     for x in peekable_in_for_loop {}
46 }
47
48 fn valid() {
49     fn takes_peekable(_peek: Peekable<Empty<u32>>) {}
50
51     // Passed to another function
52     let passed_along = std::iter::empty::<u32>().peekable();
53     takes_peekable(passed_along);
54
55     // Passed to another method
56     struct PeekableConsumer;
57     impl PeekableConsumer {
58         fn consume(&self, _: Peekable<Empty<u32>>) {}
59         fn consume_mut_ref(&self, _: &mut Peekable<Empty<u32>>) {}
60         fn consume_assoc(_: Peekable<Empty<u32>>) {}
61         fn consume_assoc_mut_ref(_: &mut Peekable<Empty<u32>>) {}
62     }
63     let peekable_consumer = PeekableConsumer;
64
65     let peekable = std::iter::empty::<u32>().peekable();
66     peekable_consumer.consume(peekable);
67
68     let mut peekable = std::iter::empty::<u32>().peekable();
69     peekable_consumer.consume_mut_ref(&mut peekable);
70
71     let peekable = std::iter::empty::<u32>().peekable();
72     PeekableConsumer::consume_assoc(peekable);
73
74     let mut peekable = std::iter::empty::<u32>().peekable();
75     PeekableConsumer::consume_assoc_mut_ref(&mut peekable);
76
77     // `peek` called in another block
78     let mut peekable_in_block = std::iter::empty::<u32>().peekable();
79     {
80         peekable_in_block.peek();
81     }
82
83     // Check the other `Peekable` methods :)
84     {
85         let mut peekable_with_peek_mut = std::iter::empty::<u32>().peekable();
86         peekable_with_peek_mut.peek_mut();
87
88         let mut peekable_with_next_if = std::iter::empty::<u32>().peekable();
89         peekable_with_next_if.next_if(|_| true);
90
91         let mut peekable_with_next_if_eq = std::iter::empty::<u32>().peekable();
92         peekable_with_next_if_eq.next_if_eq(&3);
93     }
94
95     let mut peekable_in_closure = std::iter::empty::<u32>().peekable();
96     let call_peek = |p: &mut Peekable<Empty<u32>>| {
97         p.peek();
98     };
99     call_peek(&mut peekable_in_closure);
100
101     // From a macro
102     macro_rules! make_me_a_peekable_please {
103         () => {
104             std::iter::empty::<u32>().peekable()
105         };
106     }
107
108     let _unsuspecting_macro_user = make_me_a_peekable_please!();
109
110     // Generic Iterator returned
111     fn return_an_iter() -> impl Iterator<Item = u32> {
112         std::iter::empty::<u32>().peekable()
113     }
114
115     let _unsuspecting_user = return_an_iter();
116
117     // Call `peek` in a macro
118     macro_rules! peek_iter {
119         ($iter:ident) => {
120             $iter.peek();
121         };
122     }
123
124     let mut peek_in_macro = std::iter::empty::<u32>().peekable();
125     peek_iter!(peek_in_macro);
126
127     // Behind mut ref
128     let mut by_mut_ref_test = std::iter::empty::<u32>().peekable();
129     let by_mut_ref = &mut by_mut_ref_test;
130     by_mut_ref.peek();
131
132     // Behind ref
133     let mut by_ref_test = std::iter::empty::<u32>().peekable();
134     let by_ref = &by_ref_test;
135     by_ref_test.peek();
136
137     // In struct
138     struct PeekableWrapper {
139         f: Peekable<Empty<u32>>,
140     }
141
142     let struct_test = std::iter::empty::<u32>().peekable();
143     PeekableWrapper { f: struct_test };
144
145     // `by_ref` before `peek`
146     let mut by_ref_test = std::iter::empty::<u32>().peekable();
147     let peeked_val = by_ref_test.by_ref().peek();
148
149     // `peek` called in another block as the last expression
150     let mut peekable_last_expr = std::iter::empty::<u32>().peekable();
151     {
152         peekable_last_expr.peek();
153     }
154
155     let mut peek_in_closure = std::iter::empty::<u32>().peekable();
156     let _ = || {
157         let _ = peek_in_closure.peek();
158     };
159
160     trait PeekTrait {}
161     impl<I> PeekTrait for Peekable<I> where I: Iterator {}
162
163     let mut peekable = std::iter::empty::<u32>().peekable();
164     let _dyn = &mut peekable as &mut dyn PeekTrait;
165
166     fn takes_dyn(_: &mut dyn PeekTrait) {}
167     let mut peekable = std::iter::empty::<u32>().peekable();
168     takes_dyn(&mut peekable);
169 }