Skip to content

Commit dcd8bb6

Browse files
Skip inner ordering checking in presence of #[repr(…)] (#14610)
A representation attribute `#[repr(…)]` might indicate that the ordering of the fields or the variants is dictated by the API the code is interfacing with. Better not lint with `arbitrary_source_item_ordering` in this case. changelog: [`arbitrary_source_item_ordering`]: do not lint inside items with `#[repr]` attribute
2 parents 604b7b0 + d39600e commit dcd8bb6

File tree

5 files changed

+192
-77
lines changed

5 files changed

+192
-77
lines changed

clippy_lints/src/arbitrary_source_item_ordering.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use clippy_config::types::{
66
};
77
use clippy_utils::diagnostics::span_lint_and_note;
88
use clippy_utils::is_cfg_test;
9+
use rustc_attr_parsing::AttributeKind;
910
use rustc_hir::{
10-
AssocItemKind, FieldDef, HirId, ImplItemRef, IsAuto, Item, ItemKind, Mod, QPath, TraitItemRef, TyKind, Variant,
11-
VariantData,
11+
AssocItemKind, Attribute, FieldDef, HirId, ImplItemRef, IsAuto, Item, ItemKind, Mod, QPath, TraitItemRef, TyKind,
12+
Variant, VariantData,
1213
};
1314
use rustc_lint::{LateContext, LateLintPass, LintContext};
1415
use rustc_session::impl_lint_pass;
@@ -28,6 +29,11 @@ declare_clippy_lint! {
2829
/// implemented in the code. Sometimes this will be referred to as
2930
/// "bikeshedding".
3031
///
32+
/// The content of items with a representation clause attribute, such as
33+
/// `#[repr(C)]` will not be checked, as the order of their fields or
34+
/// variants might be dictated by an external API (application binary
35+
/// interface).
36+
///
3137
/// ### Default Ordering and Configuration
3238
///
3339
/// As there is no generally applicable rule, and each project may have
@@ -256,6 +262,15 @@ impl ArbitrarySourceItemOrdering {
256262

257263
impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
258264
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
265+
if cx
266+
.tcx
267+
.hir_attrs(item.hir_id())
268+
.iter()
269+
.any(|attr| matches!(attr, Attribute::Parsed(AttributeKind::Repr(..))))
270+
{
271+
// Do not lint items with a `#[repr]` attribute as their layout may be imposed by an external API.
272+
return;
273+
}
259274
match &item.kind {
260275
ItemKind::Enum(_, enum_def, _generics) if self.enable_ordering_for_enum => {
261276
let mut cur_v: Option<&Variant<'_>> = None;

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

+47-23
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,37 @@ LL | const SNAKE_CASE: &str = "zzzzzzzz";
1313
= help: to override `-D warnings` add `#[allow(clippy::arbitrary_source_item_ordering)]`
1414

1515
error: incorrect ordering of items (module item groupings specify another order)
16-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:149:7
16+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:165:7
1717
|
1818
LL | const ZIS_SHOULD_BE_REALLY_EARLY: () = ();
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
2020
|
2121
note: should be placed before `TraitUnorderedItemKinds`
22-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:136:7
22+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:152:7
2323
|
2424
LL | trait TraitUnorderedItemKinds {
2525
| ^^^^^^^^^^^^^^^^^^^^^^^
2626

2727
error: incorrect ordering of items (module item groupings specify another order)
28-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:188:5
28+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:204:5
2929
|
3030
LL | mod this_is_in_the_wrong_position {
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232
|
3333
note: should be placed before `main`
34-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:183:4
34+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:199:4
3535
|
3636
LL | fn main() {
3737
| ^^^^
3838

3939
error: incorrect ordering of items (module item groupings specify another order)
40-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:198:7
40+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:214:7
4141
|
4242
LL | const ZIS_SHOULD_BE_EVEN_EARLIER: () = ();
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
4444
|
4545
note: should be placed before `ZisShouldBeBeforeZeMainFn`
46-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:196:8
46+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:212:8
4747
|
4848
LL | struct ZisShouldBeBeforeZeMainFn;
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -61,100 +61,124 @@ LL | C,
6161
| ^
6262

6363
error: incorrect ordering of items (must be alphabetically ordered)
64-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:96:5
64+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:57:5
65+
|
66+
LL | g: u8,
67+
| ^
68+
|
69+
note: should be placed before `r`
70+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:56:5
71+
|
72+
LL | r: u8,
73+
| ^
74+
75+
error: incorrect ordering of items (must be alphabetically ordered)
76+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:59:5
77+
|
78+
LL | b: u8,
79+
| ^
80+
|
81+
note: should be placed before `g`
82+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:57:5
83+
|
84+
LL | g: u8,
85+
| ^
86+
87+
error: incorrect ordering of items (must be alphabetically ordered)
88+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:112:5
6589
|
6690
LL | b: bool,
6791
| ^
6892
|
6993
note: should be placed before `c`
70-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:95:5
94+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:111:5
7195
|
7296
LL | c: bool,
7397
| ^
7498

7599
error: incorrect ordering of items (must be alphabetically ordered)
76-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:105:5
100+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:121:5
77101
|
78102
LL | b: bool,
79103
| ^
80104
|
81105
note: should be placed before `c`
82-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:104:5
106+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:120:5
83107
|
84108
LL | c: bool,
85109
| ^
86110

87111
error: incorrect ordering of items (must be alphabetically ordered)
88-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:125:11
112+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:141:11
89113
|
90114
LL | const B: bool;
91115
| ^
92116
|
93117
note: should be placed before `C`
94-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:124:11
118+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:140:11
95119
|
96120
LL | const C: bool;
97121
| ^
98122

99123
error: incorrect ordering of items (must be alphabetically ordered)
100-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:132:8
124+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:148:8
101125
|
102126
LL | fn b();
103127
| ^
104128
|
105129
note: should be placed before `c`
106-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:131:8
130+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:147:8
107131
|
108132
LL | fn c();
109133
| ^
110134

111135
error: incorrect ordering of trait items (defined order: [Const, Type, Fn])
112-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:139:5
136+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:155:5
113137
|
114138
LL | const A: bool;
115139
| ^^^^^^^^^^^^^^
116140
|
117141
note: should be placed before `SomeType`
118-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:137:5
142+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:153:5
119143
|
120144
LL | type SomeType;
121145
| ^^^^^^^^^^^^^^
122146

123147
error: incorrect ordering of items (must be alphabetically ordered)
124-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:155:11
148+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:171:11
125149
|
126150
LL | const B: bool = false;
127151
| ^
128152
|
129153
note: should be placed before `C`
130-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:154:11
154+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:170:11
131155
|
132156
LL | const C: bool = false;
133157
| ^
134158

135159
error: incorrect ordering of items (must be alphabetically ordered)
136-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:162:8
160+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:178:8
137161
|
138162
LL | fn b() {}
139163
| ^
140164
|
141165
note: should be placed before `c`
142-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:161:8
166+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:177:8
143167
|
144168
LL | fn c() {}
145169
| ^
146170

147171
error: incorrect ordering of impl items (defined order: [Const, Type, Fn])
148-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:173:5
172+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:189:5
149173
|
150174
LL | const A: bool = false;
151175
| ^^^^^^^^^^^^^^^^^^^^^^
152176
|
153177
note: should be placed before `SomeType`
154-
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:171:5
178+
--> tests/ui-toml/arbitrary_source_item_ordering/ordering_mixed.rs:187:5
155179
|
156180
LL | type SomeType = ();
157181
| ^^^^^^^^^^^^^^^^^^^
158182

159-
error: aborting due to 13 previous errors
183+
error: aborting due to 15 previous errors
160184

0 commit comments

Comments
 (0)