@@ -74,10 +74,8 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
74
74
. filter_by_name_unhygienic ( sym:: new)
75
75
{
76
76
if let AssocKind :: Fn { has_self : false , .. } = assoc_item. kind
77
- && let impl_item = cx
78
- . tcx
79
- . hir_node_by_def_id ( assoc_item. def_id . expect_local ( ) )
80
- . expect_impl_item ( )
77
+ && let assoc_item_hir_id = cx. tcx . local_def_id_to_hir_id ( assoc_item. def_id . expect_local ( ) )
78
+ && let impl_item = cx. tcx . hir_node ( assoc_item_hir_id) . expect_impl_item ( )
81
79
&& !impl_item. span . in_external_macro ( cx. sess ( ) . source_map ( ) )
82
80
&& let hir:: ImplItemKind :: Fn ( ref sig, _) = impl_item. kind
83
81
&& let id = impl_item. owner_id
@@ -120,6 +118,24 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
120
118
}
121
119
122
120
let mut appl = Applicability :: MachineApplicable ;
121
+ let attrs_sugg = match cx. tcx . hir_attrs ( assoc_item_hir_id) {
122
+ [ ] => String :: new ( ) ,
123
+ [ attr] if attr. has_name ( sym:: cfg_trace) => {
124
+ format ! (
125
+ "{}\n " ,
126
+ snippet_with_applicability( cx. sess( ) , attr. span( ) , "_" , & mut appl)
127
+ )
128
+ } ,
129
+ _ => {
130
+ // There might be some other attributes which the `impl Default` ought to inherit from `fn new`.
131
+ // At the same time, there are many attributes that actually can't be put on an impl block --
132
+ // like `#[inline]`, for example. And for some attributes we can't even build a suggestion,
133
+ // since `Attribute::span` may panic. Because of all this, remain conservative: don't inherit
134
+ // any attrs, and just reduce the applicability
135
+ appl = Applicability :: MaybeIncorrect ;
136
+ String :: new ( )
137
+ } ,
138
+ } ;
123
139
let generics_sugg = snippet_with_applicability ( cx, generics. span , "" , & mut appl) ;
124
140
let where_clause_sugg = if generics. has_where_clause_predicates {
125
141
format ! (
@@ -143,6 +159,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
143
159
item. span ,
144
160
"try adding this" ,
145
161
& create_new_without_default_suggest_msg (
162
+ & attrs_sugg,
146
163
& self_type_snip,
147
164
& generics_sugg,
148
165
& where_clause_sugg,
@@ -157,13 +174,14 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
157
174
}
158
175
159
176
fn create_new_without_default_suggest_msg (
177
+ attrs_sugg : & str ,
160
178
self_type_snip : & str ,
161
179
generics_sugg : & str ,
162
180
where_clause_sugg : & str ,
163
181
) -> String {
164
182
#[ rustfmt:: skip]
165
183
format ! (
166
- "impl{generics_sugg} Default for {self_type_snip}{where_clause_sugg} {{
184
+ "{attrs_sugg} impl{generics_sugg} Default for {self_type_snip}{where_clause_sugg} {{
167
185
fn default() -> Self {{
168
186
Self::new()
169
187
}}
0 commit comments