]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/trivially_copy_pass_by_ref.rs
Rollup merge of #100462 - zohnannor:master, r=thomcc
[rust.git] / src / tools / clippy / 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 #![deny(clippy::trivially_copy_pass_by_ref)]
4 #![allow(
5     clippy::disallowed_names,
6     clippy::redundant_field_names,
7     clippy::uninlined_format_args
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     fn bad_issue7518(self, other: &Self) {}
62 }
63
64 impl AsRef<u32> for Foo {
65     fn as_ref(&self) -> &u32 {
66         &self.0
67     }
68 }
69
70 impl Bar {
71     fn good(&self, a: &mut u32, b: u32, c: &Bar) {}
72
73     fn bad2(x: &u32, y: &Foo, z: &Baz) {}
74 }
75
76 trait MyTrait {
77     fn trait_method(&self, _foo: &Foo);
78 }
79
80 pub trait MyTrait2 {
81     fn trait_method2(&self, _color: &Color);
82 }
83
84 impl MyTrait for Foo {
85     fn trait_method(&self, _foo: &Foo) {
86         unimplemented!()
87     }
88 }
89
90 #[allow(unused_variables)]
91 mod issue3992 {
92     pub trait A {
93         #[allow(clippy::trivially_copy_pass_by_ref)]
94         fn a(b: &u16) {}
95     }
96
97     #[allow(clippy::trivially_copy_pass_by_ref)]
98     pub fn c(d: &u16) {}
99 }
100
101 mod issue5876 {
102     // Don't lint here as it is always inlined
103     #[inline(always)]
104     fn foo_always(x: &i32) {
105         println!("{}", x);
106     }
107
108     #[inline(never)]
109     fn foo_never(x: &i32) {
110         println!("{}", x);
111     }
112
113     #[inline]
114     fn foo(x: &i32) {
115         println!("{}", x);
116     }
117 }
118
119 fn _ref_to_opt_ref_implicit(x: &u32) -> Option<&u32> {
120     Some(x)
121 }
122
123 #[allow(clippy::needless_lifetimes)]
124 fn _ref_to_opt_ref_explicit<'a>(x: &'a u32) -> Option<&'a u32> {
125     Some(x)
126 }
127
128 fn _with_constraint<'a, 'b: 'a>(x: &'b u32, y: &'a u32) -> &'a u32 {
129     if true { x } else { y }
130 }
131
132 async fn _async_implicit(x: &u32) -> &u32 {
133     x
134 }
135
136 #[allow(clippy::needless_lifetimes)]
137 async fn _async_explicit<'a>(x: &'a u32) -> &'a u32 {
138     x
139 }
140
141 fn _unrelated_lifetimes<'a, 'b>(_x: &'a u32, y: &'b u32) -> &'b u32 {
142     y
143 }
144
145 fn _return_ptr(x: &u32) -> *const u32 {
146     x
147 }
148
149 fn _return_field_ptr(x: &(u32, u32)) -> *const u32 {
150     &x.0
151 }
152
153 fn _return_field_ptr_addr_of(x: &(u32, u32)) -> *const u32 {
154     core::ptr::addr_of!(x.0)
155 }
156
157 fn main() {
158     let (mut foo, bar) = (Foo(0), Bar([0; 24]));
159     let (mut a, b, c, x, y, z) = (0, 0, Bar([0; 24]), 0, Foo(0), 0);
160     good(&mut a, b, &c);
161     good_return_implicit_lt_ref(&y);
162     good_return_explicit_lt_ref(&y);
163     bad(&x, &y, &z);
164     foo.good(&mut a, b, &c);
165     foo.good2();
166     foo.bad(&x, &y, &z);
167     Foo::bad2(&x, &y, &z);
168     bar.good(&mut a, b, &c);
169     Bar::bad2(&x, &y, &z);
170     foo.as_ref();
171 }