Skip to content

Commit 029a94c

Browse files
authored
Merge pull request #127 from Mirko-A/mirko/allow-nonstandard-style-accessor
init: allow `nonstandard_style` for generated accessor/value
2 parents d164f4c + deeb124 commit 029a94c

7 files changed

Lines changed: 92 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3939

4040
- Corrected `T: Sized` bounds to `T: ?Sized` in the generated `PinnedDrop`
4141
check by `#[pin_data]`.
42+
- `#[pin_data]` no longer produces additional `non_snake_case` warnings if field names
43+
are not of snake case. Standard field definition warnings are unaffected.
44+
- `init!` and `pin_init!` no longer produce `non_snake_case` warnings if field names
45+
are not of snake case. Warnings on the struct definition are unaffected.
4246

4347
## [0.0.10] - 2025-08-19
4448

internal/src/init.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,9 @@ fn init_fields(
296296
#init
297297

298298
#(#cfgs)*
299-
#[allow(unused_variables)]
299+
// Allow `non_snake_case` since the same warning is going to be reported for the struct
300+
// field.
301+
#[allow(unused_variables, non_snake_case)]
300302
let #ident = #guard.let_binding();
301303
});
302304

internal/src/pin_data.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ fn generate_unpin_impl(
191191
quote! {
192192
// This struct will be used for the unpin analysis. It is needed, because only structurally
193193
// pinned fields are relevant whether the struct should implement `Unpin`.
194-
#[allow(dead_code)] // The fields below are never used.
194+
#[allow(
195+
dead_code, // The fields below are never used.
196+
non_snake_case // The warning will be emitted on the struct definition.
197+
)]
195198
struct __Unpin #generics_with_pin_lt
196199
#where_token
197200
#predicates
@@ -318,7 +321,9 @@ fn generate_projections(
318321
let docs = format!(" Pin-projections of [`{ident}`]");
319322
quote! {
320323
#[doc = #docs]
321-
#[allow(dead_code)]
324+
// Allow `non_snake_case` since the same warning will be emitted on
325+
// the struct definition.
326+
#[allow(dead_code, non_snake_case)]
322327
#[doc(hidden)]
323328
#vis struct #projection #generics_with_pin_lt
324329
#whr
@@ -386,6 +391,9 @@ fn generate_the_pin_data(
386391
/// - `(*slot).#field_name` points to uninitialized and exclusively accessed
387392
/// memory.
388393
#(#cfg_attrs)*
394+
// Allow `non_snake_case` since the same warning will be emitted on
395+
// the struct definition.
396+
#[allow(non_snake_case)]
389397
#[inline(always)]
390398
#vis unsafe fn #field_name(
391399
self,

tests/nonstandard_style.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//! Tests that no extra warnings are emitted for non-snake-case fields when using
2+
//! `#[pin_data]`, `init!` or `pin_init!`.
3+
//!
4+
//! See: https://github.com/Rust-for-Linux/pin-init/issues/125
5+
6+
#![deny(nonstandard_style)]
7+
#![allow(dead_code)]
8+
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
9+
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
10+
11+
use pin_init::*;
12+
13+
#[allow(non_snake_case)]
14+
struct Foo {
15+
NON_STANDARD_A: usize,
16+
nonStandardB: Bar,
17+
}
18+
19+
#[allow(non_snake_case)]
20+
struct Bar {
21+
Non_Standard_C: usize,
22+
}
23+
24+
impl Foo {
25+
fn new() -> impl Init<Self> {
26+
init!(Self {
27+
NON_STANDARD_A: {
28+
#[expect(
29+
nonstandard_style,
30+
reason = "User code warnings should not be suppressed"
31+
)]
32+
(0..2).map(|NonStandardInUserCode| NonStandardInUserCode + 1).sum()
33+
},
34+
nonStandardB <- init!(Bar { Non_Standard_C: 42 }),
35+
})
36+
}
37+
}
38+
39+
// Non-camel-case struct name should not produce warnings.
40+
#[allow(nonstandard_style)]
41+
#[pin_data]
42+
struct non_standard_baz {
43+
NON_STANDARD: usize,
44+
#[pin]
45+
nonStandardPin: usize,
46+
}
47+
48+
impl non_standard_baz {
49+
fn new(a: impl PinInit<usize>) -> impl PinInit<Self> {
50+
pin_init!(Self {
51+
NON_STANDARD: {
52+
#[expect(
53+
nonstandard_style,
54+
reason = "User code warnings should not be suppressed"
55+
)]
56+
let NON_STANDARD_IN_USER_CODE = 41;
57+
NON_STANDARD_IN_USER_CODE + 1
58+
},
59+
nonStandardPin <- a,
60+
})
61+
}
62+
}

