]> git.lizzy.rs Git - rust.git/blob - tests/ui/search_is_some_fixable_none.rs
Handle other projection kinds
[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 }