Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

## [0.0.10] - 2025-08-19

Expand Down
34 changes: 25 additions & 9 deletions internal/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,25 @@ fn init_fields(
};
let init = match kind {
InitializerKind::Value { ident, value } => {
let mut value_ident = ident.clone();
let value_prep = value.as_ref().map(|value| &value.1).map(|value| {
// Setting the span of `value_ident` to `value`'s span improves error messages
// when the type of `value` is wrong.
value_ident.set_span(value.span());
quote!(let #value_ident = #value;)
});
let (value_ident, value_prep) = match value.as_ref() {
None => {
// This is of short-hand syntax, so just use the identifier directly.
(ident.clone(), None)
}
Some((_, value)) => {
// We have an expression. Evaluate it to a local variable first, outside
// `unsafe {}` block.
//
// Setting the span of `value_ident` to `value`'s span improves error messages
// when the type of `value` is wrong.
(
format_ident!("value", span = value.span()),
Some(quote! {
let value = #value;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out this has to use #value_ident which has the correct span, otherwise the test will fail with the following diff

diff --git a/tests/nonstandard_style.rs b/tests/nonstandard_style.rs                                                                                               
index 33c62db81997..5020961dfde2 100644                                                                                                                            
--- a/tests/nonstandard_style.rs
+++ b/tests/nonstandard_style.rs
@@ -21,6 +21,14 @@ struct Bar {
     Non_Standard_C: usize,
 }

+macro_rules! wrap_init {
+    ($($args:tt)*) => {
+        ::pin_init::init!(
+            $($args)*
+        )
+    }
+}
+
 impl Foo {
     fn new() -> impl Init<Self> {
         init!(Self {
@@ -31,7 +39,7 @@ impl Foo {
                 )]
                 (0..2).map(|NonStandardInUserCode| NonStandardInUserCode + 1).sum()
             },
-            nonStandardB <- init!(Bar { Non_Standard_C: 42 }),
+            nonStandardB <- wrap_init!(Bar {Non_Standard_C: 1}),
         })
     }
 }

(Just wrapping the macro).

I'm kinda surprised that non of our existing test caught this, but when trying to build kernel with this diff there're lots of fallouts.

}),
)
}
};
// Again span for better diagnostics
let write = quote_spanned!(ident.span()=> ::core::ptr::write);
// NOTE: the field accessor ensures that the initialized field is properly aligned.
Expand All @@ -273,7 +285,9 @@ fn init_fields(
unsafe { #write(&raw mut (*#slot).#ident, #value_ident) };
}
#(#cfgs)*
#[allow(unused_variables)]
// Allow `non_snake_case` since the same warning is going to be reported for
// the struct field.
#[allow(unused_variables, non_snake_case)]
let #ident = #accessor;
}
}
Expand Down Expand Up @@ -325,7 +339,9 @@ fn init_fields(
#value_init
}
#(#cfgs)*
#[allow(unused_variables)]
// Allow `non_snake_case` since the same warning is going to be reported for
// the struct field.
#[allow(unused_variables, non_snake_case)]
let #ident = #accessor;
}
}
Expand Down
12 changes: 10 additions & 2 deletions internal/src/pin_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ fn generate_unpin_impl(
quote! {
// This struct will be used for the unpin analysis. It is needed, because only structurally
// pinned fields are relevant whether the struct should implement `Unpin`.
#[allow(dead_code)] // The fields below are never used.
#[allow(
dead_code, // The fields below are never used.
non_snake_case // The warning will be emitted on the struct definition.
)]
struct __Unpin #generics_with_pin_lt
#where_token
#predicates
Expand Down Expand Up @@ -302,7 +305,9 @@ fn generate_projections(
let docs = format!(" Pin-projections of [`{ident}`]");
quote! {
#[doc = #docs]
#[allow(dead_code)]
// Allow `non_snake_case` since the same warning will be emitted on
// the struct definition.
#[allow(dead_code, non_snake_case)]
#[doc(hidden)]
#vis struct #projection #generics_with_pin_lt {
#(#fields_decl)*
Expand Down Expand Up @@ -395,6 +400,9 @@ fn generate_the_pin_data(
/// to deallocate.
#pin_safety
#(#attrs)*
// Allow `non_snake_case` since the same warning will be emitted on
// the struct definition.
#[allow(non_snake_case)]
#vis unsafe fn #ident<E>(
self,
slot: *mut #ty,
Expand Down
62 changes: 62 additions & 0 deletions tests/nonstandard_style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//! Tests that no extra warnings are emitted for non-snake-case fields when using
//! `#[pin_data]`, `init!` or `pin_init!`.
//!
//! See: https://github.com/Rust-for-Linux/pin-init/issues/125

#![deny(nonstandard_style)]
#![allow(dead_code)]
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]

use pin_init::*;

#[allow(non_snake_case)]
struct Foo {
NON_STANDARD_A: usize,
nonStandardB: Bar,
}

#[allow(non_snake_case)]
struct Bar {
Non_Standard_C: usize,
}

Comment thread
nbdd0121 marked this conversation as resolved.
impl Foo {
fn new() -> impl Init<Self> {
init!(Self {
NON_STANDARD_A: {
#[expect(
nonstandard_style,
reason = "User code warnings should not be suppressed"
)]
(0..2).map(|NonStandardInUserCode| NonStandardInUserCode + 1).sum()
},
nonStandardB <- init!(Bar { Non_Standard_C: 42 }),
})
}
}

// Non-camel-case struct name should not produce warnings.
#[allow(nonstandard_style)]
#[pin_data]
struct non_standard_baz {
NON_STANDARD: usize,
#[pin]
nonStandardPin: usize,
}

