Skip to content

Commit cfd3252

Browse files
committed
Add tests to tests/ui/unnecessary_filter_map.rs
The tests currently fail, and are fixed by the next commit.
1 parent 8d12cd4 commit cfd3252

File tree

2 files changed

+133
-4
lines changed

2 files changed

+133
-4
lines changed

tests/ui/unnecessary_filter_map.rs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(dead_code)]
2+
13
fn main() {
24
let _ = (0..4).filter_map(|x| if x > 1 { Some(x) } else { None });
35
let _ = (0..4).filter_map(|x| {
@@ -19,3 +21,130 @@ fn main() {
1921
fn filter_map_none_changes_item_type() -> impl Iterator<Item = bool> {
2022
"".chars().filter_map(|_| None)
2123
}
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+
}

tests/ui/unnecessary_filter_map.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: this `.filter_map` can be written more simply using `.filter`
2-
--> $DIR/unnecessary_filter_map.rs:2:13
2+
--> $DIR/unnecessary_filter_map.rs:4:13
33
|
44
LL | let _ = (0..4).filter_map(|x| if x > 1 { Some(x) } else { None });
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::unnecessary-filter-map` implied by `-D warnings`
88

99
error: this `.filter_map` can be written more simply using `.filter`
10-
--> $DIR/unnecessary_filter_map.rs:3:13
10+
--> $DIR/unnecessary_filter_map.rs:5:13
1111
|
1212
LL | let _ = (0..4).filter_map(|x| {
1313
| _____________^
@@ -19,7 +19,7 @@ LL | | });
1919
| |______^
2020

2121
error: this `.filter_map` can be written more simply using `.filter`
22-
--> $DIR/unnecessary_filter_map.rs:9:13
22+
--> $DIR/unnecessary_filter_map.rs:11:13
2323
|
2424
LL | let _ = (0..4).filter_map(|x| match x {
2525
| _____________^
@@ -29,7 +29,7 @@ LL | | });
2929
| |______^
3030

3131
error: this `.filter_map` can be written more simply using `.map`
32-
--> $DIR/unnecessary_filter_map.rs:14:13
32+
--> $DIR/unnecessary_filter_map.rs:16:13
3333
|
3434
LL | let _ = (0..4).filter_map(|x| Some(x + 1));
3535
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)