Skip to content

Commit 4feb2f0

Browse files
committed
Make alphabetic ordering in module item groups configurable (new default: off)
From feedback to this lint after its inclusion in clippy 1.82, this has turned out to be the most requested improvement. With this improvement, it is possible to make the lint check certain top-level structural checks on modules (e.g. use statements and module inclusions at the top), but still leaving everything else up to the developer.
1 parent a19d69d commit 4feb2f0

File tree

13 files changed

+397
-97
lines changed

13 files changed

+397
-97
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6211,6 +6211,7 @@ Released 2018-09-13
62116211
[`min-ident-chars-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#min-ident-chars-threshold
62126212
[`missing-docs-in-crate-items`]: https://doc.rust-lang.org/clippy/lint_configuration.html#missing-docs-in-crate-items
62136213
[`module-item-order-groupings`]: https://doc.rust-lang.org/clippy/lint_configuration.html#module-item-order-groupings
6214+
[`module-items-ordered-within-groups`]: https://doc.rust-lang.org/clippy/lint_configuration.html#module-items-ordered-within-groups
62146215
[`msrv`]: https://doc.rust-lang.org/clippy/lint_configuration.html#msrv
62156216
[`pass-by-value-size-limit`]: https://doc.rust-lang.org/clippy/lint_configuration.html#pass-by-value-size-limit
62166217
[`pub-underscore-fields-behavior`]: https://doc.rust-lang.org/clippy/lint_configuration.html#pub-underscore-fields-behavior

book/src/lint_configuration.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,16 @@ The named groupings of different source item kinds within modules.
676676
* [`arbitrary_source_item_ordering`](https://rust-lang.github.io/rust-clippy/master/index.html#arbitrary_source_item_ordering)
677677

678678

679+
## `module-items-ordered-within-groups`
680+
Whether the items within module groups should be ordered alphabetically or not.
681+
682+
**Default Value:** `false`
683+
684+
---
685+
**Affected lints:**
686+
* [`arbitrary_source_item_ordering`](https://rust-lang.github.io/rust-clippy/master/index.html#arbitrary_source_item_ordering)
687+
688+
679689
## `msrv`
680690
The minimum rust version that the project supports. Defaults to the `rust-version` field in `Cargo.toml`
681691

clippy_config/src/conf.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,9 @@ define_Conf! {
563563
/// The named groupings of different source item kinds within modules.
564564
#[lints(arbitrary_source_item_ordering)]
565565
module_item_order_groupings: SourceItemOrderingModuleItemGroupings = DEFAULT_MODULE_ITEM_ORDERING_GROUPS.into(),
566+
/// Whether the items within module groups should be ordered alphabetically or not.
567+
#[lints(arbitrary_source_item_ordering)]
568+
module_items_ordered_within_groups: bool = false,
566569
/// The minimum rust version that the project supports. Defaults to the `rust-version` field in `Cargo.toml`
567570
#[default_text = "current version"]
568571
#[lints(

clippy_lints/src/arbitrary_source_item_ordering.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,21 @@ declare_clippy_lint! {
5858
/// | `PascalCase` | "ty_alias", "opaque_ty", "enum", "struct", "union", "trait", "trait_alias", "impl" |
5959
/// | `lower_snake_case` | "fn" |
6060
///
61+
/// The groups' names are arbitrary and can be changed to suit the
62+
/// conventions that should be enforced for a specific project.
63+
///
6164
/// All item kinds must be accounted for to create an enforceable linting
62-
/// rule set.
65+
/// rule set. Following are some example configurations that may be useful.
66+
///
67+
/// Example: *module inclusions and use statements to be at the top*
68+
///
69+
/// ```toml
70+
/// module-item-order-groupings = [
71+
/// [ "modules", [ "extern_crate", "mod", "foreign_mod" ], ],
72+
/// [ "use", [ "use", ], ],
73+
/// [ "everything_else", [ "macro", "global_asm", "static", "const", "ty_alias", "enum", "struct", "union", "trait", "trait_alias", "impl", "fn", ], ],
74+
/// ]
75+
/// ```
6376
///
6477
/// ### Known Problems
6578
///
@@ -144,6 +157,7 @@ pub struct ArbitrarySourceItemOrdering {
144157
enable_ordering_for_struct: bool,
145158
enable_ordering_for_trait: bool,
146159
module_item_order_groupings: SourceItemOrderingModuleItemGroupings,
160+
module_items_ordered_within_groups: bool,
147161
}
148162

149163
impl ArbitrarySourceItemOrdering {
@@ -158,6 +172,7 @@ impl ArbitrarySourceItemOrdering {
158172
enable_ordering_for_struct: conf.source_item_ordering.contains(&Struct),
159173
enable_ordering_for_trait: conf.source_item_ordering.contains(&Trait),
160174
module_item_order_groupings: conf.module_item_order_groupings.clone(),
175+
module_items_ordered_within_groups: conf.module_items_ordered_within_groups,
161176
}
162177
}
163178

@@ -417,7 +432,9 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
417432
Ordering::Equal if item_kind == SourceItemOrderingModuleItemKind::Use => {
418433
// Skip ordering use statements, as these should be ordered by rustfmt.
419434
},
420-
Ordering::Equal if cur_t.name > get_item_name(item) => {
435+
Ordering::Equal
436+
if (self.module_items_ordered_within_groups && cur_t.name > get_item_name(item)) =>
437+
{
421438
Self::lint_member_item(cx, item, cur_t.item);
422439
},
423440
Ordering::Equal | Ordering::Greater => {

tests/ui-toml/arbitrary_source_item_ordering/default_exp/clippy.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ module-item-order-groupings = [
99
["PascalCase", ["ty_alias", "enum", "struct", "union", "trait", "trait_alias", "impl"]],
1010
["lower_snake_case", ["fn"]]
1111
]
12-
12+
module-items-ordered-within-groups = false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module-items-ordered-within-groups = true
Lines changed: 27 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,226 +1,160 @@
11
error: incorrect ordering of items (must be alphabetically ordered)
2-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:21:14
2+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:22:14
33
|
44
LL | use std::rc::Weak;
55
| ^^^^
66
|
77
note: should be placed before `SNAKE_CASE`
8-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:19:7
8+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:20:7
99
|
1010
LL | const SNAKE_CASE: &str = "zzzzzzzz";
1111
| ^^^^^^^^^^
1212
= note: `-D clippy::arbitrary-source-item-ordering` implied by `-D warnings`
1313
= help: to override `-D warnings` add `#[allow(clippy::arbitrary_source_item_ordering)]`
1414

1515
error: incorrect ordering of items (must be alphabetically ordered)
16-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:64:1
17-
|
18-
LL | / impl CloneSelf for StructOrdered {
19-
LL | | fn clone_self(&self) -> Self {
20-
LL | | Self {
21-
LL | | a: true,
22-
... |
23-
LL | | }
24-
LL | | }
25-
| |_^
26-
|
27-
note: should be placed before the following item
28-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:54:1
29-
|
30-
LL | / impl Default for StructOrdered {
31-
LL | | fn default() -> Self {
32-
LL | | Self {
33-
LL | | a: true,
34-
... |
35-
LL | | }
36-
LL | | }
37-
| |_^
38-
39-
error: incorrect ordering of items (must be alphabetically ordered)
40-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:136:7
16+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:137:7
4117
|
4218
LL | const ZIS_SHOULD_BE_REALLY_EARLY: () = ();
4319
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
4420
|
4521
note: should be placed before `TraitUnorderedItemKinds`
46-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:124:7
22+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:125:7
4723
|
4824
LL | trait TraitUnorderedItemKinds {
4925
| ^^^^^^^^^^^^^^^^^^^^^^^
5026

5127
error: incorrect ordering of items (must be alphabetically ordered)
52-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:151:1
53-
|
54-
LL | impl BasicEmptyTrait for StructOrdered {}
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56-
|
57-
note: should be placed before the following item
58-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:138:1
59-
|
60-
LL | / impl TraitUnordered for StructUnordered {
61-
LL | | const A: bool = false;
62-
LL | | const C: bool = false;
63-
LL | | const B: bool = false;
64-
... |
65-
LL | | fn b() {}
66-
LL | | }
67-
| |_^
68-
69-
error: incorrect ordering of items (must be alphabetically ordered)
70-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:170:5
28+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:171:5
7129
|
7230
LL | mod this_is_in_the_wrong_position {
7331
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7432
|
7533
note: should be placed before `main`
76-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:165:4
34+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:166:4
7735
|
7836
LL | fn main() {
7937
| ^^^^
8038

8139
error: incorrect ordering of items (must be alphabetically ordered)
82-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:178:7
40+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:179:7
8341
|
8442
LL | const ZIS_SHOULD_BE_EVEN_EARLIER: () = ();
8543
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
8644
|
8745
note: should be placed before `ZisShouldBeBeforeZeMainFn`
88-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:176:8
46+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:177:8
8947
|
9048
LL | struct ZisShouldBeBeforeZeMainFn;
9149
| ^^^^^^^^^^^^^^^^^^^^^^^^^
9250

9351
error: incorrect ordering of items (must be alphabetically ordered)
94-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:12:11
95-
|
96-
LL | const AFTER: i8 = 0;
97-
| ^^^^^
98-
|
99-
note: should be placed before `BEFORE`
100-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:10:11
101-
|
102-
LL | const BEFORE: i8 = 0;
103-
| ^^^^^^
104-
105-
error: incorrect ordering of items (must be alphabetically ordered)
106-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:38:5
52+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:39:5
10753
|
10854
LL | B,
10955
| ^
11056
|
11157
note: should be placed before `C`
112-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:37:5
58+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:38:5
11359
|
11460
LL | C,
11561
| ^
11662

11763
error: incorrect ordering of items (must be alphabetically ordered)
118-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:88:5
64+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:89:5
11965
|
12066
LL | b: bool,
12167
| ^
12268
|
12369
note: should be placed before `c`
124-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:87:5
70+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:88:5
12571
|
12672
LL | c: bool,
12773
| ^
12874

12975
error: incorrect ordering of items (must be alphabetically ordered)
130-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:96:5
76+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:97:5
13177
|
13278
LL | b: bool,
13379
| ^
13480
|
13581
note: should be placed before `c`
136-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:95:5
82+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:96:5
13783
|
13884
LL | c: bool,
13985
| ^
14086

14187
error: incorrect ordering of items (must be alphabetically ordered)
142-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:115:11
88+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:116:11
14389
|
14490
LL | const B: bool;
14591
| ^
14692
|
14793
note: should be placed before `C`
148-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:114:11
94+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:115:11
14995
|
15096
LL | const C: bool;
15197
| ^
15298

15399
error: incorrect ordering of items (must be alphabetically ordered)
154-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:121:8
100+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:122:8
155101
|
156102
LL | fn b();
157103
| ^
158104
|
159105
note: should be placed before `c`
160-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:120:8
106+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:121:8
161107
|
162108
LL | fn c();
163109
| ^
164110

165111
error: incorrect ordering of trait items (defined order: [Const, Type, Fn])
166-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:127:5
112+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:128:5
167113
|
168114
LL | const A: bool;
169115
| ^^^^^^^^^^^^^^
170116
|
171117
note: should be placed before `SomeType`
172-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:125:5
118+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:126:5
173119
|
174120
LL | type SomeType;
175121
| ^^^^^^^^^^^^^^
176122

177123
error: incorrect ordering of items (must be alphabetically ordered)
178-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:141:11
124+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:142:11
179125
|
180126
LL | const B: bool = false;
181127
| ^
182128
|
183129
note: should be placed before `C`
184-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:140:11
130+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:141:11
185131
|
186132
LL | const C: bool = false;
187133
| ^
188134

189135
error: incorrect ordering of items (must be alphabetically ordered)
190-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:147:8
136+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:148:8
191137
|
192138
LL | fn b() {}
193139
| ^
194140
|
195141
note: should be placed before `c`
196-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:146:8
142+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:147:8
197143
|
198144
LL | fn c() {}
199145
| ^
200146

201147
error: incorrect ordering of impl items (defined order: [Const, Type, Fn])
202-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:156:5
148+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:157:5
203149
|
204150
LL | const A: bool = false;
205151
| ^^^^^^^^^^^^^^^^^^^^^^
206152
|
207153
note: should be placed before `SomeType`
208-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:154:5
154+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:155:5
209155
|
210156
LL | type SomeType = ();
211157
| ^^^^^^^^^^^^^^^^^^^
212158

213-
error: incorrect ordering of items (must be alphabetically ordered)
214-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:172:11
215-
|
216-
LL | const A: i8 = 1;
217-
| ^
218-
|
219-
note: should be placed before `C`
220-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:171:11
221-
|
222-
LL | const C: i8 = 0;
223-
| ^
224-
225-
error: aborting due to 17 previous errors
159+
error: aborting due to 13 previous errors
226160

0 commit comments

Comments
 (0)