]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/manual_find_fixable.rs
New upstream version 1.71.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / manual_find_fixable.rs
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 for &v in ARRAY {
12 if v == n {
13 return Some(v);
14 }
15 }
16 None
17 }
18
19 fn with_pat(arr: Vec<(u32, u32)>) -> Option<u32> {
20 for (a, _) in arr {
21 if a % 2 == 0 {
22 return Some(a);
23 }
24 }
25 None
26 }
27
28 struct Data {
29 name: String,
30 is_true: bool,
31 }
32 fn with_struct(arr: Vec<Data>) -> Option<Data> {
33 for el in arr {
34 if el.name.len() == 10 {
35 return Some(el);
36 }
37 }
38 None
39 }
40
41 struct Tuple(usize, usize);
42 fn with_tuple_struct(arr: Vec<Tuple>) -> Option<usize> {
43 for Tuple(a, _) in arr {
44 if a >= 3 {
45 return Some(a);
46 }
47 }
48 None
49 }
50
51 struct A;
52 impl A {
53 fn should_keep(&self) -> bool {
54 true
55 }
56 }
57 fn with_method_call(arr: Vec<A>) -> Option<A> {
58 for el in arr {
59 if el.should_keep() {
60 return Some(el);
61 }
62 }
63 None
64 }
65
66 fn with_closure(arr: Vec<u32>) -> Option<u32> {
67 let f = |el: u32| -> u32 { el + 10 };
68 for el in arr {
69 if f(el) == 20 {
70 return Some(el);
71 }
72 }
73 None
74 }
75
76 fn with_closure2(arr: HashMap<String, i32>) -> Option<i32> {
77 let f = |el: i32| -> bool { el == 10 };
78 for &el in arr.values() {
79 if f(el) {
80 return Some(el);
81 }
82 }
83 None
84 }
85
86 fn with_bool(arr: Vec<Data>) -> Option<Data> {
87 for el in arr {
88 if el.is_true {
89 return Some(el);
90 }
91 }
92 None
93 }
94
95 fn with_side_effects(arr: Vec<u32>) -> Option<u32> {
96 for v in arr {
97 if v == 1 {
98 println!("side effect");
99 return Some(v);
100 }
101 }
102 None
103 }
104
105 fn with_else(arr: Vec<u32>) -> Option<u32> {
106 for el in arr {
107 if el % 2 == 0 {
108 return Some(el);
109 } else {
110 println!("{}", el);
111 }
112 }
113 None
114 }
115
116 fn tuple_with_ref(v: [(u8, &u8); 3]) -> Option<u8> {
117 for (_, &x) in v {
118 if x > 10 {
119 return Some(x);
120 }
121 }
122 None
123 }
124
125 fn ref_to_tuple_with_ref(v: &[(u8, &u8)]) -> Option<u8> {
126 for &(_, &x) in v {
127 if x > 10 {
128 return Some(x);
129 }
130 }
131 None
132 }
133
134 fn explicit_ret(arr: Vec<i32>) -> Option<i32> {
135 for x in arr {
136 if x >= 5 {
137 return Some(x);
138 }
139 }
140 return None;
141 }
142
143 fn plus_one(a: i32) -> Option<i32> {
144 Some(a + 1)
145 }
146 fn fn_instead_of_some(a: &[i32]) -> Option<i32> {
147 for &x in a {
148 if x == 1 {
149 return plus_one(x);
150 }
151 }
152 None
153 }
154
155 fn for_in_condition(a: &[i32], b: bool) -> Option<i32> {
156 if b {
157 for &x in a {
158 if x == 1 {
159 return Some(x);
160 }
161 }
162 }
163 None
164 }
165
166 fn intermediate_statements(a: &[i32]) -> Option<i32> {
167 for &x in a {
168 if x == 1 {
169 return Some(x);
170 }
171 }
172
173 println!("side effect");
174
175 None
176 }
177
178 fn mixed_binding_modes(arr: Vec<(i32, String)>) -> Option<i32> {
179 for (x, mut s) in arr {
180 if x == 1 && s.as_mut_str().len() == 2 {
181 return Some(x);
182 }
183 }
184 None
185 }
186
187 fn as_closure() {
188 #[rustfmt::skip]
189 let f = |arr: Vec<i32>| -> Option<i32> {
190 for x in arr {
191 if x < 1 {
192 return Some(x);
193 }
194 }
195 None
196 };
197 }
198
199 fn in_block(a: &[i32]) -> Option<i32> {
200 let should_be_none = {
201 for &x in a {
202 if x == 1 {
203 return Some(x);
204 }
205 }
206 None
207 };
208
209 assert!(should_be_none.is_none());
210
211 should_be_none
212 }
213
214 // Not handled yet
215 fn mut_binding(v: Vec<String>) -> Option<String> {
216 for mut s in v {
217 if s.as_mut_str().len() > 1 {
218 return Some(s);
219 }
220 }
221 None
222 }
223
224 fn subpattern(v: Vec<[u32; 32]>) -> Option<[u32; 32]> {
225 for a @ [first, ..] in v {
226 if a[12] == first {
227 return Some(a);
228 }
229 }
230 None
231 }
232
233 fn two_bindings(v: Vec<(u8, u8)>) -> Option<u8> {
234 for (a, n) in v {
235 if a == n {
236 return Some(a);
237 }
238 }
239 None
240 }
241
242 fn main() {}