]> git.lizzy.rs Git - rust.git/blob - tests/ui/search_is_some_fixable_some.fixed
Auto merge of #8030 - WaffleLapkin:ignore_trait_assoc_types_type_complexity, r=llogiq
[rust.git] / tests / ui / search_is_some_fixable_some.fixed
1 // run-rustfix
2 #![allow(dead_code)]
3 #![warn(clippy::search_is_some)]
4
5 fn main() {
6     let v = vec![3, 2, 1, 0, -1, -2, -3];
7     let y = &&42;
8
9     // Check `find().is_some()`, single-line case.
10     let _ = v.iter().any(|x| *x < 0);
11     let _ = (0..1).any(|x| **y == x); // one dereference less
12     let _ = (0..1).any(|x| x == 0);
13     let _ = v.iter().any(|x| *x == 0);
14     let _ = (4..5).any(|x| x == 1 || x == 3 || x == 5);
15     let _ = (1..3).any(|x| [1, 2, 3].contains(&x));
16     let _ = (1..3).any(|x| x == 0 || [1, 2, 3].contains(&x));
17     let _ = (1..3).any(|x| [1, 2, 3].contains(&x) || x == 0);
18     let _ = (1..3)
19         .any(|x| [1, 2, 3].contains(&x) || x == 0 || [4, 5, 6].contains(&x) || x == -1);
20
21     // Check `position().is_some()`, single-line case.
22     let _ = v.iter().any(|&x| x < 0);
23
24     // Check `rposition().is_some()`, single-line case.
25     let _ = v.iter().any(|&x| x < 0);
26
27     let s1 = String::from("hello world");
28     let s2 = String::from("world");
29     // caller of `find()` is a `&`static str`
30     let _ = "hello world".contains("world");
31     let _ = "hello world".contains(&s2);
32     let _ = "hello world".contains(&s2[2..]);
33     // caller of `find()` is a `String`
34     let _ = s1.contains("world");
35     let _ = s1.contains(&s2);
36     let _ = s1.contains(&s2[2..]);
37     // caller of `find()` is slice of `String`
38     let _ = s1[2..].contains("world");
39     let _ = s1[2..].contains(&s2);
40     let _ = s1[2..].contains(&s2[2..]);
41 }
42
43 #[allow(clippy::clone_on_copy, clippy::map_clone)]
44 mod issue7392 {
45     struct Player {
46         hand: Vec<usize>,
47     }
48     fn filter() {
49         let p = Player {
50             hand: vec![1, 2, 3, 4, 5],
51         };
52         let filter_hand = vec![5];
53         let _ = p
54             .hand
55             .iter()
56             .filter(|c| filter_hand.iter().any(|cc| c == &cc))
57             .map(|c| c.clone())
58             .collect::<Vec<_>>();
59     }
60
61     struct PlayerTuple {
62         hand: Vec<(usize, char)>,
63     }
64     fn filter_tuple() {
65         let p = PlayerTuple {
66             hand: vec![(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')],
67         };
68         let filter_hand = vec![5];
69         let _ = p
70             .hand
71             .iter()
72             .filter(|(c, _)| filter_hand.iter().any(|cc| c == cc))
73             .map(|c| c.clone())
74             .collect::<Vec<_>>();
75     }
76
77     fn field_projection() {
78         struct Foo {
79             foo: i32,
80             bar: u32,
81         }
82         let vfoo = vec![Foo { foo: 1, bar: 2 }];
83         let _ = vfoo.iter().any(|v| v.foo == 1 && v.bar == 2);
84
85         let vfoo = vec![(42, Foo { foo: 1, bar: 2 })];
86         let _ = vfoo
87             .iter()
88             .any(|(i, v)| *i == 42 && v.foo == 1 && v.bar == 2);
89     }
90
91     fn index_projection() {
92         let vfoo = vec![[0, 1, 2, 3]];
93         let _ = vfoo.iter().any(|a| a[0] == 42);
94     }
95
96     #[allow(clippy::match_like_matches_macro)]
97     fn slice_projection() {
98         let vfoo = vec![[0, 1, 2, 3, 0, 1, 2, 3]];
99         let _ = vfoo.iter().any(|sub| sub[1..4].len() == 3);
100     }
101
102     fn please(x: &u32) -> bool {
103         *x == 9
104     }
105
106     fn deref_enough(x: u32) -> bool {
107         x == 78
108     }
109
110     fn arg_no_deref(x: &&u32) -> bool {
111         **x == 78
112     }
113
114     fn more_projections() {
115         let x = 19;
116         let ppx: &u32 = &x;
117         let _ = [ppx].iter().any(|ppp_x: &&u32| please(ppp_x));
118         let _ = [String::from("Hey hey")].iter().any(|s| s.len() == 2);
119
120         let v = vec![3, 2, 1, 0];
121         let _ = v.iter().any(|x| deref_enough(*x));
122         let _ = v.iter().any(|x: &u32| deref_enough(*x));
123
124         #[allow(clippy::redundant_closure)]
125         let _ = v.iter().any(|x| arg_no_deref(&x));
126         #[allow(clippy::redundant_closure)]
127         let _ = v.iter().any(|x: &u32| arg_no_deref(&x));
128     }
129
130     fn field_index_projection() {
131         struct FooDouble {
132             bar: Vec<Vec<i32>>,
133         }
134         struct Foo {
135             bar: Vec<i32>,
136         }
137         struct FooOuter {
138             inner: Foo,
139             inner_double: FooDouble,
140         }
141         let vfoo = vec![FooOuter {
142             inner: Foo { bar: vec![0, 1, 2, 3] },
143             inner_double: FooDouble {
144                 bar: vec![vec![0, 1, 2, 3]],
145             },
146         }];
147         let _ = vfoo
148             .iter()
149             .any(|v| v.inner_double.bar[0][0] == 2 && v.inner.bar[0] == 2);
150     }
151
152     fn index_field_projection() {
153         struct Foo {
154             bar: i32,
155         }
156         struct FooOuter {
157             inner: Vec<Foo>,
158         }
159         let vfoo = vec![FooOuter {
160             inner: vec![Foo { bar: 0 }],
161         }];
162         let _ = vfoo.iter().any(|v| v.inner[0].bar == 2);
163     }
164
165     fn double_deref_index_projection() {
166         let vfoo = vec![&&[0, 1, 2, 3]];
167         let _ = vfoo.iter().any(|x| (**x)[0] == 9);
168     }
169
170     fn method_call_by_ref() {
171         struct Foo {
172             bar: u32,
173         }
174         impl Foo {
175             pub fn by_ref(&self, x: &u32) -> bool {
176                 *x == self.bar
177             }
178         }
179         let vfoo = vec![Foo { bar: 1 }];
180         let _ = vfoo.iter().any(|v| v.by_ref(&v.bar));
181     }
182
183     fn ref_bindings() {
184         let _ = [&(&1, 2), &(&3, 4), &(&5, 4)].iter().any(|(&x, y)| x == *y);
185         let _ = [&(&1, 2), &(&3, 4), &(&5, 4)].iter().any(|(&x, y)| x == *y);
186     }
187
188     fn test_string_1(s: &str) -> bool {
189         s.is_empty()
190     }
191
192     fn test_u32_1(s: &u32) -> bool {
193         s.is_power_of_two()
194     }
195
196     fn test_u32_2(s: u32) -> bool {
197         s.is_power_of_two()
198     }
199
200     fn projection_in_args_test() {
201         // Index projections
202         let lst = &[String::from("Hello"), String::from("world")];
203         let v: Vec<&[String]> = vec![lst];
204         let _ = v.iter().any(|s| s[0].is_empty());
205         let _ = v.iter().any(|s| test_string_1(&s[0]));
206
207         // Field projections
208         struct FieldProjection<'a> {
209             field: &'a u32,
210         }
211         let field = 123456789;
212         let instance = FieldProjection { field: &field };
213         let v = vec![instance];
214         let _ = v.iter().any(|fp| fp.field.is_power_of_two());
215         let _ = v.iter().any(|fp| test_u32_1(fp.field));
216         let _ = v.iter().any(|fp| test_u32_2(*fp.field));
217     }
218 }