]> git.lizzy.rs Git - rust.git/blob - tests/ui/vec.rs
Merge branch 'master' into rustfmt_tests
[rust.git] / tests / ui / vec.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![warn(clippy::useless_vec)]
11
12 #[derive(Debug)]
13 struct NonCopy;
14
15 fn on_slice(_: &[u8]) {}
16 #[allow(clippy::ptr_arg)]
17 fn on_vec(_: &Vec<u8>) {}
18
19 struct Line {
20     length: usize,
21 }
22
23 impl Line {
24     fn length(&self) -> usize {
25         self.length
26     }
27 }
28
29 fn main() {
30     on_slice(&vec![]);
31     on_slice(&[]);
32
33     on_slice(&vec![1, 2]);
34     on_slice(&[1, 2]);
35
36     on_slice(&vec![1, 2]);
37     on_slice(&[1, 2]);
38     #[rustfmt::skip]
39     on_slice(&vec!(1, 2));
40     on_slice(&[1, 2]);
41
42     on_slice(&vec![1; 2]);
43     on_slice(&[1; 2]);
44
45     on_vec(&vec![]);
46     on_vec(&vec![1, 2]);
47     on_vec(&vec![1; 2]);
48
49     // Now with non-constant expressions
50     let line = Line { length: 2 };
51
52     on_slice(&vec![2; line.length]);
53     on_slice(&vec![2; line.length()]);
54
55     for a in vec![1, 2, 3] {
56         println!("{:?}", a);
57     }
58
59     for a in vec![NonCopy, NonCopy] {
60         println!("{:?}", a);
61     }
62 }