@@ -23,6 +23,11 @@ use if_chain::if_chain;
23
23
/// **What it does:** Checks for types with a `fn new() -> Self` method and no
24
24
/// implementation of
25
25
/// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html).
26
+ ///
27
+ /// It detects both the case when a manual
28
+ /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html)
29
+ /// implementation is required and also when it can be created with
30
+ /// `#[derive(Default)]
26
31
///
27
32
/// **Why is this bad?** The user might expect to be able to use
28
33
/// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html) as the
@@ -53,52 +58,50 @@ use if_chain::if_chain;
53
58
/// }
54
59
/// }
55
60
/// ```
56
- ///
57
- /// You can also have `new()` call `Default::default()`.
58
- declare_clippy_lint ! {
59
- pub NEW_WITHOUT_DEFAULT ,
60
- style,
61
- "`fn new() -> Self` method without `Default` implementation"
62
- }
63
-
64
- /// **What it does:** Checks for types with a `fn new() -> Self` method
65
- /// and no implementation of
66
- /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html),
67
- /// where the `Default` can be derived by `#[derive(Default)]`.
68
- ///
69
- /// **Why is this bad?** The user might expect to be able to use
70
- /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html) as the
71
- /// type can be constructed without arguments.
72
- ///
73
- /// **Known problems:** Hopefully none.
74
- ///
75
- /// **Example:**
76
- ///
61
+ ///
62
+ /// Or, if
63
+ /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html)
64
+ /// can be derived by `#[derive(Default)]`:
65
+ ///
77
66
/// ```rust
78
67
/// struct Foo;
79
- ///
68
+ ///
80
69
/// impl Foo {
81
70
/// fn new() -> Self {
82
71
/// Foo
83
72
/// }
84
73
/// }
85
74
/// ```
86
- ///
87
- /// Just prepend `#[derive(Default)]` before the `struct` definition.
75
+ ///
76
+ /// Instead, use:
77
+ ///
78
+ /// ```rust
79
+ /// #[derive(Default)]
80
+ /// struct Foo;
81
+ ///
82
+ /// impl Foo {
83
+ /// fn new() -> Self {
84
+ /// Foo
85
+ /// }
86
+ /// }
87
+ /// ```
88
+ ///
89
+ /// You can also have `new()` call `Default::default()`.
88
90
declare_clippy_lint ! {
89
- pub NEW_WITHOUT_DEFAULT_DERIVE ,
91
+ pub NEW_WITHOUT_DEFAULT ,
90
92
style,
91
- "`fn new() -> Self` without `#[derive]`able `Default` implementation"
93
+ "`fn new() -> Self` method without `Default` implementation"
92
94
}
93
95
96
+
94
97
#[ derive( Clone , Default ) ]
95
98
pub struct NewWithoutDefault {
96
99
impling_types : Option < NodeSet > ,
97
100
}
98
101
99
102
impl LintPass for NewWithoutDefault {
100
103
fn get_lints ( & self ) -> LintArray {
101
- lint_array ! ( NEW_WITHOUT_DEFAULT , NEW_WITHOUT_DEFAULT_DERIVE )
104
+ lint_array ! ( NEW_WITHOUT_DEFAULT )
102
105
}
103
106
}
104
107
@@ -167,7 +170,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
167
170
if let Some ( sp) = can_derive_default( self_ty, cx, default_trait_id) {
168
171
span_lint_node_and_then(
169
172
cx,
170
- NEW_WITHOUT_DEFAULT_DERIVE ,
173
+ NEW_WITHOUT_DEFAULT ,
171
174
id,
172
175
impl_item. span,
173
176
& format!(
0 commit comments