@@ -8,7 +8,8 @@ use rustc_hir::{
88 AssocItemKind , FieldDef , HirId , ImplItemRef , IsAuto , Item , ItemKind , Mod , QPath , TraitItemRef , TyKind , UseKind ,
99 Variant , VariantData ,
1010} ;
11- use rustc_lint:: { LateContext , LateLintPass } ;
11+ use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
12+ use rustc_middle:: lint:: in_external_macro;
1213use rustc_session:: impl_lint_pass;
1314
1415declare_clippy_lint ! {
@@ -161,7 +162,7 @@ impl ArbitrarySourceItemOrdering {
161162 }
162163
163164 /// 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 ) {
165166 span_lint_and_note (
166167 cx,
167168 ARBITRARY_SOURCE_ITEM_ORDERING ,
@@ -176,7 +177,7 @@ impl ArbitrarySourceItemOrdering {
176177 }
177178
178179 /// 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 > (
180181 cx : & T ,
181182 ident : & rustc_span:: symbol:: Ident ,
182183 before_ident : & rustc_span:: symbol:: Ident ,
@@ -191,7 +192,7 @@ impl ArbitrarySourceItemOrdering {
191192 ) ;
192193 }
193194
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 < ' _ > ) {
195196 let span = if item. ident . as_str ( ) . is_empty ( ) {
196197 & item. span
197198 } else {
@@ -210,6 +211,11 @@ impl ArbitrarySourceItemOrdering {
210211 )
211212 } ;
212213
214+ // This catches false positives where generated code gets linted.
215+ if span == before_span {
216+ return ;
217+ }
218+
213219 span_lint_and_note (
214220 cx,
215221 ARBITRARY_SOURCE_ITEM_ORDERING ,
@@ -221,7 +227,7 @@ impl ArbitrarySourceItemOrdering {
221227 }
222228
223229 /// 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 ) {
225231 span_lint_and_note (
226232 cx,
227233 ARBITRARY_SOURCE_ITEM_ORDERING ,
@@ -242,8 +248,12 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
242248 ItemKind :: Enum ( enum_def, _generics) if self . enable_ordering_for_enum => {
243249 let mut cur_v: Option < & Variant < ' _ > > = None ;
244250 for variant in enum_def. variants {
251+ if in_external_macro ( cx. sess ( ) , variant. span ) {
252+ continue ;
253+ }
254+
245255 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 {
247257 Self :: lint_member_name ( cx, & variant. ident , & cur_v. ident ) ;
248258 }
249259 }
@@ -253,8 +263,12 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
253263 ItemKind :: Struct ( VariantData :: Struct { fields, .. } , _generics) if self . enable_ordering_for_struct => {
254264 let mut cur_f: Option < & FieldDef < ' _ > > = None ;
255265 for field in * fields {
266+ if in_external_macro ( cx. sess ( ) , field. span ) {
267+ continue ;
268+ }
269+
256270 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 {
258272 Self :: lint_member_name ( cx, & field. ident , & cur_f. ident ) ;
259273 }
260274 }
@@ -267,6 +281,10 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
267281 let mut cur_t: Option < & TraitItemRef > = None ;
268282
269283 for item in * item_ref {
284+ if in_external_macro ( cx. sess ( ) , item. span ) {
285+ continue ;
286+ }
287+
270288 if let Some ( cur_t) = cur_t {
271289 let cur_t_kind = convert_assoc_item_kind ( cur_t. kind ) ;
272290 let cur_t_kind_index = self . assoc_types_order . index_of ( & cur_t_kind) ;
@@ -286,6 +304,10 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
286304 let mut cur_t: Option < & ImplItemRef > = None ;
287305
288306 for item in trait_impl. items {
307+ if in_external_macro ( cx. sess ( ) , item. span ) {
308+ continue ;
309+ }
310+
289311 if let Some ( cur_t) = cur_t {
290312 let cur_t_kind = convert_assoc_item_kind ( cur_t. kind ) ;
291313 let cur_t_kind_index = self . assoc_types_order . index_of ( & cur_t_kind) ;
@@ -326,6 +348,10 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
326348 // as no sorting by source map/line of code has to be applied.
327349 //
328350 for item in items {
351+ if in_external_macro ( cx. sess ( ) , item. span ) {
352+ continue ;
353+ }
354+
329355 // The following exceptions (skipping with `continue;`) may not be
330356 // complete, edge cases have not been explored further than what
331357 // appears in the existing code base.
0 commit comments