]> git.lizzy.rs Git - rust.git/blob - tests/ui/search_is_some_fixable_none.rs
Add test case for references bindings
[rust.git] / tests / ui / search_is_some_fixable_none.rs
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_none()`, single-line case.
10     let _ = v.iter().find(|&x| *x < 0).is_none();
11     let _ = (0..1).find(|x| **y == *x).is_none(); // one dereference less
12     let _ = (0..1).find(|x| *x == 0).is_none();
13     let _ = v.iter().find(|x| **x == 0).is_none();
14     let _ = (4..5).find(|x| *x == 1 || *x == 3 || *x == 5).is_none();
15     let _ = (1..3).find(|x| [1, 2, 3].contains(x)).is_none();
16     let _ = (1..3).find(|x| *x == 0 || [1, 2, 3].contains(x)).is_none();
17     let _ = (1..3).find(|x| [1, 2, 3].contains(x) || *x == 0).is_none();
18     let _ = (1..3)
19         .find(|x| [1, 2, 3].contains(x) || *x == 0 || [4, 5, 6].contains(x) || *x == -1)
20         .is_none();
21
22     // Check `position().is_none()`, single-line case.
23     let _ = v.iter().position(|&x| x < 0).is_none();
24
25     // Check `rposition().is_none()`, single-line case.
26     let _ = v.iter().rposition(|&x| x < 0).is_none();
27
28     let s1 = String::from("hello world");
29     let s2 = String::from("world");
30
31     // caller of `find()` is a `&`static str`
32     let _ = "hello world".find("world").is_none();
33     let _ = "hello world".find(&s2).is_none();
34     let _ = "hello world".find(&s2[2..]).is_none();
35     // caller of `find()` is a `String`
36     let _ = s1.find("world").is_none();
37     let _ = s1.find(&s2).is_none();
38     let _ = s1.find(&s2[2..]).is_none();
39     // caller of `find()` is slice of `String`
40     let _ = s1[2..].find("world").is_none();
41     let _ = s1[2..].find(&s2).is_none();
42     let _ = s1[2..].find(&s2[2..]).is_none();
43 }
44
45 #[allow(clippy::clone_on_copy, clippy::map_clone)]
46 mod issue7392 {
47     struct Player {
48         hand: Vec<usize>,
49     }
50     fn filter() {
51         let p = Player {
52             hand: vec![1, 2, 3, 4, 5],
53         };
54         let filter_hand = vec![5];
55         let _ = p
56             .hand
57             .iter()
58             .filter(|c| filter_hand.iter().find(|cc| c == cc).is_none())
59             .map(|c| c.clone())
60             .collect::<Vec<_>>();
61     }
62
63     struct PlayerTuple {
64         hand: Vec<(usize, char)>,
65     }
66     fn filter_tuple() {
67         let p = PlayerTuple {
68             hand: vec![(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')],
69         };
70         let filter_hand = vec![5];
71         let _ = p
72             .hand
73             .iter()
74             .filter(|(c, _)| filter_hand.iter().find(|cc| c == *cc).is_none())
75             .map(|c| c.clone())
76             .collect::<Vec<_>>();
77     }
78
79     fn field_projection() {
80         struct Foo {
81             foo: i32,
82             bar: u32,
83         }
84         let vfoo = vec![Foo { foo: 1, bar: 2 }];
85         let _ = vfoo.iter().find(|v| v.foo == 1 && v.bar == 2).is_none();
86
87         let vfoo = vec![(42, Foo { foo: 1, bar: 2 })];
88         let _ = vfoo
89             .iter()
90             .find(|(i, v)| *i == 42 && v.foo == 1 && v.bar == 2)
91             .is_none();
92     }
93
94     fn index_projection() {
95         let vfoo = vec![[0, 1, 2, 3]];
96         let _ = vfoo.iter().find(|a| a[0] == 42).is_none();
97     }
98
99     #[allow(clippy::match_like_matches_macro)]
100     fn slice_projection() {
101         let vfoo = vec![[0, 1, 2, 3, 0, 1, 2, 3]];
102         let _ = vfoo.iter().find(|sub| sub[1..4].len() == 3).is_none();
103     }
104
105     fn please(x: &u32) -> bool {
106         *x == 9
107     }
108
109     fn deref_enough(x: u32) -> bool {
110         x == 78
111     }
112
113     fn arg_no_deref(x: &&u32) -> bool {
114         **x == 78
115     }
116
117     fn more_projections() {
118         let x = 19;
119         let ppx: &u32 = &x;
120         let _ = [ppx].iter().find(|ppp_x: &&&u32| please(**ppp_x)).is_none();
121         let _ = [String::from("Hey hey")].iter().find(|s| s.len() == 2).is_none();
122
123         let v = vec![3, 2, 1, 0];
124         let _ = v.iter().find(|x| deref_enough(**x)).is_none();
125
126         #[allow(clippy::redundant_closure)]
127         let _ = v.iter().find(|x| arg_no_deref(x)).is_none();
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             .find(|v| v.inner_double.bar[0][0] == 2 && v.inner.bar[0] == 2)
150             .is_none();
151     }
152
153     fn index_field_projection() {
154         struct Foo {
155             bar: i32,
156         }
157         struct FooOuter {
158             inner: Vec<Foo>,
159         }
160         let vfoo = vec![FooOuter {
161             inner: vec![Foo { bar: 0 }],
162         }];
163         let _ = vfoo.iter().find(|v| v.inner[0].bar == 2).is_none();
164     }
165
166     fn double_deref_index_projection() {
167         let vfoo = vec![&&[0, 1, 2, 3]];
168         let _ = vfoo.iter().find(|x| (**x)[0] == 9).is_none();
169     }
170
171     fn method_call_by_ref() {
172         struct Foo {
173             bar: u32,
174         }
175         impl Foo {
176             pub fn by_ref(&self, x: &u32) -> bool {
177                 *x == self.bar
178             }
179         }
180         let vfoo = vec![Foo { bar: 1 }];
181         let _ = vfoo.iter().find(|v| v.by_ref(&v.bar)).is_none();
182     }
183
184     fn ref_bindings() {
185         let _ = [&(&1, 2), &(&3, 4), &(&5, 4)].iter().find(|(&x, y)| x == *y).is_none();
186         let _ = [&(&1, 2), &(&3, 4), &(&5, 4)].iter().find(|&(&x, y)| x == *y).is_none();
187     }
188 }