]> git.lizzy.rs Git - rust.git/blob - tests/ui/use_self.rs
Merge pull request #3265 from mikerite/fix-export
[rust.git] / tests / ui / use_self.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 #![feature(tool_lints)]
12
13 #![warn(clippy::use_self)]
14 #![allow(dead_code)]
15 #![allow(clippy::should_implement_trait)]
16
17 fn main() {}
18
19 mod use_self {
20     struct Foo {}
21
22     impl Foo {
23         fn new() -> Foo {
24             Foo {}
25         }
26         fn test() -> Foo {
27             Foo::new()
28         }
29     }
30
31     impl Default for Foo {
32         fn default() -> Foo {
33             Foo::new()
34         }
35     }
36 }
37
38 mod better {
39     struct Foo {}
40
41     impl Foo {
42         fn new() -> Self {
43             Self {}
44         }
45         fn test() -> Self {
46             Self::new()
47         }
48     }
49
50     impl Default for Foo {
51         fn default() -> Self {
52             Self::new()
53         }
54     }
55 }
56
57 //todo the lint does not handle lifetimed struct
58 //the following module should trigger the lint on the third method only
59 mod lifetimes {
60     struct Foo<'a>{foo_str: &'a str}
61
62     impl<'a> Foo<'a> {
63         // Cannot use `Self` as return type, because the function is actually `fn foo<'b>(s: &'b str) -> Foo<'b>`
64         fn foo(s: &str) -> Foo {
65             Foo { foo_str: s }
66         }
67         // cannot replace with `Self`, because that's `Foo<'a>`
68         fn bar() -> Foo<'static> {
69             Foo { foo_str: "foo"}
70         }
71
72         // `Self` is applicable here
73         fn clone(&self) -> Foo<'a> {
74             Foo {foo_str: self.foo_str}
75         }
76     }
77 }
78
79 #[allow(clippy::boxed_local)]
80 mod traits {
81
82     use std::ops::Mul;
83
84     trait SelfTrait {
85         fn refs(p1: &Self) -> &Self;
86         fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self;
87         fn mut_refs(p1: &mut Self) -> &mut Self;
88         fn nested(p1: Box<Self>, p2: (&u8, &Self));
89         fn vals(r: Self) -> Self;
90     }
91
92     #[derive(Default)]
93     struct Bad;
94
95     impl SelfTrait for Bad {
96         fn refs(p1: &Bad) -> &Bad {
97             p1
98         }
99
100         fn ref_refs<'a>(p1: &'a &'a Bad) -> &'a &'a Bad {
101             p1
102         }
103
104         fn mut_refs(p1: &mut Bad) -> &mut Bad {
105             p1
106         }
107
108         fn nested(_p1: Box<Bad>, _p2: (&u8, &Bad)) {
109         }
110
111         fn vals(_: Bad) -> Bad {
112             Bad::default()
113         }
114     }
115
116     impl Mul for Bad {
117         type Output = Bad;
118
119         fn mul(self, rhs: Bad) -> Bad {
120             rhs
121         }
122     }
123
124     #[derive(Default)]
125     struct Good;
126
127     impl SelfTrait for Good {
128         fn refs(p1: &Self) -> &Self {
129             p1
130         }
131
132         fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
133             p1
134         }
135
136         fn mut_refs(p1: &mut Self) -> &mut Self {
137             p1
138         }
139
140         fn nested(_p1: Box<Self>, _p2: (&u8, &Self)) {
141         }
142
143         fn vals(_: Self) -> Self {
144             Self::default()
145         }
146     }
147
148     impl Mul for Good {
149         type Output = Self;
150
151         fn mul(self, rhs: Self) -> Self {
152             rhs
153         }
154     }
155
156     trait NameTrait {
157         fn refs(p1: &u8) -> &u8;
158         fn ref_refs<'a>(p1: &'a &'a u8) -> &'a &'a u8;
159         fn mut_refs(p1: &mut u8) -> &mut u8;
160         fn nested(p1: Box<u8>, p2: (&u8, &u8));
161         fn vals(p1: u8) -> u8;
162     }
163
164     // Using `Self` instead of the type name is OK
165     impl NameTrait for u8 {
166         fn refs(p1: &Self) -> &Self {
167             p1
168         }
169
170         fn ref_refs<'a>(p1: &'a &'a Self) -> &'a &'a Self {
171             p1
172         }
173
174         fn mut_refs(p1: &mut Self) -> &mut Self {
175             p1
176         }
177
178         fn nested(_p1: Box<Self>, _p2: (&Self, &Self)) {
179         }
180
181         fn vals(_: Self) -> Self {
182             Self::default()
183         }
184     }
185
186     // Check that self arg isn't linted
187     impl Clone for Good {
188         fn clone(&self) -> Self {
189             // Note: Not linted and it wouldn't be valid
190             // because "can't use `Self` as a constructor`"
191             Good
192         }
193     }
194 }
195
196 mod issue2894 {
197     trait IntoBytes {
198         fn into_bytes(&self) -> Vec<u8>;
199     }
200
201     // This should not be linted
202     impl IntoBytes for u8 {
203         fn into_bytes(&self) -> Vec<u8> {
204             vec![*self]
205         }
206     }
207 }