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