@@ -8,7 +8,8 @@ use rustc_hir::{
8
8
AssocItemKind , FieldDef , HirId , ImplItemRef , IsAuto , Item , ItemKind , Mod , QPath , TraitItemRef , TyKind , UseKind ,
9
9
Variant , VariantData ,
10
10
} ;
11
- use rustc_lint:: { LateContext , LateLintPass } ;
11
+ use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
12
+ use rustc_middle:: lint:: in_external_macro;
12
13
use rustc_session:: impl_lint_pass;
13
14
14
15
declare_clippy_lint ! {
@@ -161,7 +162,7 @@ impl ArbitrarySourceItemOrdering {
161
162
}
162
163
163
164
/// Produces a linting warning for incorrectly ordered impl items.
164
- fn lint_impl_item < T : rustc_lint :: LintContext > ( & self , cx : & T , item : & ImplItemRef , before_item : & ImplItemRef ) {
165
+ fn lint_impl_item < T : LintContext > ( & self , cx : & T , item : & ImplItemRef , before_item : & ImplItemRef ) {
165
166
span_lint_and_note (
166
167
cx,
167
168
ARBITRARY_SOURCE_ITEM_ORDERING ,
@@ -176,7 +177,7 @@ impl ArbitrarySourceItemOrdering {
176
177
}
177
178
178
179
/// Produces a linting warning for incorrectly ordered item members.
179
- fn lint_member_name < T : rustc_lint :: LintContext > (
180
+ fn lint_member_name < T : LintContext > (
180
181
cx : & T ,
181
182
ident : & rustc_span:: symbol:: Ident ,
182
183
before_ident : & rustc_span:: symbol:: Ident ,
@@ -191,7 +192,7 @@ impl ArbitrarySourceItemOrdering {
191
192
) ;
192
193
}
193
194
194
- fn lint_member_item < T : rustc_lint :: LintContext > ( cx : & T , item : & Item < ' _ > , before_item : & Item < ' _ > ) {
195
+ fn lint_member_item < T : LintContext > ( cx : & T , item : & Item < ' _ > , before_item : & Item < ' _ > ) {
195
196
let span = if item. ident . as_str ( ) . is_empty ( ) {
196
197
& item. span
197
198
} else {
@@ -210,6 +211,11 @@ impl ArbitrarySourceItemOrdering {
210
211
)
211
212
} ;
212
213
214
+ // This catches false positives where generated code gets linted.
215
+ if span == before_span {
216
+ return ;
217
+ }
218
+
213
219
span_lint_and_note (
214
220
cx,
215
221
ARBITRARY_SOURCE_ITEM_ORDERING ,
@@ -221,7 +227,7 @@ impl ArbitrarySourceItemOrdering {
221
227
}
222
228
223
229
/// Produces a linting warning for incorrectly ordered trait items.
224
- fn lint_trait_item < T : rustc_lint :: LintContext > ( & self , cx : & T , item : & TraitItemRef , before_item : & TraitItemRef ) {
230
+ fn lint_trait_item < T : LintContext > ( & self , cx : & T , item : & TraitItemRef , before_item : & TraitItemRef ) {
225
231
span_lint_and_note (
226
232
cx,
227
233
ARBITRARY_SOURCE_ITEM_ORDERING ,
@@ -242,8 +248,12 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
242
248
ItemKind :: Enum ( enum_def, _generics) if self . enable_ordering_for_enum => {
243
249
let mut cur_v: Option < & Variant < ' _ > > = None ;
244
250
for variant in enum_def. variants {
251
+ if in_external_macro ( cx. sess ( ) , variant. span ) {
252
+ continue ;
253
+ }
254
+
245
255
if let Some ( cur_v) = cur_v {
246
- if cur_v. ident . name . as_str ( ) > variant. ident . name . as_str ( ) {
256
+ if cur_v. ident . name . as_str ( ) > variant. ident . name . as_str ( ) && cur_v . span != variant . span {
247
257
Self :: lint_member_name ( cx, & variant. ident , & cur_v. ident ) ;
248
258
}
249
259
}
@@ -253,8 +263,12 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
253
263
ItemKind :: Struct ( VariantData :: Struct { fields, .. } , _generics) if self . enable_ordering_for_struct => {
254
264
let mut cur_f: Option < & FieldDef < ' _ > > = None ;
255
265
for field in * fields {
266
+ if in_external_macro ( cx. sess ( ) , field. span ) {
267
+ continue ;
268
+ }
269
+
256
270
if let Some ( cur_f) = cur_f {
257
- if cur_f. ident . name . as_str ( ) > field. ident . name . as_str ( ) {
271
+ if cur_f. ident . name . as_str ( ) > field. ident . name . as_str ( ) && cur_f . span != field . span {
258
272
Self :: lint_member_name ( cx, & field. ident , & cur_f. ident ) ;
259
273
}
260
274
}
@@ -267,6 +281,10 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
267
281
let mut cur_t: Option < & TraitItemRef > = None ;
268
282
269
283
for item in * item_ref {
284
+ if in_external_macro ( cx. sess ( ) , item. span ) {
285
+ continue ;
286
+ }
287
+
270
288
if let Some ( cur_t) = cur_t {
271
289
let cur_t_kind = convert_assoc_item_kind ( cur_t. kind ) ;
272
290
let cur_t_kind_index = self . assoc_types_order . index_of ( & cur_t_kind) ;
@@ -286,6 +304,10 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
286
304
let mut cur_t: Option < & ImplItemRef > = None ;
287
305
288
306
for item in trait_impl. items {
307
+ if in_external_macro ( cx. sess ( ) , item. span ) {
308
+ continue ;
309
+ }
310
+
289
311
if let Some ( cur_t) = cur_t {
290
312
let cur_t_kind = convert_assoc_item_kind ( cur_t. kind ) ;
291
313
let cur_t_kind_index = self . assoc_types_order . index_of ( & cur_t_kind) ;
@@ -326,6 +348,10 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
326
348
// as no sorting by source map/line of code has to be applied.
327
349
//
328
350
for item in items {
351
+ if in_external_macro ( cx. sess ( ) , item. span ) {
352
+ continue ;
353
+ }
354
+
329
355
// The following exceptions (skipping with `continue;`) may not be
330
356
// complete, edge cases have not been explored further than what
331
357
// appears in the existing code base.
0 commit comments