]> git.lizzy.rs Git - rust.git/blob - src/docs/new_without_default.txt
Add iter_kv_map lint
[rust.git] / src / docs / new_without_default.txt
1 ### What it does
2 Checks for public types with a `pub fn new() -> Self` method and no
3 implementation of
4 [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html).
5
6 ### Why is this bad?
7 The user might expect to be able to use
8 [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html) as the
9 type can be constructed without arguments.
10
11 ### Example
12 ```
13 pub struct Foo(Bar);
14
15 impl Foo {
16     pub fn new() -> Self {
17         Foo(Bar::new())
18     }
19 }
20 ```
21
22 To fix the lint, add a `Default` implementation that delegates to `new`:
23
24 ```
25 pub struct Foo(Bar);
26
27 impl Default for Foo {
28     fn default() -> Self {
29         Foo::new()
30     }
31 }
32 ```