@@ -6,8 +6,8 @@ use clippy_config::types::{
66} ;
77use clippy_utils:: diagnostics:: span_lint_and_note;
88use rustc_hir:: {
9- AssocItemKind , FieldDef , HirId , ImplItemRef , IsAuto , Item , ItemKind , Mod , QPath , TraitItemRef , TyKind , UseKind ,
10- Variant , VariantData ,
9+ AssocItemKind , FieldDef , HirId , ImplItemRef , IsAuto , Item , ItemKind , Mod , QPath , TraitItemRef , TyKind , Variant ,
10+ VariantData ,
1111} ;
1212use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
1313use rustc_session:: impl_lint_pass;
@@ -202,11 +202,7 @@ impl ArbitrarySourceItemOrdering {
202202 }
203203
204204 /// Produces a linting warning for incorrectly ordered item members.
205- fn lint_member_name < T : LintContext > (
206- cx : & T ,
207- ident : & rustc_span:: symbol:: Ident ,
208- before_ident : & rustc_span:: symbol:: Ident ,
209- ) {
205+ fn lint_member_name < T : LintContext > ( cx : & T , ident : & rustc_span:: Ident , before_ident : & rustc_span:: Ident ) {
210206 span_lint_and_note (
211207 cx,
212208 ARBITRARY_SOURCE_ITEM_ORDERING ,
@@ -218,21 +214,18 @@ impl ArbitrarySourceItemOrdering {
218214 }
219215
220216 fn lint_member_item < T : LintContext > ( cx : & T , item : & Item < ' _ > , before_item : & Item < ' _ > , msg : & ' static str ) {
221- let span = if item . ident . as_str ( ) . is_empty ( ) {
222- & item . span
217+ let span = if let Some ( ident ) = item . kind . ident ( ) {
218+ ident . span
223219 } else {
224- & item. ident . span
220+ item. span
225221 } ;
226222
227- let ( before_span, note) = if before_item. ident . as_str ( ) . is_empty ( ) {
228- (
229- & before_item. span ,
230- "should be placed before the following item" . to_owned ( ) ,
231- )
223+ let ( before_span, note) = if let Some ( ident) = before_item. kind . ident ( ) {
224+ ( ident. span , format ! ( "should be placed before `{}`" , ident. as_str( ) , ) )
232225 } else {
233226 (
234- & before_item. ident . span ,
235- format ! ( "should be placed before `{}`" , before_item . ident . as_str ( ) , ) ,
227+ before_item. span ,
228+ "should be placed before the following item" . to_owned ( ) ,
236229 )
237230 } ;
238231
@@ -241,7 +234,7 @@ impl ArbitrarySourceItemOrdering {
241234 return ;
242235 }
243236
244- span_lint_and_note ( cx, ARBITRARY_SOURCE_ITEM_ORDERING , * span, msg, Some ( * before_span) , note) ;
237+ span_lint_and_note ( cx, ARBITRARY_SOURCE_ITEM_ORDERING , span, msg, Some ( before_span) , note) ;
245238 }
246239
247240 /// Produces a linting warning for incorrectly ordered trait items.
@@ -263,7 +256,7 @@ impl ArbitrarySourceItemOrdering {
263256impl < ' tcx > LateLintPass < ' tcx > for ArbitrarySourceItemOrdering {
264257 fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx Item < ' tcx > ) {
265258 match & item. kind {
266- ItemKind :: Enum ( enum_def, _generics) if self . enable_ordering_for_enum => {
259+ ItemKind :: Enum ( _ , enum_def, _generics) if self . enable_ordering_for_enum => {
267260 let mut cur_v: Option < & Variant < ' _ > > = None ;
268261 for variant in enum_def. variants {
269262 if variant. span . in_external_macro ( cx. sess ( ) . source_map ( ) ) {
@@ -278,7 +271,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
278271 cur_v = Some ( variant) ;
279272 }
280273 } ,
281- ItemKind :: Struct ( VariantData :: Struct { fields, .. } , _generics) if self . enable_ordering_for_struct => {
274+ ItemKind :: Struct ( _ , VariantData :: Struct { fields, .. } , _generics) if self . enable_ordering_for_struct => {
282275 let mut cur_f: Option < & FieldDef < ' _ > > = None ;
283276 for field in * fields {
284277 if field. span . in_external_macro ( cx. sess ( ) . source_map ( ) ) {
@@ -293,7 +286,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
293286 cur_f = Some ( field) ;
294287 }
295288 } ,
296- ItemKind :: Trait ( is_auto, _safety, _generics, _generic_bounds, item_ref)
289+ ItemKind :: Trait ( is_auto, _safety, _ident , _generics, _generic_bounds, item_ref)
297290 if self . enable_ordering_for_trait && * is_auto == IsAuto :: No =>
298291 {
299292 let mut cur_t: Option < & TraitItemRef > = None ;
@@ -370,50 +363,24 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
370363 continue ;
371364 }
372365
373- // The following exceptions (skipping with `continue;`) may not be
374- // complete, edge cases have not been explored further than what
375- // appears in the existing code base.
376- if item. ident . name == rustc_span:: symbol:: kw:: Empty {
377- if let ItemKind :: Impl ( _) = item. kind {
378- // Sorting trait impls for unnamed types makes no sense.
379- if get_item_name ( item) . is_empty ( ) {
380- continue ;
381- }
382- } else if let ItemKind :: ForeignMod { .. } = item. kind {
383- continue ;
384- } else if let ItemKind :: GlobalAsm { .. } = item. kind {
385- continue ;
386- } else if let ItemKind :: Use ( path, use_kind) = item. kind {
387- if path. segments . is_empty ( ) {
388- // Use statements that contain braces get caught here.
389- // They will still be linted internally.
390- continue ;
391- } else if path. segments . len ( ) >= 2
392- && ( path. segments [ 0 ] . ident . name == rustc_span:: sym:: std
393- || path. segments [ 0 ] . ident . name == rustc_span:: sym:: core)
394- && path. segments [ 1 ] . ident . name == rustc_span:: sym:: prelude
395- {
396- // Filters the autogenerated prelude use statement.
397- // e.g. `use std::prelude::rustc_2021`
398- } else if use_kind == UseKind :: Glob {
399- // Filters glob kinds of uses.
400- // e.g. `use std::sync::*`
401- } else {
402- // This can be used for debugging.
403- // println!("Unknown autogenerated use statement: {:?}", item);
404- }
405- continue ;
406- }
407- }
366+ let ident = if let Some ( ident) = item. kind . ident ( ) {
367+ ident
368+ } else if let ItemKind :: Impl ( _) = item. kind
369+ && !get_item_name ( item) . is_empty ( )
370+ {
371+ rustc_span:: Ident :: empty ( ) // FIXME: a bit strange, is there a better way to do it?
372+ } else {
373+ continue ;
374+ } ;
408375
409- if item . ident . name . as_str ( ) . starts_with ( '_' ) {
376+ if ident. name . as_str ( ) . starts_with ( '_' ) {
410377 // Filters out unnamed macro-like impls for various derives,
411378 // e.g. serde::Serialize or num_derive::FromPrimitive.
412379 continue ;
413380 }
414381
415- if item . ident . name == rustc_span:: sym:: std && item. span . is_dummy ( ) {
416- if let ItemKind :: ExternCrate ( None ) = item. kind {
382+ if ident. name == rustc_span:: sym:: std && item. span . is_dummy ( ) {
383+ if let ItemKind :: ExternCrate ( None , _ ) = item. kind {
417384 // Filters the auto-included Rust standard library.
418385 continue ;
419386 }
@@ -559,6 +526,14 @@ fn get_item_name(item: &Item<'_>) -> String {
559526 String :: new ( )
560527 }
561528 } ,
562- _ => item. ident . name . as_str ( ) . to_owned ( ) ,
529+ // FIXME: `Ident::empty` for anonymous items is a bit strange, is there
530+ // a better way to do it?
531+ _ => item
532+ . kind
533+ . ident ( )
534+ . unwrap_or ( rustc_span:: Ident :: empty ( ) )
535+ . name
536+ . as_str ( )
537+ . to_owned ( ) ,
563538 }
564539}
0 commit comments