@@ -5,7 +5,7 @@ use clippy_config::types::{
5
5
} ;
6
6
use clippy_utils:: diagnostics:: span_lint_and_note;
7
7
use 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 ,
9
9
Variant , VariantData ,
10
10
} ;
11
11
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
@@ -178,8 +178,8 @@ impl ArbitrarySourceItemOrdering {
178
178
/// Produces a linting warning for incorrectly ordered item members.
179
179
fn lint_member_name < T : LintContext > (
180
180
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 ,
183
183
) {
184
184
span_lint_and_note (
185
185
cx,
@@ -192,21 +192,21 @@ impl ArbitrarySourceItemOrdering {
192
192
}
193
193
194
194
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
197
197
} else {
198
- & item. ident . span
198
+ item. span
199
199
} ;
200
200
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 ( ) {
202
202
(
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 ( ) , ) ,
205
205
)
206
206
} else {
207
207
(
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 ( ) ,
210
210
)
211
211
} ;
212
212
@@ -218,9 +218,9 @@ impl ArbitrarySourceItemOrdering {
218
218
span_lint_and_note (
219
219
cx,
220
220
ARBITRARY_SOURCE_ITEM_ORDERING ,
221
- * span,
221
+ span,
222
222
"incorrect ordering of items (must be alphabetically ordered)" ,
223
- Some ( * before_span) ,
223
+ Some ( before_span) ,
224
224
note,
225
225
) ;
226
226
}
@@ -244,7 +244,7 @@ impl ArbitrarySourceItemOrdering {
244
244
impl < ' tcx > LateLintPass < ' tcx > for ArbitrarySourceItemOrdering {
245
245
fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx Item < ' tcx > ) {
246
246
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 => {
248
248
let mut cur_v: Option < & Variant < ' _ > > = None ;
249
249
for variant in enum_def. variants {
250
250
if variant. span . in_external_macro ( cx. sess ( ) . source_map ( ) ) {
@@ -259,7 +259,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
259
259
cur_v = Some ( variant) ;
260
260
}
261
261
} ,
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 => {
263
263
let mut cur_f: Option < & FieldDef < ' _ > > = None ;
264
264
for field in * fields {
265
265
if field. span . in_external_macro ( cx. sess ( ) . source_map ( ) ) {
@@ -274,7 +274,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
274
274
cur_f = Some ( field) ;
275
275
}
276
276
} ,
277
- ItemKind :: Trait ( is_auto, _safety, _generics, _generic_bounds, item_ref)
277
+ ItemKind :: Trait ( is_auto, _safety, _ident , _generics, _generic_bounds, item_ref)
278
278
if self . enable_ordering_for_trait && * is_auto == IsAuto :: No =>
279
279
{
280
280
let mut cur_t: Option < & TraitItemRef > = None ;
@@ -351,50 +351,24 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
351
351
continue ;
352
352
}
353
353
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
+ } ;
389
363
390
- if item . ident . name . as_str ( ) . starts_with ( '_' ) {
364
+ if ident. name . as_str ( ) . starts_with ( '_' ) {
391
365
// Filters out unnamed macro-like impls for various derives,
392
366
// e.g. serde::Serialize or num_derive::FromPrimitive.
393
367
continue ;
394
368
}
395
369
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 {
398
372
// Filters the auto-included Rust standard library.
399
373
continue ;
400
374
}
@@ -525,6 +499,8 @@ fn get_item_name(item: &Item<'_>) -> String {
525
499
String :: new ( )
526
500
}
527
501
} ,
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 ( ) ,
529
505
}
530
506
}
0 commit comments