]> git.lizzy.rs Git - rust.git/blob - tests/ui/unnecessary_filter_map.rs
`assertions_on_result_states` fix suggestion when `assert!` not in a statement
[rust.git] / tests / ui / unnecessary_filter_map.rs
1 #![allow(dead_code)]
2
3 fn main() {
4     let _ = (0..4).filter_map(|x| if x > 1 { Some(x) } else { None });
5     let _ = (0..4).filter_map(|x| {
6         if x > 1 {
7             return Some(x);
8         };
9         None
10     });
11     let _ = (0..4).filter_map(|x| match x {
12         0 | 1 => None,
13         _ => Some(x),
14     });
15
16     let _ = (0..4).filter_map(|x| Some(x + 1));
17
18     let _ = (0..4).filter_map(i32::checked_abs);
19 }
20
21 fn filter_map_none_changes_item_type() -> impl Iterator<Item = bool> {
22     "".chars().filter_map(|_| None)
23 }
24
25 // https://github.com/rust-lang/rust-clippy/issues/4433#issue-483920107
26 mod comment_483920107 {
27     enum Severity {
28         Warning,
29         Other,
30     }
31
32     struct ServerError;
33
34     impl ServerError {
35         fn severity(&self) -> Severity {
36             Severity::Warning
37         }
38     }
39
40     struct S {
41         warnings: Vec<ServerError>,
42     }
43
44     impl S {
45         fn foo(&mut self, server_errors: Vec<ServerError>) {
46             #[allow(unused_variables)]
47             let errors: Vec<ServerError> = server_errors
48                 .into_iter()
49                 .filter_map(|se| match se.severity() {
50                     Severity::Warning => {
51                         self.warnings.push(se);
52                         None
53                     },
54                     _ => Some(se),
55                 })
56                 .collect();
57         }
58     }
59 }
60
61 // https://github.com/rust-lang/rust-clippy/issues/4433#issuecomment-611006622
62 mod comment_611006622 {
63     struct PendingRequest {
64         reply_to: u8,
65         token: u8,
66         expires: u8,
67         group_id: u8,
68     }
69
70     enum Value {
71         Null,
72     }
73
74     struct Node;
75
76     impl Node {
77         fn send_response(&self, _reply_to: u8, _token: u8, _value: Value) -> &Self {
78             self
79         }
80         fn on_error_warn(&self) -> &Self {
81             self
82         }
83     }
84
85     struct S {
86         pending_requests: Vec<PendingRequest>,
87     }
88
89     impl S {
90         fn foo(&mut self, node: Node, now: u8, group_id: u8) {
91             // "drain_filter"
92             self.pending_requests = self
93                 .pending_requests
94                 .drain(..)
95                 .filter_map(|pending| {
96                     if pending.expires <= now {
97                         return None; // Expired, remove
98                     }
99
100                     if pending.group_id == group_id {
101                         // Matched - reuse strings and remove
102                         node.send_response(pending.reply_to, pending.token, Value::Null)
103                             .on_error_warn();
104                         None
105                     } else {
106                         // Keep waiting
107                         Some(pending)
108                     }
109                 })
110                 .collect();
111         }
112     }
113 }
114
115 // https://github.com/rust-lang/rust-clippy/issues/4433#issuecomment-621925270
116 // This extrapolation doesn't reproduce the false positive. Additional context seems necessary.
117 mod comment_621925270 {
118     struct Signature(u8);
119
120     fn foo(sig_packets: impl Iterator<Item = Result<Signature, ()>>) -> impl Iterator<Item = u8> {
121         sig_packets.filter_map(|res| match res {
122             Ok(Signature(sig_packet)) => Some(sig_packet),
123             _ => None,
124         })
125     }
126 }
127
128 // https://github.com/rust-lang/rust-clippy/issues/4433#issuecomment-1052978898
129 mod comment_1052978898 {
130     #![allow(clippy::redundant_closure)]
131
132     pub struct S(u8);
133
134     impl S {
135         pub fn consume(self) {
136             println!("yum");
137         }
138     }
139
140     pub fn filter_owned() -> impl Iterator<Item = S> {
141         (0..10).map(|i| S(i)).filter_map(|s| {
142             if s.0 & 1 == 0 {
143                 s.consume();
144                 None
145             } else {
146                 Some(s)
147             }
148         })
149     }
150 }