fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
3.7 KiB178 linesraw
1#![warn(clippy::useless_vec)]
2#![allow(clippy::nonstandard_macro_braces, clippy::uninlined_format_args, unused)]
3
4use std::rc::Rc;
5
6struct StructWithVec {
7    _x: Vec<i32>,
8}
9
10fn on_slice(_: &[u8]) {}
11
12fn on_mut_slice(_: &mut [u8]) {}
13
14#[allow(clippy::ptr_arg)]
15fn on_vec(_: &Vec<u8>) {}
16
17fn on_mut_vec(_: &mut Vec<u8>) {}
18
19struct Line {
20    length: usize,
21}
22
23impl Line {
24    fn length(&self) -> usize {
25        self.length
26    }
27}
28
29fn main() {
30    on_slice(&[]);
31    on_slice(&[]);
32    on_mut_slice(&mut []);
33
34    on_slice(&[1, 2]);
35    on_slice(&[1, 2]);
36    on_mut_slice(&mut [1, 2]);
37
38    on_slice(&[1, 2]);
39    on_slice(&[1, 2]);
40    on_mut_slice(&mut [1, 2]);
41    #[rustfmt::skip]
42    on_slice(&[1, 2]);
43    on_slice(&[1, 2]);
44    on_mut_slice(&mut [1, 2]);
45
46    on_slice(&[1; 2]);
47    on_slice(&[1; 2]);
48    on_mut_slice(&mut [1; 2]);
49
50    on_vec(&vec![]);
51    on_vec(&vec![1, 2]);
52    on_vec(&vec![1; 2]);
53    on_mut_vec(&mut vec![]);
54    on_mut_vec(&mut vec![1, 2]);
55    on_mut_vec(&mut vec![1; 2]);
56
57    // Now with non-constant expressions
58    let line = Line { length: 2 };
59
60    on_slice(&vec![2; line.length]);
61    on_slice(&vec![2; line.length()]);
62    on_mut_slice(&mut vec![2; line.length]);
63    on_mut_slice(&mut vec![2; line.length()]);
64
65    on_vec(&vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
66    on_mut_vec(&mut vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
67
68    // Ok
69    for a in vec![1; 201] {
70        println!("{:?}", a);
71    }
72
73    // https://github.com/rust-lang/rust-clippy/issues/2262#issuecomment-783979246
74    let _x: i32 = [1, 2, 3].iter().sum();
75
76    // Do lint
77    let mut x = [1, 2, 3];
78    x.fill(123);
79    dbg!(x[0]);
80    dbg!(x.len());
81    dbg!(x.iter().sum::<i32>());
82
83    let _x: &[i32] = &[1, 2, 3];
84
85    for _ in [1, 2, 3] {}
86
87    // Don't lint
88    let x = vec![1, 2, 3];
89    let _v: Vec<i32> = x;
90
91    let x = vec![1, 2, 3];
92    let _s = StructWithVec { _x: x };
93
94    // Explicit type annotation would make the change to [1, 2, 3]
95    // a compile error.
96    let _x: Vec<i32> = vec![1, 2, 3];
97
98    // Calling a Vec method through a mutable reference
99    let mut x = vec![1, 2, 3];
100    let re = &mut x;
101    re.push(4);
102
103    // Comparing arrays whose length is not equal is a compile error
104    let x = vec![1, 2, 3];
105    let y = vec![1, 2, 3, 4];
106    dbg!(x == y);
107
108    // Non-copy types
109    let _x = vec![String::new(); 10];
110    #[allow(clippy::rc_clone_in_vec_init)]
111    let _x = vec![Rc::new(1); 10];
112
113    // Too large
114    let _x = vec![1; 201];
115}
116
117fn issue11075() {
118    macro_rules! repro {
119        ($e:expr) => {
120            stringify!($e)
121        };
122    }
123    #[allow(clippy::never_loop)]
124    for _string in [repro!(true), repro!(null)] {
125        unimplemented!();
126    }
127
128    macro_rules! in_macro {
129        ($e:expr, $vec:expr, $vec2:expr) => {{
130            vec![1; 2].fill(3);
131            vec![1, 2].fill(3);
132            for _ in vec![1, 2] {}
133            for _ in vec![1; 2] {}
134            for _ in vec![$e, $e] {}
135            for _ in vec![$e; 2] {}
136            for _ in $vec {}
137            for _ in $vec2 {}
138        }};
139    }
140
141    in_macro!(1, [1, 2], [1; 2]);
142
143    macro_rules! from_macro {
144        () => {
145            vec![1, 2, 3]
146        };
147    }
148    macro_rules! from_macro_repeat {
149        () => {
150            vec![1; 3]
151        };
152    }
153
154    for _ in from_macro!() {}
155    for _ in from_macro_repeat!() {}
156}
157
158#[clippy::msrv = "1.53"]
159fn above() {
160    for a in [1, 2, 3] {
161        let _: usize = a;
162    }
163
164    for a in [String::new(), String::new()] {
165        let _: String = a;
166    }
167}
168
169#[clippy::msrv = "1.52"]
170fn below() {
171    for a in vec![1, 2, 3] {
172        let _: usize = a;
173    }
174
175    for a in vec![String::new(), String::new()] {
176        let _: String = a;
177    }
178}