impl non_standard_baz {
fn new(a: impl PinInit<usize>) -> impl PinInit<Self> {
pin_init!(Self {
NON_STANDARD: {
#[expect(
nonstandard_style,
reason = "User code warnings should not be suppressed"
)]
let NON_STANDARD_IN_USER_CODE = 41;
NON_STANDARD_IN_USER_CODE + 1
},
nonStandardPin <- a,
})
}
}
7 changes: 5 additions & 2 deletions tests/ui/expand/many_generics.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ where
_pin: PhantomPinned,
}
/// Pin-projections of [`Foo`]
#[allow(dead_code)]
#[allow(dead_code, non_snake_case)]
#[doc(hidden)]
struct FooProjection<
'__pin,
Expand Down Expand Up @@ -92,6 +92,7 @@ const _: () = {
/// - `slot` is a valid pointer to uninitialized memory.
/// - the caller does not touch `slot` when `Err` is returned, they are only permitted
/// to deallocate.
#[allow(non_snake_case)]
unsafe fn array<E>(
self,
slot: *mut [u8; 1024 * 1024],
Expand All @@ -113,6 +114,7 @@ const _: () = {
/// - `slot` is a valid pointer to uninitialized memory.
/// - the caller does not touch `slot` when `Err` is returned, they are only permitted
/// to deallocate.
#[allow(non_snake_case)]
unsafe fn r<E>(
self,
slot: *mut &'b mut [&'a mut T; SIZE],
Expand All @@ -135,6 +137,7 @@ const _: () = {
/// - the caller does not touch `slot` when `Err` is returned, they are only permitted
/// to deallocate.
/// - `slot` will not move until it is dropped, i.e. it will be pinned.
#[allow(non_snake_case)]
unsafe fn _pin<E>(
self,
slot: *mut PhantomPinned,
Expand Down Expand Up @@ -179,7 +182,7 @@ const _: () = {
{
type Datee = Foo<'a, 'b, T, SIZE>;
}
#[allow(dead_code)]
#[allow(dead_code, non_snake_case)]
struct __Unpin<'__pin, 'a, 'b: 'a, T: Bar<'b> + ?Sized + 'a, const SIZE: usize = 0>
where
T: Bar<'a, 1>,
Expand Down
6 changes: 4 additions & 2 deletions tests/ui/expand/pin-data.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct Foo {
_pin: PhantomPinned,
}
/// Pin-projections of [`Foo`]
#[allow(dead_code)]
#[allow(dead_code, non_snake_case)]
#[doc(hidden)]
struct FooProjection<'__pin> {
array: &'__pin mut [u8; 1024 * 1024],
Expand Down Expand Up @@ -51,6 +51,7 @@ const _: () = {
/// - `slot` is a valid pointer to uninitialized memory.
/// - the caller does not touch `slot` when `Err` is returned, they are only permitted
/// to deallocate.
#[allow(non_snake_case)]
unsafe fn array<E>(
self,
slot: *mut [u8; 1024 * 1024],
Expand All @@ -73,6 +74,7 @@ const _: () = {
/// - the caller does not touch `slot` when `Err` is returned, they are only permitted
/// to deallocate.
/// - `slot` will not move until it is dropped, i.e. it will be pinned.
#[allow(non_snake_case)]
unsafe fn _pin<E>(
self,
slot: *mut PhantomPinned,
Expand Down Expand Up @@ -101,7 +103,7 @@ const _: () = {
unsafe impl ::pin_init::__internal::PinData for __ThePinData {
type Datee = Foo;
}
#[allow(dead_code)]
#[allow(dead_code, non_snake_case)]
struct __Unpin<'__pin> {
__phantom_pin: ::core::marker::PhantomData<fn(&'__pin ()) -> &'__pin ()>,
__phantom: ::core::marker::PhantomData<fn(Foo) -> Foo>,
Expand Down
6 changes: 4 additions & 2 deletions tests/ui/expand/pinned_drop.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct Foo {
_pin: PhantomPinned,
}
/// Pin-projections of [`Foo`]
#[allow(dead_code)]
#[allow(dead_code, non_snake_case)]
#[doc(hidden)]
struct FooProjection<'__pin> {
array: &'__pin mut [u8; 1024 * 1024],
Expand Down Expand Up @@ -51,6 +51,7 @@ const _: () = {
/// - `slot` is a valid pointer to uninitialized memory.
/// - the caller does not touch `slot` when `Err` is returned, they are only permitted
/// to deallocate.
#[allow(non_snake_case)]
unsafe fn array<E>(
self,
slot: *mut [u8; 1024 * 1024],
Expand All @@ -73,6 +74,7 @@ const _: () = {
/// - the caller does not touch `slot` when `Err` is returned, they are only permitted
/// to deallocate.
/// - `slot` will not move until it is dropped, i.e. it will be pinned.
#[allow(non_snake_case)]
unsafe fn _pin<E>(
self,
slot: *mut PhantomPinned,
Expand Down Expand Up @@ -101,7 +103,7 @@ const _: () = {
unsafe impl ::pin_init::__internal::PinData for __ThePinData {
type Datee = Foo;
}
#[allow(dead_code)]
#[allow(dead_code, non_snake_case)]
struct __Unpin<'__pin> {
__phantom_pin: ::core::marker::PhantomData<fn(&'__pin ()) -> &'__pin ()>,
__phantom: ::core::marker::PhantomData<fn(Foo) -> Foo>,
Expand Down
Loading