]> git.lizzy.rs Git - rust.git/blob - tests/ui/trivially_copy_pass_by_ref.rs
Auto merge of #68717 - petrochenkov:stabexpat, r=varkor
[rust.git] / tests / ui / trivially_copy_pass_by_ref.rs
1 // normalize-stderr-test "\(\d+ byte\)" -> "(N byte)"
2 // normalize-stderr-test "\(limit: \d+ byte\)" -> "(limit: N byte)"
3
4 #![deny(clippy::trivially_copy_pass_by_ref)]
5 #![allow(
6     clippy::many_single_char_names,
7     clippy::blacklisted_name,
8     clippy::redundant_field_names
9 )]
10
11 #[derive(Copy, Clone)]
12 struct Foo(u32);
13
14 #[derive(Copy, Clone)]
15 struct Bar([u8; 24]);
16
17 #[derive(Copy, Clone)]
18 pub struct Color {
19     pub r: u8,
20     pub g: u8,
21     pub b: u8,
22     pub a: u8,
23 }
24
25 struct FooRef<'a> {
26     foo: &'a Foo,
27 }
28
29 type Baz = u32;
30
31 fn good(a: &mut u32, b: u32, c: &Bar) {}
32
33 fn good_return_implicit_lt_ref(foo: &Foo) -> &u32 {
34     &foo.0
35 }
36
37 #[allow(clippy::needless_lifetimes)]
38 fn good_return_explicit_lt_ref<'a>(foo: &'a Foo) -> &'a u32 {
39     &foo.0
40 }
41
42 fn good_return_implicit_lt_struct(foo: &Foo) -> FooRef {
43     FooRef { foo }
44 }
45
46 #[allow(clippy::needless_lifetimes)]
47 fn good_return_explicit_lt_struct<'a>(foo: &'a Foo) -> FooRef<'a> {
48     FooRef { foo }
49 }
50
51 fn bad(x: &u32, y: &Foo, z: &Baz) {}
52
53 impl Foo {
54     fn good(self, a: &mut u32, b: u32, c: &Bar) {}
55
56     fn good2(&mut self) {}
57
58     fn bad(&self, x: &u32, y: &Foo, z: &Baz) {}
59
60     fn bad2(x: &u32, y: &Foo, z: &Baz) {}
61 }
62
63 impl AsRef<u32> for Foo {
64     fn as_ref(&self) -> &u32 {
65         &self.0
66     }
67 }
68
69 impl Bar {
70     fn good(&self, a: &mut u32, b: u32, c: &Bar) {}
71
72     fn bad2(x: &u32, y: &Foo, z: &Baz) {}
73 }
74
75 trait MyTrait {
76     fn trait_method(&self, _foo: &Foo);
77 }
78
79 pub trait MyTrait2 {
80     fn trait_method2(&self, _color: &Color);
81 }
82
83 impl MyTrait for Foo {
84     fn trait_method(&self, _foo: &Foo) {
85         unimplemented!()
86     }
87 }
88
89 #[allow(unused_variables)]
90 mod issue3992 {
91     pub trait A {
92         #[allow(clippy::trivially_copy_pass_by_ref)]
93         fn a(b: &u16) {}
94     }
95
96     #[allow(clippy::trivially_copy_pass_by_ref)]
97     pub fn c(d: &u16) {}
98 }
99
100 fn main() {
101     let (mut foo, bar) = (Foo(0), Bar([0; 24]));
102     let (mut a, b, c, x, y, z) = (0, 0, Bar([0; 24]), 0, Foo(0), 0);
103     good(&mut a, b, &c);
104     good_return_implicit_lt_ref(&y);
105     good_return_explicit_lt_ref(&y);
106     bad(&x, &y, &z);
107     foo.good(&mut a, b, &c);
108     foo.good2();
109     foo.bad(&x, &y, &z);
110     Foo::bad2(&x, &y, &z);
111     bar.good(&mut a, b, &c);
112     Bar::bad2(&x, &y, &z);
113     foo.as_ref();
114 }