@@ -8,12 +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- 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 ,
1212 Variant , VariantData ,
1313} ;
1414use rustc_middle:: ty:: AssocKind ;
1515use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
1616use rustc_session:: impl_lint_pass;
17+ use rustc_span:: Ident ;
1718
1819declare_clippy_lint ! {
1920 /// ### What it does
@@ -195,22 +196,22 @@ impl ArbitrarySourceItemOrdering {
195196 }
196197
197198 /// 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 ) {
199200 span_lint_and_note (
200201 cx,
201202 ARBITRARY_SOURCE_ITEM_ORDERING ,
202- item. span ,
203+ cx . tcx . def_span ( item. owner_id ) ,
203204 format ! (
204205 "incorrect ordering of impl items (defined order: {:?})" ,
205206 self . assoc_types_order
206207 ) ,
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 ) ) ,
209210 ) ;
210211 }
211212
212213 /// 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 ) {
214215 span_lint_and_note (
215216 cx,
216217 ARBITRARY_SOURCE_ITEM_ORDERING ,
@@ -221,7 +222,7 @@ impl ArbitrarySourceItemOrdering {
221222 ) ;
222223 }
223224
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 ) {
225226 let span = if let Some ( ident) = item. kind . ident ( ) {
226227 ident. span
227228 } else {
@@ -246,17 +247,17 @@ impl ArbitrarySourceItemOrdering {
246247 }
247248
248249 /// 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 ) {
250251 span_lint_and_note (
251252 cx,
252253 ARBITRARY_SOURCE_ITEM_ORDERING ,
253- item. span ,
254+ cx . tcx . def_span ( item. owner_id ) ,
254255 format ! (
255256 "incorrect ordering of trait items (defined order: {:?})" ,
256257 self . assoc_types_order
257258 ) ,
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 ) ) ,
260261 ) ;
261262 }
262263}
@@ -284,7 +285,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
284285 && cur_v. ident . name . as_str ( ) > variant. ident . name . as_str ( )
285286 && cur_v. span != variant. span
286287 {
287- Self :: lint_member_name ( cx, & variant. ident , & cur_v. ident ) ;
288+ Self :: lint_member_name ( cx, variant. ident , cur_v. ident ) ;
288289 }
289290 cur_v = Some ( variant) ;
290291 }
@@ -300,57 +301,61 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
300301 && cur_f. ident . name . as_str ( ) > field. ident . name . as_str ( )
301302 && cur_f. span != field. span
302303 {
303- Self :: lint_member_name ( cx, & field. ident , & cur_f. ident ) ;
304+ Self :: lint_member_name ( cx, field. ident , cur_f. ident ) ;
304305 }
305306 cur_f = Some ( field) ;
306307 }
307308 } ,
308309 ItemKind :: Trait ( is_auto, _safety, _ident, _generics, _generic_bounds, item_ref)
309310 if self . enable_ordering_for_trait && * is_auto == IsAuto :: No =>
310311 {
311- let mut cur_t: Option < & TraitItemRef > = None ;
312+ let mut cur_t: Option < ( TraitItemId , Ident ) > = None ;
312313
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 ( ) ) {
315318 continue ;
316319 }
317320
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 ) ;
320323 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 ) ;
322325 let item_kind_index = self . assoc_types_order . index_of ( & item_kind) ;
323326
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 ) ;
326329 } else if cur_t_kind_index > item_kind_index {
327330 self . lint_trait_item ( cx, item, cur_t) ;
328331 }
329332 }
330- cur_t = Some ( item) ;
333+ cur_t = Some ( ( item, ident ) ) ;
331334 }
332335 } ,
333336 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 ;
335338
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 ( ) ) {
338343 continue ;
339344 }
340345
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 ) ;
343348 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 ) ;
345350 let item_kind_index = self . assoc_types_order . index_of ( & item_kind) ;
346351
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 ) ;
349354 } else if cur_t_kind_index > item_kind_index {
350355 self . lint_impl_item ( cx, item, cur_t) ;
351356 }
352357 }
353- cur_t = Some ( item) ;
358+ cur_t = Some ( ( item, ident ) ) ;
354359 }
355360 } ,
356361 _ => { } , // Catch-all for `ItemKinds` that don't have fields.
0 commit comments