Skip to content

Commit 1239c30

Browse files
committed
fixup! new lint: source_item_ordering
1 parent 72fccdf commit 1239c30

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

clippy_lints/src/arbitrary_source_item_ordering.rs

+33-7
Original file line numberDiff line numberDiff line change
@@ -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;
1213
use rustc_session::impl_lint_pass;
1314

1415
declare_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.

tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.default.stderr

+5-32
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,6 @@ LL | | }
3636
LL | | }
3737
| |_^
3838

39-
error: incorrect ordering of items (must be alphabetically ordered)
40-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:84:19
41-
|
42-
LL | #[derive(Default, Clone)]
43-
| ^^^^^
44-
|
45-
note: should be placed before the following item
46-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:84:10
47-
|
48-
LL | #[derive(Default, Clone)]
49-
| ^^^^^^^
50-
= 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)
51-
5239
error: incorrect ordering of items (must be alphabetically ordered)
5340
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:136:7
5441
|
@@ -91,31 +78,17 @@ note: should be placed before `main`
9178
LL | fn main() {
9279
| ^^^^
9380

94-
error: incorrect ordering of items (must be alphabetically ordered)
95-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:175:19
96-
|
97-
LL | #[derive(Default, std::clone::Clone)]
98-
| ^^^^^^^^^^^^^^^^^
99-
|
100-
note: should be placed before the following item
101-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:175:10
102-
|
103-
LL | #[derive(Default, std::clone::Clone)]
104-
| ^^^^^^^
105-
= 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)
106-
10781
error: incorrect ordering of items (must be alphabetically ordered)
10882
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:178:7
10983
|
11084
LL | const ZIS_SHOULD_BE_EVEN_EARLIER: () = ();
11185
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
11286
|
113-
note: should be placed before the following item
114-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:175:19
87+
note: should be placed before `ZisShouldBeBeforeZeMainFn`
88+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:176:8
11589
|
116-
LL | #[derive(Default, std::clone::Clone)]
117-
| ^^^^^^^^^^^^^^^^^
118-
= note: this error originates in the derive macro `std::clone::Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
90+
LL | struct ZisShouldBeBeforeZeMainFn;
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
11992

12093
error: incorrect ordering of items (must be alphabetically ordered)
12194
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:12:11
@@ -249,5 +222,5 @@ note: should be placed before `C`
249222
LL | const C: i8 = 0;
250223
| ^
251224

252-
error: aborting due to 19 previous errors
225+
error: aborting due to 17 previous errors
253226

0 commit comments

Comments
 (0)