tests/ui/expand/many_generics.expanded.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ where
1313
_pin: PhantomPinned,
1414
}
1515
/// Pin-projections of [`Foo`]
16-
#[allow(dead_code)]
16+
#[allow(dead_code, non_snake_case)]
1717
#[doc(hidden)]
1818
struct FooProjection<'__pin, 'a, 'b: 'a, T: Bar<'b> + ?Sized + 'a, const SIZE: usize = 0>
1919
where
@@ -98,6 +98,7 @@ const _: () = {
9898
/// - `(*slot).#field_name` is properly aligned.
9999
/// - `(*slot).#field_name` points to uninitialized and exclusively accessed
100100
/// memory.
101+
#[allow(non_snake_case)]
101102
#[inline(always)]
102103
unsafe fn array(
103104
self,
@@ -114,6 +115,7 @@ const _: () = {
114115
/// - `(*slot).#field_name` is properly aligned.
115116
/// - `(*slot).#field_name` points to uninitialized and exclusively accessed
116117
/// memory.
118+
#[allow(non_snake_case)]
117119
#[inline(always)]
118120
unsafe fn r(
119121
self,
@@ -130,6 +132,7 @@ const _: () = {
130132
/// - `(*slot).#field_name` is properly aligned.
131133
/// - `(*slot).#field_name` points to uninitialized and exclusively accessed
132134
/// memory.
135+
#[allow(non_snake_case)]
133136
#[inline(always)]
134137
unsafe fn _pin(
135138
self,
@@ -157,7 +160,7 @@ const _: () = {
157160
}
158161
}
159162
}
160-
#[allow(dead_code)]
163+
#[allow(dead_code, non_snake_case)]
161164
struct __Unpin<'__pin, 'a, 'b: 'a, T: Bar<'b> + ?Sized + 'a, const SIZE: usize = 0>
162165
where
163166
T: Bar<'a, 1>,

tests/ui/expand/pin-data.expanded.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ struct Foo {
55
_pin: PhantomPinned,
66
}
77
/// Pin-projections of [`Foo`]
8-
#[allow(dead_code)]
8+
#[allow(dead_code, non_snake_case)]
99
#[doc(hidden)]
1010
struct FooProjection<'__pin> {
1111
array: &'__pin mut [u8; 1024 * 1024],
@@ -62,6 +62,7 @@ const _: () = {
6262
/// - `(*slot).#field_name` is properly aligned.
6363
/// - `(*slot).#field_name` points to uninitialized and exclusively accessed
6464
/// memory.
65+
#[allow(non_snake_case)]
6566
#[inline(always)]
6667
unsafe fn array(
6768
self,
@@ -78,6 +79,7 @@ const _: () = {
7879
/// - `(*slot).#field_name` is properly aligned.
7980
/// - `(*slot).#field_name` points to uninitialized and exclusively accessed
8081
/// memory.
82+
#[allow(non_snake_case)]
8183
#[inline(always)]
8284
unsafe fn _pin(
8385
self,
@@ -97,7 +99,7 @@ const _: () = {
9799
}
98100
}
99101
}
100-
#[allow(dead_code)]
102+
#[allow(dead_code, non_snake_case)]
101103
struct __Unpin<'__pin> {
102104
__phantom_pin: ::pin_init::__internal::PhantomInvariantLifetime<'__pin>,
103105
__phantom: ::pin_init::__internal::PhantomInvariant<Foo>,

tests/ui/expand/pinned_drop.expanded.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ struct Foo {
55
_pin: PhantomPinned,
66
}
77
/// Pin-projections of [`Foo`]
8-
#[allow(dead_code)]
8+
#[allow(dead_code, non_snake_case)]
99
#[doc(hidden)]
1010
struct FooProjection<'__pin> {
1111
array: &'__pin mut [u8; 1024 * 1024],
@@ -62,6 +62,7 @@ const _: () = {
6262
/// - `(*slot).#field_name` is properly aligned.
6363
/// - `(*slot).#field_name` points to uninitialized and exclusively accessed
6464
/// memory.
65+
#[allow(non_snake_case)]
6566
#[inline(always)]
6667
unsafe fn array(
6768
self,
@@ -78,6 +79,7 @@ const _: () = {
7879
/// - `(*slot).#field_name` is properly aligned.
7980
/// - `(*slot).#field_name` points to uninitialized and exclusively accessed
8081
/// memory.
82+
#[allow(non_snake_case)]
8183
#[inline(always)]
8284
unsafe fn _pin(
8385
self,
@@ -97,7 +99,7 @@ const _: () = {
9799
}
98100
}
99101
}
100-
#[allow(dead_code)]
102+
#[allow(dead_code, non_snake_case)]
101103
struct __Unpin<'__pin> {
102104
__phantom_pin: ::pin_init::__internal::PhantomInvariantLifetime<'__pin>,
103105
__phantom: ::pin_init::__internal::PhantomInvariant<Foo>,

0 commit comments

Comments
 (0)