@@ -8,11 +8,13 @@ use clippy_utils::diagnostics::span_lint_and_note;
88use clippy_utils:: is_cfg_test;
99use rustc_attr_data_structures:: AttributeKind ;
1010use 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 ,
1313} ;
1414use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
15+ use rustc_middle:: ty:: AssocKind ;
1516use rustc_session:: impl_lint_pass;
17+ use rustc_span:: Ident ;
1618
1719declare_clippy_lint ! {
1820 /// ### What it does
@@ -194,22 +196,22 @@ impl ArbitrarySourceItemOrdering {
194196 }
195197
196198 /// 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 ) {
198200 span_lint_and_note (
199201 cx,
200202 ARBITRARY_SOURCE_ITEM_ORDERING ,
201- item. span ,
203+ cx . tcx . def_span ( item. owner_id ) ,
202204 format ! (
203205 "incorrect ordering of impl items (defined order: {:?})" ,
204206 self . assoc_types_order
205207 ) ,
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 ) ) ,
208210 ) ;
209211 }
210212
211213 /// 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 ) {
213215 span_lint_and_note (
214216 cx,
215217 ARBITRARY_SOURCE_ITEM_ORDERING ,
@@ -220,7 +222,7 @@ impl ArbitrarySourceItemOrdering {
220222 ) ;
221223 }
222224
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 ) {
224226 let span = if let Some ( ident) = item. kind . ident ( ) {
225227 ident. span
226228 } else {
@@ -245,17 +247,17 @@ impl ArbitrarySourceItemOrdering {
245247 }
246248
247249 /// 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 ) {
249251 span_lint_and_note (
250252 cx,
251253 ARBITRARY_SOURCE_ITEM_ORDERING ,
252- item. span ,
254+ cx . tcx . def_span ( item. owner_id ) ,
253255 format ! (
254256 "incorrect ordering of trait items (defined order: {:?})" ,
255257 self . assoc_types_order
256258 ) ,
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 ) ) ,
259261 ) ;
260262 }
261263}
@@ -283,7 +285,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
283285 && cur_v. ident . name . as_str ( ) > variant. ident . name . as_str ( )
284286 && cur_v. span != variant. span
285287 {
286- Self :: lint_member_name ( cx, & variant. ident , & cur_v. ident ) ;
288+ Self :: lint_member_name ( cx, variant. ident , cur_v. ident ) ;
287289 }
288290 cur_v = Some ( variant) ;
289291 }
@@ -299,57 +301,61 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
299301 && cur_f. ident . name . as_str ( ) > field. ident . name . as_str ( )
300302 && cur_f. span != field. span
301303 {
302- Self :: lint_member_name ( cx, & field. ident , & cur_f. ident ) ;
304+ Self :: lint_member_name ( cx, field. ident , cur_f. ident ) ;
303305 }
304306 cur_f = Some ( field) ;
305307 }
306308 } ,
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)
308310 if self . enable_ordering_for_trait && * is_auto == IsAuto :: No =>
309311 {
310- let mut cur_t: Option < & TraitItemRef > = None ;
312+ let mut cur_t: Option < ( TraitItemId , Ident ) > = None ;
311313
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 ( ) ) {
314318 continue ;
315319 }
316320
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 ) ;
319323 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 ) ;
321325 let item_kind_index = self . assoc_types_order . index_of ( & item_kind) ;
322326
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 ) ;
325329 } else if cur_t_kind_index > item_kind_index {
326330 self . lint_trait_item ( cx, item, cur_t) ;
327331 }
328332 }
329- cur_t = Some ( item) ;
333+ cur_t = Some ( ( item, ident ) ) ;
330334 }
331335 } ,
332336 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 ;
334338
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 ( ) ) {
337343 continue ;
338344 }
339345
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 ) ;
342348 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 ) ;
344350 let item_kind_index = self . assoc_types_order . index_of ( & item_kind) ;
345351
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 ) ;
348354 } else if cur_t_kind_index > item_kind_index {
349355 self . lint_impl_item ( cx, item, cur_t) ;
350356 }
351357 }
352- cur_t = Some ( item) ;
358+ cur_t = Some ( ( item, ident ) ) ;
353359 }
354360 } ,
355361 _ => { } , // Catch-all for `ItemKinds` that don't have fields.
@@ -458,18 +464,20 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
458464 }
459465}
460466
461- /// Converts a [`rustc_hir::AssocItemKind`] to a
462- /// [`SourceItemOrderingTraitAssocItemKind`].
467+ /// Converts a [`ty::AssocKind`] to a [`SourceItemOrderingTraitAssocItemKind`].
463468///
464469/// This is implemented here because `rustc_hir` is not a dependency of
465470/// `clippy_config`.
466- fn convert_assoc_item_kind ( value : AssocItemKind ) -> SourceItemOrderingTraitAssocItemKind {
471+ fn convert_assoc_item_kind ( cx : & LateContext < ' _ > , owner_id : OwnerId ) -> SourceItemOrderingTraitAssocItemKind {
467472 #[ allow( clippy:: enum_glob_use) ] // Very local glob use for legibility.
468473 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 ,
473481 }
474482}
475483
0 commit comments