]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/search_is_some_fixable_none.fixed
Auto merge of #98120 - TaKO8Ki:box-diagnostic-metadata-field, r=estebank
[rust.git] / src / tools / clippy / tests / ui / search_is_some_fixable_none.fixed
1 // run-rustfix
2 #![allow(dead_code, clippy::explicit_auto_deref)]
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_none()`, 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).any(|x| [1, 2, 3].contains(&x) || x == 0 || [4, 5, 6].contains(&x) || x == -1);
19
20     // Check `position().is_none()`, single-line case.
21     let _ = !v.iter().any(|&x| x < 0);
22
23     // Check `rposition().is_none()`, single-line case.
24     let _ = !v.iter().any(|&x| x < 0);
25
26     let s1 = String::from("hello world");
27     let s2 = String::from("world");
28
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().any(|(i, v)| *i == 42 && v.foo == 1 && v.bar == 2);
88     }
89
90     fn index_projection() {
91         let vfoo = vec![[0, 1, 2, 3]];
92         let _ = !vfoo.iter().any(|a| a[0] == 42);
93     }
94
95     #[allow(clippy::match_like_matches_macro)]
96     fn slice_projection() {
97         let vfoo = vec![[0, 1, 2, 3, 0, 1, 2, 3]];
98         let _ = !vfoo.iter().any(|sub| sub[1..4].len() == 3);
99     }
100
101     fn please(x: &u32) -> bool {
102         *x == 9
103     }
104
105     fn deref_enough(x: u32) -> bool {
106         x == 78
107     }
108
109     fn arg_no_deref(x: &&u32) -> bool {
110         **x == 78
111     }
112
113     fn more_projections() {
114         let x = 19;
115         let ppx: &u32 = &x;
116         let _ = ![ppx].iter().any(|ppp_x: &&u32| please(ppp_x));
117         let _ = ![String::from("Hey hey")].iter().any(|s| s.len() == 2);
118
119         let v = vec![3, 2, 1, 0];
120         let _ = !v.iter().any(|x| deref_enough(*x));
121         let _ = !v.iter().any(|x: &u32| deref_enough(*x));
122
123         #[allow(clippy::redundant_closure)]
124         let _ = !v.iter().any(|x| arg_no_deref(&x));
125         #[allow(clippy::redundant_closure)]
126         let _ = !v.iter().any(|x: &u32| arg_no_deref(&x));
127     }
128
129     fn field_index_projection() {
130         struct FooDouble {
131             bar: Vec<Vec<i32>>,
132         }
133         struct Foo {
134             bar: Vec<i32>,
135         }
136         struct FooOuter {
137             inner: Foo,
138             inner_double: FooDouble,
139         }
140         let vfoo = vec![FooOuter {
141             inner: Foo { bar: vec![0, 1, 2, 3] },
142             inner_double: FooDouble {
143                 bar: vec![vec![0, 1, 2, 3]],
144             },
145         }];
146         let _ = !vfoo
147             .iter().any(|v| v.inner_double.bar[0][0] == 2 && v.inner.bar[0] == 2);
148     }
149
150     fn index_field_projection() {
151         struct Foo {
152             bar: i32,
153         }
154         struct FooOuter {
155             inner: Vec<Foo>,
156         }
157         let vfoo = vec![FooOuter {
158             inner: vec![Foo { bar: 0 }],
159         }];
160         let _ = !vfoo.iter().any(|v| v.inner[0].bar == 2);
161     }
162
163     fn double_deref_index_projection() {
164         let vfoo = vec![&&[0, 1, 2, 3]];
165         let _ = !vfoo.iter().any(|x| (**x)[0] == 9);
166     }
167
168     fn method_call_by_ref() {
169         struct Foo {
170             bar: u32,
171         }
172         impl Foo {
173             pub fn by_ref(&self, x: &u32) -> bool {
174                 *x == self.bar
175             }
176         }
177         let vfoo = vec![Foo { bar: 1 }];
178         let _ = !vfoo.iter().any(|v| v.by_ref(&v.bar));
179     }
180
181     fn ref_bindings() {
182         let _ = ![&(&1, 2), &(&3, 4), &(&5, 4)].iter().any(|(&x, y)| x == *y);
183         let _ = ![&(&1, 2), &(&3, 4), &(&5, 4)].iter().any(|(&x, y)| x == *y);
184     }
185
186     fn test_string_1(s: &str) -> bool {
187         s.is_empty()
188     }
189
190     fn test_u32_1(s: &u32) -> bool {
191         s.is_power_of_two()
192     }
193
194     fn test_u32_2(s: u32) -> bool {
195         s.is_power_of_two()
196     }
197
198     fn projection_in_args_test() {
199         // Index projections
200         let lst = &[String::from("Hello"), String::from("world")];
201         let v: Vec<&[String]> = vec![lst];
202         let _ = !v.iter().any(|s| s[0].is_empty());
203         let _ = !v.iter().any(|s| test_string_1(&s[0]));
204
205         // Field projections
206         struct FieldProjection<'a> {
207             field: &'a u32,
208         }
209         let field = 123456789;
210         let instance = FieldProjection { field: &field };
211         let v = vec![instance];
212         let _ = !v.iter().any(|fp| fp.field.is_power_of_two());
213         let _ = !v.iter().any(|fp| test_u32_1(fp.field));
214         let _ = !v.iter().any(|fp| test_u32_2(*fp.field));
215     }
216 }