@@ -6,8 +6,8 @@ use clippy_config::types::{
6
6
} ;
7
7
use clippy_utils:: diagnostics:: span_lint_and_note;
8
8
use 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 ,
11
11
} ;
12
12
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
13
13
use rustc_session:: impl_lint_pass;
@@ -202,11 +202,7 @@ impl ArbitrarySourceItemOrdering {
202
202
}
203
203
204
204
/// 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 ) {
210
206
span_lint_and_note (
211
207
cx,
212
208
ARBITRARY_SOURCE_ITEM_ORDERING ,
@@ -218,21 +214,18 @@ impl ArbitrarySourceItemOrdering {
218
214
}
219
215
220
216
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
223
219
} else {
224
- & item. ident . span
220
+ item. span
225
221
} ;
226
222
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( ) , ) )
232
225
} else {
233
226
(
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 ( ) ,
236
229
)
237
230
} ;
238
231
@@ -241,7 +234,7 @@ impl ArbitrarySourceItemOrdering {
241
234
return ;
242
235
}
243
236
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) ;
245
238
}
246
239
247
240
/// Produces a linting warning for incorrectly ordered trait items.
@@ -263,7 +256,7 @@ impl ArbitrarySourceItemOrdering {
263
256
impl < ' tcx > LateLintPass < ' tcx > for ArbitrarySourceItemOrdering {
264
257
fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx Item < ' tcx > ) {
265
258
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 => {
267
260
let mut cur_v: Option < & Variant < ' _ > > = None ;
268
261
for variant in enum_def. variants {
269
262
if variant. span . in_external_macro ( cx. sess ( ) . source_map ( ) ) {
@@ -278,7 +271,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
278
271
cur_v = Some ( variant) ;
279
272
}
280
273
} ,
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 => {
282
275
let mut cur_f: Option < & FieldDef < ' _ > > = None ;
283
276
for field in * fields {
284
277
if field. span . in_external_macro ( cx. sess ( ) . source_map ( ) ) {
@@ -293,7 +286,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
293
286
cur_f = Some ( field) ;
294
287
}
295
288
} ,
296
- ItemKind :: Trait ( is_auto, _safety, _generics, _generic_bounds, item_ref)
289
+ ItemKind :: Trait ( is_auto, _safety, _ident , _generics, _generic_bounds, item_ref)
297
290
if self . enable_ordering_for_trait && * is_auto == IsAuto :: No =>
298
291
{
299
292
let mut cur_t: Option < & TraitItemRef > = None ;
@@ -370,50 +363,24 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
370
363
continue ;
371
364
}
372
365
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
+ } ;
408
375
409
- if item . ident . name . as_str ( ) . starts_with ( '_' ) {
376
+ if ident. name . as_str ( ) . starts_with ( '_' ) {
410
377
// Filters out unnamed macro-like impls for various derives,
411
378
// e.g. serde::Serialize or num_derive::FromPrimitive.
412
379
continue ;
413
380
}
414
381
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 {
417
384
// Filters the auto-included Rust standard library.
418
385
continue ;
419
386
}
@@ -559,6 +526,14 @@ fn get_item_name(item: &Item<'_>) -> String {
559
526
String :: new ( )
560
527
}
561
528
} ,
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 ( ) ,
563
538
}
564
539
}
0 commit comments