diff --git a/clippy_lints/src/arbitrary_source_item_ordering.rs b/clippy_lints/src/arbitrary_source_item_ordering.rs index b7a916bc2afd..bbd7ba47031f 100644 --- a/clippy_lints/src/arbitrary_source_item_ordering.rs +++ b/clippy_lints/src/arbitrary_source_item_ordering.rs @@ -8,7 +8,8 @@ use rustc_hir::{ AssocItemKind, FieldDef, HirId, ImplItemRef, IsAuto, Item, ItemKind, Mod, QPath, TraitItemRef, TyKind, UseKind, Variant, VariantData, }; -use rustc_lint::{LateContext, LateLintPass}; +use rustc_lint::{LateContext, LateLintPass, LintContext}; +use rustc_middle::lint::in_external_macro; use rustc_session::impl_lint_pass; declare_clippy_lint! { @@ -161,7 +162,7 @@ impl ArbitrarySourceItemOrdering { } /// Produces a linting warning for incorrectly ordered impl items. - fn lint_impl_item(&self, cx: &T, item: &ImplItemRef, before_item: &ImplItemRef) { + fn lint_impl_item(&self, cx: &T, item: &ImplItemRef, before_item: &ImplItemRef) { span_lint_and_note( cx, ARBITRARY_SOURCE_ITEM_ORDERING, @@ -176,7 +177,7 @@ impl ArbitrarySourceItemOrdering { } /// Produces a linting warning for incorrectly ordered item members. - fn lint_member_name( + fn lint_member_name( cx: &T, ident: &rustc_span::symbol::Ident, before_ident: &rustc_span::symbol::Ident, @@ -191,7 +192,7 @@ impl ArbitrarySourceItemOrdering { ); } - fn lint_member_item(cx: &T, item: &Item<'_>, before_item: &Item<'_>) { + fn lint_member_item(cx: &T, item: &Item<'_>, before_item: &Item<'_>) { let span = if item.ident.as_str().is_empty() { &item.span } else { @@ -210,6 +211,11 @@ impl ArbitrarySourceItemOrdering { ) }; + // This catches false positives where generated code gets linted. + if span == before_span { + return; + } + span_lint_and_note( cx, ARBITRARY_SOURCE_ITEM_ORDERING, @@ -221,7 +227,7 @@ impl ArbitrarySourceItemOrdering { } /// Produces a linting warning for incorrectly ordered trait items. - fn lint_trait_item(&self, cx: &T, item: &TraitItemRef, before_item: &TraitItemRef) { + fn lint_trait_item(&self, cx: &T, item: &TraitItemRef, before_item: &TraitItemRef) { span_lint_and_note( cx, ARBITRARY_SOURCE_ITEM_ORDERING, @@ -242,8 +248,12 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering { ItemKind::Enum(enum_def, _generics) if self.enable_ordering_for_enum => { let mut cur_v: Option<&Variant<'_>> = None; for variant in enum_def.variants { + if in_external_macro(cx.sess(), variant.span) { + continue; + } + if let Some(cur_v) = cur_v { - if cur_v.ident.name.as_str() > variant.ident.name.as_str() { + if cur_v.ident.name.as_str() > variant.ident.name.as_str() && cur_v.span != variant.span { Self::lint_member_name(cx, &variant.ident, &cur_v.ident); } } @@ -253,8 +263,12 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering { ItemKind::Struct(VariantData::Struct { fields, .. }, _generics) if self.enable_ordering_for_struct => { let mut cur_f: Option<&FieldDef<'_>> = None; for field in *fields { + if in_external_macro(cx.sess(), field.span) { + continue; + } + if let Some(cur_f) = cur_f { - if cur_f.ident.name.as_str() > field.ident.name.as_str() { + if cur_f.ident.name.as_str() > field.ident.name.as_str() && cur_f.span != field.span { Self::lint_member_name(cx, &field.ident, &cur_f.ident); } } @@ -267,6 +281,10 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering { let mut cur_t: Option<&TraitItemRef> = None; for item in *item_ref { + if in_external_macro(cx.sess(), item.span) { + continue; + } + if let Some(cur_t) = cur_t { let cur_t_kind = convert_assoc_item_kind(cur_t.kind); let cur_t_kind_index = self.assoc_types_order.index_of(&cur_t_kind); @@ -286,6 +304,10 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering { let mut cur_t: Option<&ImplItemRef> = None; for item in trait_impl.items { + if in_external_macro(cx.sess(), item.span) { + continue; + } + if let Some(cur_t) = cur_t { let cur_t_kind = convert_assoc_item_kind(cur_t.kind); let cur_t_kind_index = self.assoc_types_order.index_of(&cur_t_kind); @@ -326,6 +348,10 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering { // as no sorting by source map/line of code has to be applied. // for item in items { + if in_external_macro(cx.sess(), item.span) { + continue; + } + // The following exceptions (skipping with `continue;`) may not be // complete, edge cases have not been explored further than what // appears in the existing code base. diff --git a/tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.default.stderr b/tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.default.stderr index c9ec22de9c1d..062bf25ea624 100644 --- a/tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.default.stderr +++ b/tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.default.stderr @@ -36,19 +36,6 @@ LL | | } LL | | } | |_^ -error: incorrect ordering of items (must be alphabetically ordered) - --> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:84:19 - | -LL | #[derive(Default, Clone)] - | ^^^^^ - | -note: should be placed before the following item - --> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:84:10 - | -LL | #[derive(Default, Clone)] - | ^^^^^^^ - = note: this error originates in the derive macro `Clone` which comes from the expansion of the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info) - error: incorrect ordering of items (must be alphabetically ordered) --> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:136:7 | @@ -91,31 +78,17 @@ note: should be placed before `main` LL | fn main() { | ^^^^ -error: incorrect ordering of items (must be alphabetically ordered) - --> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:175:19 - | -LL | #[derive(Default, std::clone::Clone)] - | ^^^^^^^^^^^^^^^^^ - | -note: should be placed before the following item - --> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:175:10 - | -LL | #[derive(Default, std::clone::Clone)] - | ^^^^^^^ - = note: this error originates in the derive macro `std::clone::Clone` which comes from the expansion of the derive macro `Default` (in Nightly builds, run with -Z macro-backtrace for more info) - error: incorrect ordering of items (must be alphabetically ordered) --> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:178:7 | LL | const ZIS_SHOULD_BE_EVEN_EARLIER: () = (); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: should be placed before the following item - --> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:175:19 +note: should be placed before `ZisShouldBeBeforeZeMainFn` + --> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:176:8 | -LL | #[derive(Default, std::clone::Clone)] - | ^^^^^^^^^^^^^^^^^ - = note: this error originates in the derive macro `std::clone::Clone` (in Nightly builds, run with -Z macro-backtrace for more info) +LL | struct ZisShouldBeBeforeZeMainFn; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: incorrect ordering of items (must be alphabetically ordered) --> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:12:11 @@ -249,5 +222,5 @@ note: should be placed before `C` LL | const C: i8 = 0; | ^ -error: aborting due to 19 previous errors +error: aborting due to 17 previous errors