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