@@ -8,11 +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
- AssocItemKind , Attribute , FieldDef , HirId , ImplItemRef , IsAuto , Item , ItemKind , Mod , QPath , TraitItemRef , TyKind ,
12
- Variant , VariantData ,
11
+ Attribute , FieldDef , HirId , ImplItemId , IsAuto , Item , ItemKind , Mod , OwnerId , QPath , TraitItemId , TyKind , Variant ,
12
+ VariantData ,
13
13
} ;
14
14
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
15
+ use rustc_middle:: ty:: AssocKind ;
15
16
use rustc_session:: impl_lint_pass;
17
+ use rustc_span:: Ident ;
16
18
17
19
declare_clippy_lint ! {
18
20
/// ### What it does
@@ -194,22 +196,22 @@ impl ArbitrarySourceItemOrdering {
194
196
}
195
197
196
198
/// Produces a linting warning for incorrectly ordered impl items.
197
- 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 ) {
198
200
span_lint_and_note (
199
201
cx,
200
202
ARBITRARY_SOURCE_ITEM_ORDERING ,
201
- item. span ,
203
+ cx . tcx . def_span ( item. owner_id ) ,
202
204
format ! (
203
205
"incorrect ordering of impl items (defined order: {:?})" ,
204
206
self . assoc_types_order
205
207
) ,
206
- Some ( before_item. span ) ,
207
- 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 ) ) ,
208
210
) ;
209
211
}
210
212
211
213
/// Produces a linting warning for incorrectly ordered item members.
212
- 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 ) {
213
215
span_lint_and_note (
214
216
cx,
215
217
ARBITRARY_SOURCE_ITEM_ORDERING ,
@@ -220,7 +222,7 @@ impl ArbitrarySourceItemOrdering {
220
222
) ;
221
223
}
222
224
223
- 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 ) {
224
226
let span = if let Some ( ident) = item. kind . ident ( ) {
225
227
ident. span
226
228
} else {
@@ -245,17 +247,17 @@ impl ArbitrarySourceItemOrdering {
245
247
}
246
248
247
249
/// Produces a linting warning for incorrectly ordered trait items.
248
- 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 ) {
249
251
span_lint_and_note (
250
252
cx,
251
253
ARBITRARY_SOURCE_ITEM_ORDERING ,
252
- item. span ,
254
+ cx . tcx . def_span ( item. owner_id ) ,
253
255
format ! (
254
256
"incorrect ordering of trait items (defined order: {:?})" ,
255
257
self . assoc_types_order
256
258
) ,
257
- Some ( before_item. span ) ,
258
- 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 ) ) ,
259
261
) ;
260
262
}
261
263
}
@@ -283,7 +285,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
283
285
&& cur_v. ident . name . as_str ( ) > variant. ident . name . as_str ( )
284
286
&& cur_v. span != variant. span
285
287
{
286
- Self :: lint_member_name ( cx, & variant. ident , & cur_v. ident ) ;
288
+ Self :: lint_member_name ( cx, variant. ident , cur_v. ident ) ;
287
289
}
288
290
cur_v = Some ( variant) ;
289
291
}
@@ -299,57 +301,61 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
299
301
&& cur_f. ident . name . as_str ( ) > field. ident . name . as_str ( )
300
302
&& cur_f. span != field. span
301
303
{
302
- Self :: lint_member_name ( cx, & field. ident , & cur_f. ident ) ;
304
+ Self :: lint_member_name ( cx, field. ident , cur_f. ident ) ;
303
305
}
304
306
cur_f = Some ( field) ;
305
307
}
306
308
} ,
307
- ItemKind :: Trait ( is_auto, _safety, _ident, _generics, _generic_bounds, item_ref)
309
+ ItemKind :: Trait ( _constness , is_auto, _safety, _ident, _generics, _generic_bounds, item_ref)
308
310
if self . enable_ordering_for_trait && * is_auto == IsAuto :: No =>
309
311
{
310
- let mut cur_t: Option < & TraitItemRef > = None ;
312
+ let mut cur_t: Option < ( TraitItemId , Ident ) > = None ;
311
313
312
- for item in * item_ref {
313
- 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 ( ) ) {
314
318
continue ;
315
319
}
316
320
317
- if let Some ( cur_t) = cur_t {
318
- let cur_t_kind = convert_assoc_item_kind ( cur_t. kind ) ;
321
+ if let Some ( ( cur_t, cur_ident ) ) = cur_t {
322
+ let cur_t_kind = convert_assoc_item_kind ( cx , cur_t. owner_id ) ;
319
323
let cur_t_kind_index = self . assoc_types_order . index_of ( & cur_t_kind) ;
320
- let item_kind = convert_assoc_item_kind ( item. kind ) ;
324
+ let item_kind = convert_assoc_item_kind ( cx , item. owner_id ) ;
321
325
let item_kind_index = self . assoc_types_order . index_of ( & item_kind) ;
322
326
323
- if cur_t_kind == item_kind && cur_t . ident . name . as_str ( ) > item . ident . name . as_str ( ) {
324
- 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 ) ;
325
329
} else if cur_t_kind_index > item_kind_index {
326
330
self . lint_trait_item ( cx, item, cur_t) ;
327
331
}
328
332
}
329
- cur_t = Some ( item) ;
333
+ cur_t = Some ( ( item, ident ) ) ;
330
334
}
331
335
} ,
332
336
ItemKind :: Impl ( trait_impl) if self . enable_ordering_for_impl => {
333
- let mut cur_t: Option < & ImplItemRef > = None ;
337
+ let mut cur_t: Option < ( ImplItemId , Ident ) > = None ;
334
338
335
- for item in trait_impl. items {
336
- 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 ( ) ) {
337
343
continue ;
338
344
}
339
345
340
- if let Some ( cur_t) = cur_t {
341
- let cur_t_kind = convert_assoc_item_kind ( cur_t. kind ) ;
346
+ if let Some ( ( cur_t, cur_ident ) ) = cur_t {
347
+ let cur_t_kind = convert_assoc_item_kind ( cx , cur_t. owner_id ) ;
342
348
let cur_t_kind_index = self . assoc_types_order . index_of ( & cur_t_kind) ;
343
- let item_kind = convert_assoc_item_kind ( item. kind ) ;
349
+ let item_kind = convert_assoc_item_kind ( cx , item. owner_id ) ;
344
350
let item_kind_index = self . assoc_types_order . index_of ( & item_kind) ;
345
351
346
- if cur_t_kind == item_kind && cur_t . ident . name . as_str ( ) > item . ident . name . as_str ( ) {
347
- 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 ) ;
348
354
} else if cur_t_kind_index > item_kind_index {
349
355
self . lint_impl_item ( cx, item, cur_t) ;
350
356
}
351
357
}
352
- cur_t = Some ( item) ;
358
+ cur_t = Some ( ( item, ident ) ) ;
353
359
}
354
360
} ,
355
361
_ => { } , // Catch-all for `ItemKinds` that don't have fields.
@@ -458,18 +464,20 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
458
464
}
459
465
}
460
466
461
- /// Converts a [`rustc_hir::AssocItemKind`] to a
462
- /// [`SourceItemOrderingTraitAssocItemKind`].
467
+ /// Converts a [`ty::AssocKind`] to a [`SourceItemOrderingTraitAssocItemKind`].
463
468
///
464
469
/// This is implemented here because `rustc_hir` is not a dependency of
465
470
/// `clippy_config`.
466
- fn convert_assoc_item_kind ( value : AssocItemKind ) -> SourceItemOrderingTraitAssocItemKind {
471
+ fn convert_assoc_item_kind ( cx : & LateContext < ' _ > , owner_id : OwnerId ) -> SourceItemOrderingTraitAssocItemKind {
467
472
#[ allow( clippy:: enum_glob_use) ] // Very local glob use for legibility.
468
473
use SourceItemOrderingTraitAssocItemKind :: * ;
469
- match value {
470
- AssocItemKind :: Const => Const ,
471
- AssocItemKind :: Type => Type ,
472
- AssocItemKind :: Fn { .. } => Fn ,
474
+
475
+ let kind = cx. tcx . associated_item ( owner_id. def_id ) . kind ;
476
+
477
+ match kind {
478
+ AssocKind :: Const { .. } => Const ,
479
+ AssocKind :: Type { .. } => Type ,
480
+ AssocKind :: Fn { .. } => Fn ,
473
481
}
474
482
}
475
483
0 commit comments