fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

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