]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/vec.fixed
New upstream version 1.55.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / vec.fixed
1 // run-rustfix
2 #![allow(clippy::nonstandard_macro_braces)]
3 #![warn(clippy::useless_vec)]
4
5 #[derive(Debug)]
6 struct NonCopy;
7
8 fn on_slice(_: &[u8]) {}
9
10 fn on_mut_slice(_: &mut [u8]) {}
11
12 #[allow(clippy::ptr_arg)]
13 fn on_vec(_: &Vec<u8>) {}
14
15 fn on_mut_vec(_: &mut Vec<u8>) {}
16
17 struct Line {
18 length: usize,
19 }
20
21 impl Line {
22 fn length(&self) -> usize {
23 self.length
24 }
25 }
26
27 fn main() {
28 on_slice(&[]);
29 on_slice(&[]);
30 on_mut_slice(&mut []);
31
32 on_slice(&[1, 2]);
33 on_slice(&[1, 2]);
34 on_mut_slice(&mut [1, 2]);
35
36 on_slice(&[1, 2]);
37 on_slice(&[1, 2]);
38 on_mut_slice(&mut [1, 2]);
39 #[rustfmt::skip]
40 on_slice(&[1, 2]);
41 on_slice(&[1, 2]);
42 on_mut_slice(&mut [1, 2]);
43
44 on_slice(&[1; 2]);
45 on_slice(&[1; 2]);
46 on_mut_slice(&mut [1; 2]);
47
48 on_vec(&vec![]);
49 on_vec(&vec![1, 2]);
50 on_vec(&vec![1; 2]);
51 on_mut_vec(&mut vec![]);
52 on_mut_vec(&mut vec![1, 2]);
53 on_mut_vec(&mut vec![1; 2]);
54
55 // Now with non-constant expressions
56 let line = Line { length: 2 };
57
58 on_slice(&vec![2; line.length]);
59 on_slice(&vec![2; line.length()]);
60 on_mut_slice(&mut vec![2; line.length]);
61 on_mut_slice(&mut vec![2; line.length()]);
62
63 for a in &[1, 2, 3] {
64 println!("{:?}", a);
65 }
66
67 for a in vec![NonCopy, NonCopy] {
68 println!("{:?}", a);
69 }
70
71 on_vec(&vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
72 on_mut_vec(&mut vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
73
74 // Ok
75 for a in vec![1; 201] {
76 println!("{:?}", a);
77 }
78 }