@@ -5,7 +5,7 @@ use clippy_config::types::{
55} ;
66use clippy_utils:: diagnostics:: span_lint_and_note;
77use rustc_hir:: {
8- AssocItemKind , FieldDef , HirId , ImplItemRef , IsAuto , Item , ItemKind , Mod , QPath , TraitItemRef , TyKind , UseKind ,
8+ AssocItemKind , FieldDef , HirId , ImplItemRef , IsAuto , Item , ItemKind , Mod , QPath , TraitItemRef , TyKind ,
99 Variant , VariantData ,
1010} ;
1111use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
@@ -178,8 +178,8 @@ impl ArbitrarySourceItemOrdering {
178178 /// Produces a linting warning for incorrectly ordered item members.
179179 fn lint_member_name < T : LintContext > (
180180 cx : & T ,
181- ident : & rustc_span:: symbol :: Ident ,
182- before_ident : & rustc_span:: symbol :: Ident ,
181+ ident : & rustc_span:: Ident ,
182+ before_ident : & rustc_span:: Ident ,
183183 ) {
184184 span_lint_and_note (
185185 cx,
@@ -192,21 +192,21 @@ impl ArbitrarySourceItemOrdering {
192192 }
193193
194194 fn lint_member_item < T : LintContext > ( cx : & T , item : & Item < ' _ > , before_item : & Item < ' _ > ) {
195- let span = if item . ident . as_str ( ) . is_empty ( ) {
196- & item . span
195+ let span = if let Some ( ident ) = item . kind . ident ( ) {
196+ ident . span
197197 } else {
198- & item. ident . span
198+ item. span
199199 } ;
200200
201- let ( before_span, note) = if before_item . ident . as_str ( ) . is_empty ( ) {
201+ let ( before_span, note) = if let Some ( ident ) = before_item . kind . ident ( ) {
202202 (
203- & before_item . span ,
204- "should be placed before the following item" . to_owned ( ) ,
203+ ident . span ,
204+ format ! ( "should be placed before `{}`" , ident . as_str ( ) , ) ,
205205 )
206206 } else {
207207 (
208- & before_item. ident . span ,
209- format ! ( "should be placed before `{}`" , before_item . ident . as_str ( ) , ) ,
208+ before_item. span ,
209+ "should be placed before the following item" . to_owned ( ) ,
210210 )
211211 } ;
212212
@@ -218,9 +218,9 @@ impl ArbitrarySourceItemOrdering {
218218 span_lint_and_note (
219219 cx,
220220 ARBITRARY_SOURCE_ITEM_ORDERING ,
221- * span,
221+ span,
222222 "incorrect ordering of items (must be alphabetically ordered)" ,
223- Some ( * before_span) ,
223+ Some ( before_span) ,
224224 note,
225225 ) ;
226226 }
@@ -244,7 +244,7 @@ impl ArbitrarySourceItemOrdering {
244244impl < ' tcx > LateLintPass < ' tcx > for ArbitrarySourceItemOrdering {
245245 fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx Item < ' tcx > ) {
246246 match & item. kind {
247- ItemKind :: Enum ( enum_def, _generics) if self . enable_ordering_for_enum => {
247+ ItemKind :: Enum ( _ , enum_def, _generics) if self . enable_ordering_for_enum => {
248248 let mut cur_v: Option < & Variant < ' _ > > = None ;
249249 for variant in enum_def. variants {
250250 if variant. span . in_external_macro ( cx. sess ( ) . source_map ( ) ) {
@@ -259,7 +259,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
259259 cur_v = Some ( variant) ;
260260 }
261261 } ,
262- ItemKind :: Struct ( VariantData :: Struct { fields, .. } , _generics) if self . enable_ordering_for_struct => {
262+ ItemKind :: Struct ( _ , VariantData :: Struct { fields, .. } , _generics) if self . enable_ordering_for_struct => {
263263 let mut cur_f: Option < & FieldDef < ' _ > > = None ;
264264 for field in * fields {
265265 if field. span . in_external_macro ( cx. sess ( ) . source_map ( ) ) {
@@ -274,7 +274,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
274274 cur_f = Some ( field) ;
275275 }
276276 } ,
277- ItemKind :: Trait ( is_auto, _safety, _generics, _generic_bounds, item_ref)
277+ ItemKind :: Trait ( is_auto, _safety, _ident , _generics, _generic_bounds, item_ref)
278278 if self . enable_ordering_for_trait && * is_auto == IsAuto :: No =>
279279 {
280280 let mut cur_t: Option < & TraitItemRef > = None ;
@@ -351,50 +351,24 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
351351 continue ;
352352 }
353353
354- // The following exceptions (skipping with `continue;`) may not be
355- // complete, edge cases have not been explored further than what
356- // appears in the existing code base.
357- if item. ident . name == rustc_span:: symbol:: kw:: Empty {
358- if let ItemKind :: Impl ( _) = item. kind {
359- // Sorting trait impls for unnamed types makes no sense.
360- if get_item_name ( item) . is_empty ( ) {
361- continue ;
362- }
363- } else if let ItemKind :: ForeignMod { .. } = item. kind {
364- continue ;
365- } else if let ItemKind :: GlobalAsm { .. } = item. kind {
366- continue ;
367- } else if let ItemKind :: Use ( path, use_kind) = item. kind {
368- if path. segments . is_empty ( ) {
369- // Use statements that contain braces get caught here.
370- // They will still be linted internally.
371- continue ;
372- } else if path. segments . len ( ) >= 2
373- && ( path. segments [ 0 ] . ident . name == rustc_span:: sym:: std
374- || path. segments [ 0 ] . ident . name == rustc_span:: sym:: core)
375- && path. segments [ 1 ] . ident . name == rustc_span:: sym:: prelude
376- {
377- // Filters the autogenerated prelude use statement.
378- // e.g. `use std::prelude::rustc_2021`
379- } else if use_kind == UseKind :: Glob {
380- // Filters glob kinds of uses.
381- // e.g. `use std::sync::*`
382- } else {
383- // This can be used for debugging.
384- // println!("Unknown autogenerated use statement: {:?}", item);
385- }
386- continue ;
387- }
388- }
354+ let ident = if let Some ( ident) = item. kind . ident ( ) {
355+ ident
356+ } else if let ItemKind :: Impl ( _) = item. kind
357+ && !get_item_name ( item) . is_empty ( )
358+ {
359+ rustc_span:: Ident :: empty ( ) // FIXME: a bit strange, is there a better way to do it?
360+ } else {
361+ continue ;
362+ } ;
389363
390- if item . ident . name . as_str ( ) . starts_with ( '_' ) {
364+ if ident. name . as_str ( ) . starts_with ( '_' ) {
391365 // Filters out unnamed macro-like impls for various derives,
392366 // e.g. serde::Serialize or num_derive::FromPrimitive.
393367 continue ;
394368 }
395369
396- if item . ident . name == rustc_span:: sym:: std && item. span . is_dummy ( ) {
397- if let ItemKind :: ExternCrate ( None ) = item. kind {
370+ if ident. name == rustc_span:: sym:: std && item. span . is_dummy ( ) {
371+ if let ItemKind :: ExternCrate ( None , _ ) = item. kind {
398372 // Filters the auto-included Rust standard library.
399373 continue ;
400374 }
@@ -525,6 +499,8 @@ fn get_item_name(item: &Item<'_>) -> String {
525499 String :: new ( )
526500 }
527501 } ,
528- _ => item. ident . name . as_str ( ) . to_owned ( ) ,
502+ // FIXME: `Ident::empty` for anonymous items is a bit strange, is there
503+ // a better way to do it?
504+ _ => item. kind . ident ( ) . unwrap_or ( rustc_span:: Ident :: empty ( ) ) . name . as_str ( ) . to_owned ( ) ,
529505 }
530506}
0 commit comments