]> git.lizzy.rs Git - rust.git/blob - src/docs/new_ret_no_self.txt
Auto merge of #9421 - xphoniex:fix-#9420, r=giraffate
[rust.git] / src / docs / new_ret_no_self.txt
1 ### What it does
2 Checks for `new` not returning a type that contains `Self`.
3
4 ### Why is this bad?
5 As a convention, `new` methods are used to make a new
6 instance of a type.
7
8 ### Example
9 In an impl block:
10 ```
11 impl Foo {
12     fn new() -> NotAFoo {
13     }
14 }
15 ```
16
17 ```
18 struct Bar(Foo);
19 impl Foo {
20     // Bad. The type name must contain `Self`
21     fn new() -> Bar {
22     }
23 }
24 ```
25
26 ```
27 impl Foo {
28     // Good. Return type contains `Self`
29     fn new() -> Result<Foo, FooError> {
30     }
31 }
32 ```
33
34 Or in a trait definition:
35 ```
36 pub trait Trait {
37     // Bad. The type name must contain `Self`
38     fn new();
39 }
40 ```
41
42 ```
43 pub trait Trait {
44     // Good. Return type contains `Self`
45     fn new() -> Self;
46 }
47 ```