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(clippy::blacklisted_name, clippy::redundant_field_names)]
6 
7 #[derive(Copy, Clone)]
8 struct Foo(u32);
9 
10 #[derive(Copy, Clone)]
11 struct Bar([u8; 24]);
12 
13 #[derive(Copy, Clone)]
14 pub struct Color {
15     pub r: u8,
16     pub g: u8,
17     pub b: u8,
18     pub a: u8,
19 }
20 
21 struct FooRef<'a> {
22     foo: &'a Foo,
23 }
24 
25 type Baz = u32;
26 
good(a: &mut u32, b: u32, c: &Bar)27 fn good(a: &mut u32, b: u32, c: &Bar) {}
28 
good_return_implicit_lt_ref(foo: &Foo) -> &u3229 fn good_return_implicit_lt_ref(foo: &Foo) -> &u32 {
30     &foo.0
31 }
32 
33 #[allow(clippy::needless_lifetimes)]
good_return_explicit_lt_ref<'a>(foo: &'a Foo) -> &'a u3234 fn good_return_explicit_lt_ref<'a>(foo: &'a Foo) -> &'a u32 {
35     &foo.0
36 }
37 
good_return_implicit_lt_struct(foo: &Foo) -> FooRef38 fn good_return_implicit_lt_struct(foo: &Foo) -> FooRef {
39     FooRef { foo }
40 }
41 
42 #[allow(clippy::needless_lifetimes)]
good_return_explicit_lt_struct<'a>(foo: &'a Foo) -> FooRef<'a>43 fn good_return_explicit_lt_struct<'a>(foo: &'a Foo) -> FooRef<'a> {
44     FooRef { foo }
45 }
46 
bad(x: &u32, y: &Foo, z: &Baz)47 fn bad(x: &u32, y: &Foo, z: &Baz) {}
48 
49 impl Foo {
good(self, a: &mut u32, b: u32, c: &Bar)50     fn good(self, a: &mut u32, b: u32, c: &Bar) {}
51 
good2(&mut self)52     fn good2(&mut self) {}
53 
bad(&self, x: &u32, y: &Foo, z: &Baz)54     fn bad(&self, x: &u32, y: &Foo, z: &Baz) {}
55 
bad2(x: &u32, y: &Foo, z: &Baz)56     fn bad2(x: &u32, y: &Foo, z: &Baz) {}
57 
bad_issue7518(self, other: &Self)58     fn bad_issue7518(self, other: &Self) {}
59 }
60 
61 impl AsRef<u32> for Foo {
as_ref(&self) -> &u3262     fn as_ref(&self) -> &u32 {
63         &self.0
64     }
65 }
66 
67 impl Bar {
good(&self, a: &mut u32, b: u32, c: &Bar)68     fn good(&self, a: &mut u32, b: u32, c: &Bar) {}
69 
bad2(x: &u32, y: &Foo, z: &Baz)70     fn bad2(x: &u32, y: &Foo, z: &Baz) {}
71 }
72 
73 trait MyTrait {
trait_method(&self, _foo: &Foo)74     fn trait_method(&self, _foo: &Foo);
75 }
76 
77 pub trait MyTrait2 {
trait_method2(&self, _color: &Color)78     fn trait_method2(&self, _color: &Color);
79 }
80 
81 impl MyTrait for Foo {
trait_method(&self, _foo: &Foo)82     fn trait_method(&self, _foo: &Foo) {
83         unimplemented!()
84     }
85 }
86 
87 #[allow(unused_variables)]
88 mod issue3992 {
89     pub trait A {
90         #[allow(clippy::trivially_copy_pass_by_ref)]
a(b: &u16)91         fn a(b: &u16) {}
92     }
93 
94     #[allow(clippy::trivially_copy_pass_by_ref)]
c(d: &u16)95     pub fn c(d: &u16) {}
96 }
97 
98 mod issue5876 {
99     // Don't lint here as it is always inlined
100     #[inline(always)]
foo_always(x: &i32)101     fn foo_always(x: &i32) {
102         println!("{}", x);
103     }
104 
105     #[inline(never)]
foo_never(x: &i32)106     fn foo_never(x: &i32) {
107         println!("{}", x);
108     }
109 
110     #[inline]
foo(x: &i32)111     fn foo(x: &i32) {
112         println!("{}", x);
113     }
114 }
115 
main()116 fn main() {
117     let (mut foo, bar) = (Foo(0), Bar([0; 24]));
118     let (mut a, b, c, x, y, z) = (0, 0, Bar([0; 24]), 0, Foo(0), 0);
119     good(&mut a, b, &c);
120     good_return_implicit_lt_ref(&y);
121     good_return_explicit_lt_ref(&y);
122     bad(&x, &y, &z);
123     foo.good(&mut a, b, &c);
124     foo.good2();
125     foo.bad(&x, &y, &z);
126     Foo::bad2(&x, &y, &z);
127     bar.good(&mut a, b, &c);
128     Bar::bad2(&x, &y, &z);
129     foo.as_ref();
130 }
131