]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/unused_peekable.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / src / tools / clippy / 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 }
61
62 let peekable_consumer = PeekableConsumer;
63 let mut passed_along_to_method = std::iter::empty::<u32>().peekable();
64 peekable_consumer.consume_mut_ref(&mut passed_along_to_method);
65 peekable_consumer.consume(passed_along_to_method);
66
67 // `peek` called in another block
68 let mut peekable_in_block = std::iter::empty::<u32>().peekable();
69 {
70 peekable_in_block.peek();
71 }
72
73 // Check the other `Peekable` methods :)
74 {
75 let mut peekable_with_peek_mut = std::iter::empty::<u32>().peekable();
76 peekable_with_peek_mut.peek_mut();
77
78 let mut peekable_with_next_if = std::iter::empty::<u32>().peekable();
79 peekable_with_next_if.next_if(|_| true);
80
81 let mut peekable_with_next_if_eq = std::iter::empty::<u32>().peekable();
82 peekable_with_next_if_eq.next_if_eq(&3);
83 }
84
85 let mut peekable_in_closure = std::iter::empty::<u32>().peekable();
86 let call_peek = |p: &mut Peekable<Empty<u32>>| {
87 p.peek();
88 };
89 call_peek(&mut peekable_in_closure);
90
91 // From a macro
92 macro_rules! make_me_a_peekable_please {
93 () => {
94 std::iter::empty::<u32>().peekable()
95 };
96 }
97
98 let _unsuspecting_macro_user = make_me_a_peekable_please!();
99
100 // Generic Iterator returned
101 fn return_an_iter() -> impl Iterator<Item = u32> {
102 std::iter::empty::<u32>().peekable()
103 }
104
105 let _unsuspecting_user = return_an_iter();
106
107 // Call `peek` in a macro
108 macro_rules! peek_iter {
109 ($iter:ident) => {
110 $iter.peek();
111 };
112 }
113
114 let mut peek_in_macro = std::iter::empty::<u32>().peekable();
115 peek_iter!(peek_in_macro);
116
117 // Behind mut ref
118 let mut by_mut_ref_test = std::iter::empty::<u32>().peekable();
119 let by_mut_ref = &mut by_mut_ref_test;
120 by_mut_ref.peek();
121
122 // Behind ref
123 let mut by_ref_test = std::iter::empty::<u32>().peekable();
124 let by_ref = &by_ref_test;
125 by_ref_test.peek();
126
127 // In struct
128 struct PeekableWrapper {
129 f: Peekable<Empty<u32>>,
130 }
131
132 let struct_test = std::iter::empty::<u32>().peekable();
133 PeekableWrapper { f: struct_test };
134
135 // `by_ref` before `peek`
136 let mut by_ref_test = std::iter::empty::<u32>().peekable();
137 let peeked_val = by_ref_test.by_ref().peek();
138
139 // `peek` called in another block as the last expression
140 let mut peekable_last_expr = std::iter::empty::<u32>().peekable();
141 {
142 peekable_last_expr.peek();
143 }
144 }