]> git.lizzy.rs Git - rust.git/blob - tests/ui/trivially_copy_pass_by_ref.rs
Auto merge of #7453 - F3real:assume_function_calls_have_side_effect, 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 #![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     fn bad_issue7518(self, other: &Self) {}
63 }
64
65 impl AsRef<u32> for Foo {
66     fn as_ref(&self) -> &u32 {
67         &self.0
68     }
69 }
70
71 impl Bar {
72     fn good(&self, a: &mut u32, b: u32, c: &Bar) {}
73
74     fn bad2(x: &u32, y: &Foo, z: &Baz) {}
75 }
76
77 trait MyTrait {
78     fn trait_method(&self, _foo: &Foo);
79 }
80
81 pub trait MyTrait2 {
82     fn trait_method2(&self, _color: &Color);
83 }
84
85 impl MyTrait for Foo {
86     fn trait_method(&self, _foo: &Foo) {
87         unimplemented!()
88     }
89 }
90
91 #[allow(unused_variables)]
92 mod issue3992 {
93     pub trait A {
94         #[allow(clippy::trivially_copy_pass_by_ref)]
95         fn a(b: &u16) {}
96     }
97
98     #[allow(clippy::trivially_copy_pass_by_ref)]
99     pub fn c(d: &u16) {}
100 }
101
102 mod issue5876 {
103     // Don't lint here as it is always inlined
104     #[inline(always)]
105     fn foo_always(x: &i32) {
106         println!("{}", x);
107     }
108
109     #[inline(never)]
110     fn foo_never(x: &i32) {
111         println!("{}", x);
112     }
113
114     #[inline]
115     fn foo(x: &i32) {
116         println!("{}", x);
117     }
118 }
119
120 fn main() {
121     let (mut foo, bar) = (Foo(0), Bar([0; 24]));
122     let (mut a, b, c, x, y, z) = (0, 0, Bar([0; 24]), 0, Foo(0), 0);
123     good(&mut a, b, &c);
124     good_return_implicit_lt_ref(&y);
125     good_return_explicit_lt_ref(&y);
126     bad(&x, &y, &z);
127     foo.good(&mut a, b, &c);
128     foo.good2();
129     foo.bad(&x, &y, &z);
130     Foo::bad2(&x, &y, &z);
131     bar.good(&mut a, b, &c);
132     Bar::bad2(&x, &y, &z);
133     foo.as_ref();
134 }