]> git.lizzy.rs Git - rust.git/blob - tests/ui/trivially_copy_pass_by_ref.rs
Update trivially_copy_pass_by_ref with Trait examples
[rust.git] / tests / ui / trivially_copy_pass_by_ref.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11
12
13 #![allow(clippy::many_single_char_names, clippy::blacklisted_name, clippy::redundant_field_names)]
14
15 #[derive(Copy, Clone)]
16 struct Foo(u32);
17
18 #[derive(Copy, Clone)]
19 struct Bar([u8; 24]);
20
21 #[derive(Copy, Clone)]
22 pub struct Color {
23     pub r: u8, pub g: u8, pub b: u8, pub a: u8,
24 }
25
26 struct FooRef<'a> {
27     foo: &'a Foo,
28 }
29
30 type Baz = u32;
31
32 fn good(a: &mut u32, b: u32, c: &Bar) {
33 }
34
35 fn good_return_implicit_lt_ref(foo: &Foo) -> &u32 {
36     &foo.0
37 }
38
39 #[allow(clippy::needless_lifetimes)]
40 fn good_return_explicit_lt_ref<'a>(foo: &'a Foo) -> &'a u32 {
41     &foo.0
42 }
43
44 fn good_return_implicit_lt_struct(foo: &Foo) -> FooRef {
45     FooRef {
46         foo,
47     }
48 }
49
50 #[allow(clippy::needless_lifetimes)]
51 fn good_return_explicit_lt_struct<'a>(foo: &'a Foo) -> FooRef<'a> {
52     FooRef {
53         foo,
54     }
55 }
56
57 fn bad(x: &u32, y: &Foo, z: &Baz) {
58 }
59
60 impl Foo {
61     fn good(self, a: &mut u32, b: u32, c: &Bar) {
62     }
63
64     fn good2(&mut self) {
65     }
66
67     fn bad(&self, x: &u32, y: &Foo, z: &Baz) {
68     }
69
70     fn bad2(x: &u32, y: &Foo, z: &Baz) {
71     }
72 }
73
74 impl AsRef<u32> for Foo {
75     fn as_ref(&self) -> &u32 {
76         &self.0
77     }
78 }
79
80 impl Bar {
81     fn good(&self, a: &mut u32, b: u32, c: &Bar) {
82     }
83
84     fn bad2(x: &u32, y: &Foo, z: &Baz) {
85     }
86 }
87
88 trait MyTrait {
89     fn trait_method(&self, _foo: &Foo);
90 }
91
92 pub trait MyTrait2 {
93     fn trait_method2(&self, _color: &Color);
94 }
95
96 impl MyTrait for Foo {
97     fn trait_method(&self, _foo: &Foo) {
98         unimplemented!()
99     }
100 }
101
102 fn main() {
103     let (mut foo, bar) = (Foo(0), Bar([0; 24]));
104     let (mut a, b, c, x, y, z) = (0, 0, Bar([0; 24]), 0, Foo(0), 0);
105     good(&mut a, b, &c);
106     good_return_implicit_lt_ref(&y);
107     good_return_explicit_lt_ref(&y);
108     bad(&x, &y, &z);
109     foo.good(&mut a, b, &c);
110     foo.good2();
111     foo.bad(&x, &y, &z);
112     Foo::bad2(&x, &y, &z);
113     bar.good(&mut a, b, &c);
114     Bar::bad2(&x, &y, &z);
115     foo.as_ref();
116 }