@@ -8,12 +8,13 @@ use clippy_utils::diagnostics::span_lint_and_note;
8
8
use clippy_utils:: is_cfg_test;
9
9
use rustc_attr_data_structures:: AttributeKind ;
10
10
use rustc_hir:: {
11
- Attribute , FieldDef , HirId , ImplItemRef , IsAuto , Item , ItemKind , Mod , OwnerId , QPath , TraitItemRef , TyKind ,
11
+ Attribute , FieldDef , HirId , IsAuto , ImplItemId , Item , ItemKind , Mod , OwnerId , QPath , TraitItemId , TyKind ,
12
12
Variant , VariantData ,
13
13
} ;
14
14
use rustc_middle:: ty:: AssocKind ;
15
15
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
16
16
use rustc_session:: impl_lint_pass;
17
+ use rustc_span:: Ident ;
17
18
18
19
declare_clippy_lint ! {
19
20
/// ### What it does
@@ -195,22 +196,22 @@ impl ArbitrarySourceItemOrdering {
195
196
}
196
197
197
198
/// Produces a linting warning for incorrectly ordered impl items.
198
- fn lint_impl_item < T : LintContext > ( & self , cx : & T , item : & ImplItemRef , before_item : & ImplItemRef ) {
199
+ fn lint_impl_item ( & self , cx : & LateContext < ' _ > , item : ImplItemId , before_item : ImplItemId ) {
199
200
span_lint_and_note (
200
201
cx,
201
202
ARBITRARY_SOURCE_ITEM_ORDERING ,
202
- item. span ,
203
+ cx . tcx . def_span ( item. owner_id ) ,
203
204
format ! (
204
205
"incorrect ordering of impl items (defined order: {:?})" ,
205
206
self . assoc_types_order
206
207
) ,
207
- Some ( before_item. span ) ,
208
- format ! ( "should be placed before `{}`" , before_item . ident . name ) ,
208
+ Some ( cx . tcx . def_span ( before_item. owner_id ) ) ,
209
+ format ! ( "should be placed before `{}`" , cx . tcx . item_name ( before_item . owner_id ) ) ,
209
210
) ;
210
211
}
211
212
212
213
/// Produces a linting warning for incorrectly ordered item members.
213
- fn lint_member_name < T : LintContext > ( cx : & T , ident : & rustc_span :: Ident , before_ident : & rustc_span :: Ident ) {
214
+ fn lint_member_name < T : LintContext > ( cx : & T , ident : Ident , before_ident : Ident ) {
214
215
span_lint_and_note (
215
216
cx,
216
217
ARBITRARY_SOURCE_ITEM_ORDERING ,
@@ -221,7 +222,7 @@ impl ArbitrarySourceItemOrdering {
221
222
) ;
222
223
}
223
224
224
- fn lint_member_item < T : LintContext > ( cx : & T , item : & Item < ' _ > , before_item : & Item < ' _ > , msg : & ' static str ) {
225
+ fn lint_member_item ( cx : & LateContext < ' _ > , item : & Item < ' _ > , before_item : & Item < ' _ > , msg : & ' static str ) {
225
226
let span = if let Some ( ident) = item. kind . ident ( ) {
226
227
ident. span
227
228
} else {
@@ -246,17 +247,17 @@ impl ArbitrarySourceItemOrdering {
246
247
}
247
248
248
249
/// Produces a linting warning for incorrectly ordered trait items.
249
- fn lint_trait_item < T : LintContext > ( & self , cx : & T , item : & TraitItemRef , before_item : & TraitItemRef ) {
250
+ fn lint_trait_item ( & self , cx : & LateContext < ' _ > , item : TraitItemId , before_item : TraitItemId ) {
250
251
span_lint_and_note (
251
252
cx,
252
253
ARBITRARY_SOURCE_ITEM_ORDERING ,
253
- item. span ,
254
+ cx . tcx . def_span ( item. owner_id ) ,
254
255
format ! (
255
256
"incorrect ordering of trait items (defined order: {:?})" ,
256
257
self . assoc_types_order
257
258
) ,
258
- Some ( before_item. span ) ,
259
- format ! ( "should be placed before `{}`" , before_item . ident . name ) ,
259
+ Some ( cx . tcx . def_span ( before_item. owner_id ) ) ,
260
+ format ! ( "should be placed before `{}`" , cx . tcx . item_name ( before_item . owner_id ) ) ,
260
261
) ;
261
262
}
262
263
}
@@ -284,7 +285,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
284
285
&& cur_v. ident . name . as_str ( ) > variant. ident . name . as_str ( )
285
286
&& cur_v. span != variant. span
286
287
{
287
- Self :: lint_member_name ( cx, & variant. ident , & cur_v. ident ) ;
288
+ Self :: lint_member_name ( cx, variant. ident , cur_v. ident ) ;
288
289
}
289
290
cur_v = Some ( variant) ;
290
291
}
@@ -300,57 +301,61 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
300
301
&& cur_f. ident . name . as_str ( ) > field. ident . name . as_str ( )
301
302
&& cur_f. span != field. span
302
303
{
303
- Self :: lint_member_name ( cx, & field. ident , & cur_f. ident ) ;
304
+ Self :: lint_member_name ( cx, field. ident , cur_f. ident ) ;
304
305
}
305
306
cur_f = Some ( field) ;
306
307
}
307
308
} ,
308
309
ItemKind :: Trait ( is_auto, _safety, _ident, _generics, _generic_bounds, item_ref)
309
310
if self . enable_ordering_for_trait && * is_auto == IsAuto :: No =>
310
311
{
311
- let mut cur_t: Option < & TraitItemRef > = None ;
312
+ let mut cur_t: Option < ( TraitItemId , Ident ) > = None ;
312
313
313
- for item in * item_ref {
314
- if item. span . in_external_macro ( cx. sess ( ) . source_map ( ) ) {
314
+ for & item in * item_ref {
315
+ let span = cx. tcx . def_span ( item. owner_id ) ;
316
+ let ident = cx. tcx . item_ident ( item. owner_id ) ;
317
+ if span. in_external_macro ( cx. sess ( ) . source_map ( ) ) {
315
318
continue ;
316
319
}
317
320
318
- if let Some ( cur_t) = cur_t {
319
- let cur_t_kind = convert_assoc_item_kind ( cx, cur_t. id . owner_id ) ;
321
+ if let Some ( ( cur_t, cur_ident ) ) = cur_t {
322
+ let cur_t_kind = convert_assoc_item_kind ( cx, cur_t. owner_id ) ;
320
323
let cur_t_kind_index = self . assoc_types_order . index_of ( & cur_t_kind) ;
321
- let item_kind = convert_assoc_item_kind ( cx, item. id . owner_id ) ;
324
+ let item_kind = convert_assoc_item_kind ( cx, item. owner_id ) ;
322
325
let item_kind_index = self . assoc_types_order . index_of ( & item_kind) ;
323
326
324
- if cur_t_kind == item_kind && cur_t . ident . name . as_str ( ) > item . ident . name . as_str ( ) {
325
- Self :: lint_member_name ( cx, & item . ident , & cur_t . ident ) ;
327
+ if cur_t_kind == item_kind && cur_ident . name . as_str ( ) > ident. name . as_str ( ) {
328
+ Self :: lint_member_name ( cx, ident, cur_ident ) ;
326
329
} else if cur_t_kind_index > item_kind_index {
327
330
self . lint_trait_item ( cx, item, cur_t) ;
328
331
}
329
332
}
330
- cur_t = Some ( item) ;
333
+ cur_t = Some ( ( item, ident ) ) ;
331
334
}
332
335
} ,
333
336
ItemKind :: Impl ( trait_impl) if self . enable_ordering_for_impl => {
334
- let mut cur_t: Option < & ImplItemRef > = None ;
337
+ let mut cur_t: Option < ( ImplItemId , Ident ) > = None ;
335
338
336
- for item in trait_impl. items {
337
- if item. span . in_external_macro ( cx. sess ( ) . source_map ( ) ) {
339
+ for & item in trait_impl. items {
340
+ let span = cx. tcx . def_span ( item. owner_id ) ;
341
+ let ident = cx. tcx . item_ident ( item. owner_id ) ;
342
+ if span. in_external_macro ( cx. sess ( ) . source_map ( ) ) {
338
343
continue ;
339
344
}
340
345
341
- if let Some ( cur_t) = cur_t {
342
- let cur_t_kind = convert_assoc_item_kind ( cx, cur_t. id . owner_id ) ;
346
+ if let Some ( ( cur_t, cur_ident ) ) = cur_t {
347
+ let cur_t_kind = convert_assoc_item_kind ( cx, cur_t. owner_id ) ;
343
348
let cur_t_kind_index = self . assoc_types_order . index_of ( & cur_t_kind) ;
344
- let item_kind = convert_assoc_item_kind ( cx, item. id . owner_id ) ;
349
+ let item_kind = convert_assoc_item_kind ( cx, item. owner_id ) ;
345
350
let item_kind_index = self . assoc_types_order . index_of ( & item_kind) ;
346
351
347
- if cur_t_kind == item_kind && cur_t . ident . name . as_str ( ) > item . ident . name . as_str ( ) {
348
- Self :: lint_member_name ( cx, & item . ident , & cur_t . ident ) ;
352
+ if cur_t_kind == item_kind && cur_ident . name . as_str ( ) > ident. name . as_str ( ) {
353
+ Self :: lint_member_name ( cx, ident, cur_ident ) ;
349
354
} else if cur_t_kind_index > item_kind_index {
350
355
self . lint_impl_item ( cx, item, cur_t) ;
351
356
}
352
357
}
353
- cur_t = Some ( item) ;
358
+ cur_t = Some ( ( item, ident ) ) ;
354
359
}
355
360
} ,
356
361
_ => { } , // Catch-all for `ItemKinds` that don't have fields.
0 commit comments