summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/unused_peekable.rs
blob: 7374dfdf92e87b5de8014c1cb27e01043cbcbe1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#![warn(clippy::unused_peekable)]
#![allow(clippy::no_effect)]

use std::iter::Empty;
use std::iter::Peekable;

fn main() {
    invalid();
    valid();
}

#[allow(clippy::unused_unit)]
fn invalid() {
    let peekable = std::iter::empty::<u32>().peekable();

    // Only lint `new_local`
    let old_local = std::iter::empty::<u32>().peekable();
    let new_local = old_local;

    // Behind mut ref
    let mut by_mut_ref_test = std::iter::empty::<u32>().peekable();
    let by_mut_ref = &mut by_mut_ref_test;

    // Explicitly returns `Peekable`
    fn returns_peekable() -> Peekable<Empty<u32>> {
        std::iter::empty().peekable()
    }

    let peekable_from_fn = returns_peekable();

    // Using a method not exclusive to `Peekable`
    let mut peekable_using_iterator_method = std::iter::empty::<u32>().peekable();
    peekable_using_iterator_method.next();

    // Passed by ref to another function
    fn takes_ref(_peek: &Peekable<Empty<u32>>) {}
    let passed_along_ref = std::iter::empty::<u32>().peekable();
    takes_ref(&passed_along_ref);

    // `by_ref` without `peek`
    let mut by_ref_test = std::iter::empty::<u32>().peekable();
    let _by_ref = by_ref_test.by_ref();

    let mut peekable_in_for_loop = std::iter::empty::<u32>().peekable();
    for x in peekable_in_for_loop {}
}

fn valid() {
    fn takes_peekable(_peek: Peekable<Empty<u32>>) {}

    // Passed to another function
    let passed_along = std::iter::empty::<u32>().peekable();
    takes_peekable(passed_along);

    // Passed to another method
    struct PeekableConsumer;
    impl PeekableConsumer {
        fn consume(&self, _: Peekable<Empty<u32>>) {}
        fn consume_mut_ref(&self, _: &mut Peekable<Empty<u32>>) {}
        fn consume_assoc(_: Peekable<Empty<u32>>) {}
        fn consume_assoc_mut_ref(_: &mut Peekable<Empty<u32>>) {}
    }
    let peekable_consumer = PeekableConsumer;

    let peekable = std::iter::empty::<u32>().peekable();
    peekable_consumer.consume(peekable);

    let mut peekable = std::iter::empty::<u32>().peekable();
    peekable_consumer.consume_mut_ref(&mut peekable);

    let peekable = std::iter::empty::<u32>().peekable();
    PeekableConsumer::consume_assoc(peekable);

    let mut peekable = std::iter::empty::<u32>().peekable();
    PeekableConsumer::consume_assoc_mut_ref(&mut peekable);

    // `peek` called in another block
    let mut peekable_in_block = std::iter::empty::<u32>().peekable();
    {
        peekable_in_block.peek();
    }

    // Check the other `Peekable` methods :)
    {
        let mut peekable_with_peek_mut = std::iter::empty::<u32>().peekable();
        peekable_with_peek_mut.peek_mut();

        let mut peekable_with_next_if = std::iter::empty::<u32>().peekable();
        peekable_with_next_if.next_if(|_| true);

        let mut peekable_with_next_if_eq = std::iter::empty::<u32>().peekable();
        peekable_with_next_if_eq.next_if_eq(&3);
    }

    let mut peekable_in_closure = std::iter::empty::<u32>().peekable();
    let call_peek = |p: &mut Peekable<Empty<u32>>| {
        p.peek();
    };
    call_peek(&mut peekable_in_closure);

    // From a macro
    macro_rules! make_me_a_peekable_please {
        () => {
            std::iter::empty::<u32>().peekable()
        };
    }

    let _unsuspecting_macro_user = make_me_a_peekable_please!();

    // Generic Iterator returned
    fn return_an_iter() -> impl Iterator<Item = u32> {
        std::iter::empty::<u32>().peekable()
    }

    let _unsuspecting_user = return_an_iter();

    // Call `peek` in a macro
    macro_rules! peek_iter {
        ($iter:ident) => {
            $iter.peek();
        };
    }

    let mut peek_in_macro = std::iter::empty::<u32>().peekable();
    peek_iter!(peek_in_macro);

    // Behind mut ref
    let mut by_mut_ref_test = std::iter::empty::<u32>().peekable();
    let by_mut_ref = &mut by_mut_ref_test;
    by_mut_ref.peek();

    // Behind ref
    let mut by_ref_test = std::iter::empty::<u32>().peekable();
    let by_ref = &by_ref_test;
    by_ref_test.peek();

    // In struct
    struct PeekableWrapper {
        f: Peekable<Empty<u32>>,
    }

    let struct_test = std::iter::empty::<u32>().peekable();
    PeekableWrapper { f: struct_test };

    // `by_ref` before `peek`
    let mut by_ref_test = std::iter::empty::<u32>().peekable();
    let peeked_val = by_ref_test.by_ref().peek();

    // `peek` called in another block as the last expression
    let mut peekable_last_expr = std::iter::empty::<u32>().peekable();
    {
        peekable_last_expr.peek();
    }

    let mut peek_in_closure = std::iter::empty::<u32>().peekable();
    let _ = || {
        let _ = peek_in_closure.peek();
    };

    trait PeekTrait {}
    impl<I> PeekTrait for Peekable<I> where I: Iterator {}

    let mut peekable = std::iter::empty::<u32>().peekable();
    let _dyn = &mut peekable as &mut dyn PeekTrait;

    fn takes_dyn(_: &mut dyn PeekTrait) {}
    let mut peekable = std::iter::empty::<u32>().peekable();
    takes_dyn(&mut peekable);
}