]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/manual_find_fixable.fixed
Auto merge of #102026 - Bryanskiy:resolve_update, r=petrochenkov
[rust.git] / src / tools / clippy / tests / ui / manual_find_fixable.fixed
1 // run-rustfix
2 #![warn(clippy::manual_find)]
3 #![allow(unused)]
4 #![allow(clippy::needless_return, clippy::uninlined_format_args)]
5
6 use std::collections::HashMap;
7
8 const ARRAY: &[u32; 5] = &[2, 7, 1, 9, 3];
9
10 fn lookup(n: u32) -> Option<u32> {
11     ARRAY.iter().find(|&&v| v == n).copied()
12 }
13
14 fn with_pat(arr: Vec<(u32, u32)>) -> Option<u32> {
15     arr.into_iter().map(|(a, _)| a).find(|&a| a % 2 == 0)
16 }
17
18 struct Data {
19     name: String,
20     is_true: bool,
21 }
22 fn with_struct(arr: Vec<Data>) -> Option<Data> {
23     arr.into_iter().find(|el| el.name.len() == 10)
24 }
25
26 struct Tuple(usize, usize);
27 fn with_tuple_struct(arr: Vec<Tuple>) -> Option<usize> {
28     arr.into_iter().map(|Tuple(a, _)| a).find(|&a| a >= 3)
29 }
30
31 struct A;
32 impl A {
33     fn should_keep(&self) -> bool {
34         true
35     }
36 }
37 fn with_method_call(arr: Vec<A>) -> Option<A> {
38     arr.into_iter().find(|el| el.should_keep())
39 }
40
41 fn with_closure(arr: Vec<u32>) -> Option<u32> {
42     let f = |el: u32| -> u32 { el + 10 };
43     arr.into_iter().find(|&el| f(el) == 20)
44 }
45
46 fn with_closure2(arr: HashMap<String, i32>) -> Option<i32> {
47     let f = |el: i32| -> bool { el == 10 };
48     arr.values().find(|&&el| f(el)).copied()
49 }
50
51 fn with_bool(arr: Vec<Data>) -> Option<Data> {
52     arr.into_iter().find(|el| el.is_true)
53 }
54
55 fn with_side_effects(arr: Vec<u32>) -> Option<u32> {
56     for v in arr {
57         if v == 1 {
58             println!("side effect");
59             return Some(v);
60         }
61     }
62     None
63 }
64
65 fn with_else(arr: Vec<u32>) -> Option<u32> {
66     for el in arr {
67         if el % 2 == 0 {
68             return Some(el);
69         } else {
70             println!("{}", el);
71         }
72     }
73     None
74 }
75
76 fn tuple_with_ref(v: [(u8, &u8); 3]) -> Option<u8> {
77     v.into_iter().map(|(_, &x)| x).find(|&x| x > 10)
78 }
79
80 fn ref_to_tuple_with_ref(v: &[(u8, &u8)]) -> Option<u8> {
81     v.iter().map(|&(_, &x)| x).find(|&x| x > 10)
82 }
83
84 fn explicit_ret(arr: Vec<i32>) -> Option<i32> {
85     arr.into_iter().find(|&x| x >= 5)
86 }
87
88 fn plus_one(a: i32) -> Option<i32> {
89     Some(a + 1)
90 }
91 fn fn_instead_of_some(a: &[i32]) -> Option<i32> {
92     for &x in a {
93         if x == 1 {
94             return plus_one(x);
95         }
96     }
97     None
98 }
99
100 fn for_in_condition(a: &[i32], b: bool) -> Option<i32> {
101     if b {
102         for &x in a {
103             if x == 1 {
104                 return Some(x);
105             }
106         }
107     }
108     None
109 }
110
111 fn intermediate_statements(a: &[i32]) -> Option<i32> {
112     for &x in a {
113         if x == 1 {
114             return Some(x);
115         }
116     }
117
118     println!("side effect");
119
120     None
121 }
122
123 fn mixed_binding_modes(arr: Vec<(i32, String)>) -> Option<i32> {
124     for (x, mut s) in arr {
125         if x == 1 && s.as_mut_str().len() == 2 {
126             return Some(x);
127         }
128     }
129     None
130 }
131
132 fn as_closure() {
133     #[rustfmt::skip]
134     let f = |arr: Vec<i32>| -> Option<i32> {
135         arr.into_iter().find(|&x| x < 1)
136     };
137 }
138
139 fn in_block(a: &[i32]) -> Option<i32> {
140     let should_be_none = {
141         for &x in a {
142             if x == 1 {
143                 return Some(x);
144             }
145         }
146         None
147     };
148
149     assert!(should_be_none.is_none());
150
151     should_be_none
152 }
153
154 // Not handled yet
155 fn mut_binding(v: Vec<String>) -> Option<String> {
156     for mut s in v {
157         if s.as_mut_str().len() > 1 {
158             return Some(s);
159         }
160     }
161     None
162 }
163
164 fn subpattern(v: Vec<[u32; 32]>) -> Option<[u32; 32]> {
165     for a @ [first, ..] in v {
166         if a[12] == first {
167             return Some(a);
168         }
169     }
170     None
171 }
172
173 fn two_bindings(v: Vec<(u8, u8)>) -> Option<u8> {
174     for (a, n) in v {
175         if a == n {
176             return Some(a);
177         }
178     }
179     None
180 }
181
182 fn main() {}