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
170
171
172
173
174
175
176
177
178
179
180
181
|
#![warn(clippy::manual_find)]
#![allow(unused)]
#![allow(clippy::needless_return, clippy::uninlined_format_args)]
use std::collections::HashMap;
const ARRAY: &[u32; 5] = &[2, 7, 1, 9, 3];
fn lookup(n: u32) -> Option<u32> {
ARRAY.iter().find(|&&v| v == n).copied()
}
fn with_pat(arr: Vec<(u32, u32)>) -> Option<u32> {
arr.into_iter().map(|(a, _)| a).find(|&a| a % 2 == 0)
}
struct Data {
name: String,
is_true: bool,
}
fn with_struct(arr: Vec<Data>) -> Option<Data> {
arr.into_iter().find(|el| el.name.len() == 10)
}
struct Tuple(usize, usize);
fn with_tuple_struct(arr: Vec<Tuple>) -> Option<usize> {
arr.into_iter().map(|Tuple(a, _)| a).find(|&a| a >= 3)
}
struct A;
impl A {
fn should_keep(&self) -> bool {
true
}
}
fn with_method_call(arr: Vec<A>) -> Option<A> {
arr.into_iter().find(|el| el.should_keep())
}
fn with_closure(arr: Vec<u32>) -> Option<u32> {
let f = |el: u32| -> u32 { el + 10 };
arr.into_iter().find(|&el| f(el) == 20)
}
fn with_closure2(arr: HashMap<String, i32>) -> Option<i32> {
let f = |el: i32| -> bool { el == 10 };
arr.values().find(|&&el| f(el)).copied()
}
fn with_bool(arr: Vec<Data>) -> Option<Data> {
arr.into_iter().find(|el| el.is_true)
}
fn with_side_effects(arr: Vec<u32>) -> Option<u32> {
for v in arr {
if v == 1 {
println!("side effect");
return Some(v);
}
}
None
}
fn with_else(arr: Vec<u32>) -> Option<u32> {
for el in arr {
if el % 2 == 0 {
return Some(el);
} else {
println!("{}", el);
}
}
None
}
fn tuple_with_ref(v: [(u8, &u8); 3]) -> Option<u8> {
v.into_iter().map(|(_, &x)| x).find(|&x| x > 10)
}
fn ref_to_tuple_with_ref(v: &[(u8, &u8)]) -> Option<u8> {
v.iter().map(|&(_, &x)| x).find(|&x| x > 10)
}
fn explicit_ret(arr: Vec<i32>) -> Option<i32> {
arr.into_iter().find(|&x| x >= 5)
}
fn plus_one(a: i32) -> Option<i32> {
Some(a + 1)
}
fn fn_instead_of_some(a: &[i32]) -> Option<i32> {
for &x in a {
if x == 1 {
return plus_one(x);
}
}
None
}
fn for_in_condition(a: &[i32], b: bool) -> Option<i32> {
if b {
for &x in a {
if x == 1 {
return Some(x);
}
}
}
None
}
fn intermediate_statements(a: &[i32]) -> Option<i32> {
for &x in a {
if x == 1 {
return Some(x);
}
}
println!("side effect");
None
}
fn mixed_binding_modes(arr: Vec<(i32, String)>) -> Option<i32> {
for (x, mut s) in arr {
if x == 1 && s.as_mut_str().len() == 2 {
return Some(x);
}
}
None
}
fn as_closure() {
#[rustfmt::skip]
let f = |arr: Vec<i32>| -> Option<i32> {
arr.into_iter().find(|&x| x < 1)
};
}
fn in_block(a: &[i32]) -> Option<i32> {
let should_be_none = {
for &x in a {
if x == 1 {
return Some(x);
}
}
None
};
assert!(should_be_none.is_none());
should_be_none
}
// Not handled yet
fn mut_binding(v: Vec<String>) -> Option<String> {
for mut s in v {
if s.as_mut_str().len() > 1 {
return Some(s);
}
}
None
}
fn subpattern(v: Vec<[u32; 32]>) -> Option<[u32; 32]> {
for a @ [first, ..] in v {
if a[12] == first {
return Some(a);
}
}
None
}
fn two_bindings(v: Vec<(u8, u8)>) -> Option<u8> {
for (a, n) in v {
if a == n {
return Some(a);
}
}
None
}
fn main() {}
|