]> git.lizzy.rs Git - rust.git/blob - tests/ui/methods.rs
Add `#[rustfmt::skip]` to methods tests
[rust.git] / tests / ui / methods.rs
1 // aux-build:option_helpers.rs
2
3 #![warn(clippy::all, clippy::pedantic, clippy::option_unwrap_used)]
4 #![allow(
5     clippy::blacklisted_name,
6     unused,
7     clippy::print_stdout,
8     clippy::non_ascii_literal,
9     clippy::new_without_default,
10     clippy::missing_docs_in_private_items,
11     clippy::needless_pass_by_value,
12     clippy::default_trait_access,
13     clippy::use_self,
14     clippy::new_ret_no_self,
15     clippy::useless_format
16 )]
17
18 #[macro_use]
19 extern crate option_helpers;
20
21 use std::collections::BTreeMap;
22 use std::collections::HashMap;
23 use std::collections::HashSet;
24 use std::collections::VecDeque;
25 use std::ops::Mul;
26 use std::iter::FromIterator;
27 use std::rc::{self, Rc};
28 use std::sync::{self, Arc};
29
30 use option_helpers::IteratorFalsePositives;
31
32 pub struct T;
33
34 impl T {
35     pub fn add(self, other: T) -> T { self }
36
37     pub(crate) fn drop(&mut self) { } // no error, not public interfact
38     fn neg(self) -> Self { self } // no error, private function
39     fn eq(&self, other: T) -> bool { true } // no error, private function
40
41     fn sub(&self, other: T) -> &T { self } // no error, self is a ref
42     fn div(self) -> T { self } // no error, different #arguments
43     fn rem(self, other: T) { } // no error, wrong return type
44
45     fn into_u32(self) -> u32 { 0 } // fine
46     fn into_u16(&self) -> u16 { 0 }
47
48     fn to_something(self) -> u32 { 0 }
49
50     fn new(self) -> Self { unimplemented!(); }
51 }
52
53 struct Lt<'a> {
54     foo: &'a u32,
55 }
56
57 impl<'a> Lt<'a> {
58     // The lifetime is different, but that’s irrelevant, see #734
59     #[allow(clippy::needless_lifetimes)]
60     pub fn new<'b>(s: &'b str) -> Lt<'b> { unimplemented!() }
61 }
62
63 struct Lt2<'a> {
64     foo: &'a u32,
65 }
66
67 impl<'a> Lt2<'a> {
68     // The lifetime is different, but that’s irrelevant, see #734
69     pub fn new(s: &str) -> Lt2 { unimplemented!() }
70 }
71
72 struct Lt3<'a> {
73     foo: &'a u32,
74 }
75
76 impl<'a> Lt3<'a> {
77     // The lifetime is different, but that’s irrelevant, see #734
78     pub fn new() -> Lt3<'static> { unimplemented!() }
79 }
80
81 #[derive(Clone,Copy)]
82 struct U;
83
84 impl U {
85     fn new() -> Self { U }
86     fn to_something(self) -> u32 { 0 } // ok because U is Copy
87 }
88
89 struct V<T> {
90     _dummy: T
91 }
92
93 impl<T> V<T> {
94     fn new() -> Option<V<T>> { None }
95 }
96
97 impl Mul<T> for T {
98     type Output = T;
99     fn mul(self, other: T) -> T { self } // no error, obviously
100 }
101
102 /// Checks implementation of the following lints:
103 /// * `OPTION_MAP_UNWRAP_OR`
104 /// * `OPTION_MAP_UNWRAP_OR_ELSE`
105 /// * `OPTION_MAP_OR_NONE`
106 #[rustfmt::skip]
107 fn option_methods() {
108     let opt = Some(1);
109
110     // Check OPTION_MAP_UNWRAP_OR
111     // single line case
112     let _ = opt.map(|x| x + 1)
113
114                .unwrap_or(0); // should lint even though this call is on a separate line
115     // multi line cases
116     let _ = opt.map(|x| {
117                         x + 1
118                     }
119               ).unwrap_or(0);
120     let _ = opt.map(|x| x + 1)
121                .unwrap_or({
122                     0
123                 });
124     // single line `map(f).unwrap_or(None)` case
125     let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
126     // multiline `map(f).unwrap_or(None)` cases
127     let _ = opt.map(|x| {
128         Some(x + 1)
129     }
130     ).unwrap_or(None);
131     let _ = opt
132         .map(|x| Some(x + 1))
133         .unwrap_or(None);
134     // macro case
135     let _ = opt_map!(opt, |x| x + 1).unwrap_or(0); // should not lint
136
137     // Check OPTION_MAP_UNWRAP_OR_ELSE
138     // single line case
139     let _ = opt.map(|x| x + 1)
140
141                .unwrap_or_else(|| 0); // should lint even though this call is on a separate line
142     // multi line cases
143     let _ = opt.map(|x| {
144                         x + 1
145                     }
146               ).unwrap_or_else(|| 0);
147     let _ = opt.map(|x| x + 1)
148                .unwrap_or_else(||
149                     0
150                 );
151     // macro case
152     let _ = opt_map!(opt, |x| x + 1).unwrap_or_else(|| 0); // should not lint
153
154     // Check OPTION_MAP_OR_NONE
155     // single line case
156     let _ = opt.map_or(None, |x| Some(x + 1));
157     // multi line case
158     let _ = opt.map_or(None, |x| {
159                         Some(x + 1)
160                        }
161                 );
162 }
163
164 /// Struct to generate false positives for things with .iter()
165 #[derive(Copy, Clone)]
166 struct HasIter;
167
168 impl HasIter {
169     fn iter(self) -> IteratorFalsePositives {
170         IteratorFalsePositives { foo: 0 }
171     }
172
173     fn iter_mut(self) -> IteratorFalsePositives {
174         IteratorFalsePositives { foo: 0 }
175     }
176 }
177
178 /// Checks implementation of `FILTER_NEXT` lint
179 #[rustfmt::skip]
180 fn filter_next() {
181     let v = vec![3, 2, 1, 0, -1, -2, -3];
182
183     // check single-line case
184     let _ = v.iter().filter(|&x| *x < 0).next();
185
186     // check multi-line case
187     let _ = v.iter().filter(|&x| {
188                                 *x < 0
189                             }
190                    ).next();
191
192     // check that we don't lint if the caller is not an Iterator
193     let foo = IteratorFalsePositives { foo: 0 };
194     let _ = foo.filter().next();
195 }
196
197 /// Checks implementation of `SEARCH_IS_SOME` lint
198 #[rustfmt::skip]
199 fn search_is_some() {
200     let v = vec![3, 2, 1, 0, -1, -2, -3];
201
202     // check `find().is_some()`, single-line
203     let _ = v.iter().find(|&x| *x < 0).is_some();
204
205     // check `find().is_some()`, multi-line
206     let _ = v.iter().find(|&x| {
207                               *x < 0
208                           }
209                    ).is_some();
210
211     // check `position().is_some()`, single-line
212     let _ = v.iter().position(|&x| x < 0).is_some();
213
214     // check `position().is_some()`, multi-line
215     let _ = v.iter().position(|&x| {
216                                   x < 0
217                               }
218                    ).is_some();
219
220     // check `rposition().is_some()`, single-line
221     let _ = v.iter().rposition(|&x| x < 0).is_some();
222
223     // check `rposition().is_some()`, multi-line
224     let _ = v.iter().rposition(|&x| {
225                                    x < 0
226                                }
227                    ).is_some();
228
229     // check that we don't lint if the caller is not an Iterator
230     let foo = IteratorFalsePositives { foo: 0 };
231     let _ = foo.find().is_some();
232     let _ = foo.position().is_some();
233     let _ = foo.rposition().is_some();
234 }
235
236 /// Checks implementation of the `OR_FUN_CALL` lint
237 fn or_fun_call() {
238     struct Foo;
239
240     impl Foo {
241         fn new() -> Foo { Foo }
242     }
243
244     enum Enum {
245         A(i32),
246     }
247
248
249
250     fn make<T>() -> T { unimplemented!(); }
251
252     let with_enum = Some(Enum::A(1));
253     with_enum.unwrap_or(Enum::A(5));
254
255     let with_const_fn = Some(::std::time::Duration::from_secs(1));
256     with_const_fn.unwrap_or(::std::time::Duration::from_secs(5));
257
258     let with_constructor = Some(vec![1]);
259     with_constructor.unwrap_or(make());
260
261     let with_new = Some(vec![1]);
262     with_new.unwrap_or(Vec::new());
263
264     let with_const_args = Some(vec![1]);
265     with_const_args.unwrap_or(Vec::with_capacity(12));
266
267     let with_err : Result<_, ()> = Ok(vec![1]);
268     with_err.unwrap_or(make());
269
270     let with_err_args : Result<_, ()> = Ok(vec![1]);
271     with_err_args.unwrap_or(Vec::with_capacity(12));
272
273     let with_default_trait = Some(1);
274     with_default_trait.unwrap_or(Default::default());
275
276     let with_default_type = Some(1);
277     with_default_type.unwrap_or(u64::default());
278
279     let with_vec = Some(vec![1]);
280     with_vec.unwrap_or(vec![]);
281
282     // FIXME #944: ~|SUGGESTION with_vec.unwrap_or_else(|| vec![]);
283
284     let without_default = Some(Foo);
285     without_default.unwrap_or(Foo::new());
286
287     let mut map = HashMap::<u64, String>::new();
288     map.entry(42).or_insert(String::new());
289
290     let mut btree = BTreeMap::<u64, String>::new();
291     btree.entry(42).or_insert(String::new());
292
293     let stringy = Some(String::from(""));
294     let _ = stringy.unwrap_or("".to_owned());
295 }
296
297 /// Checks implementation of `ITER_NTH` lint
298 fn iter_nth() {
299     let mut some_vec = vec![0, 1, 2, 3];
300     let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
301     let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect();
302
303     {
304         // Make sure we lint `.iter()` for relevant types
305         let bad_vec = some_vec.iter().nth(3);
306         let bad_slice = &some_vec[..].iter().nth(3);
307         let bad_boxed_slice = boxed_slice.iter().nth(3);
308         let bad_vec_deque = some_vec_deque.iter().nth(3);
309     }
310
311     {
312         // Make sure we lint `.iter_mut()` for relevant types
313         let bad_vec = some_vec.iter_mut().nth(3);
314     }
315     {
316         let bad_slice = &some_vec[..].iter_mut().nth(3);
317     }
318     {
319         let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
320     }
321
322     // Make sure we don't lint for non-relevant types
323     let false_positive = HasIter;
324     let ok = false_positive.iter().nth(3);
325     let ok_mut = false_positive.iter_mut().nth(3);
326 }
327
328 #[allow(clippy::similar_names)]
329 fn main() {
330     let opt = Some(0);
331     let _ = opt.unwrap();
332 }