Skip to content

init: allow nonstandard_style for generated accessor/value#127

Open
Mirko-A wants to merge 6 commits intoRust-for-Linux:mainfrom
Mirko-A:mirko/allow-nonstandard-style-accessor
Open

init: allow nonstandard_style for generated accessor/value#127
Mirko-A wants to merge 6 commits intoRust-for-Linux:mainfrom
Mirko-A:mirko/allow-nonstandard-style-accessor

Conversation

@Mirko-A
Copy link
Copy Markdown

@Mirko-A Mirko-A commented Apr 1, 2026

Allows nonstandard_style lint on accessors/values generated as local variables in init!.

Since the same warning will be reported by the compiler on the struct field, having the extra warning for the generated accessor/value is unnecessary and confusing.

Reported-by: Gary Guo gary@garyguo.net
Link: #125
Closes: https://lore.kernel.org/rust-for-linux/DGTBJBIVFZ2K.2F1ZEFGY0G7NK@garyguo.net/
Fixes: f1b0c3c ("internal: init: remove #[disable_initialized_field_access]")

Allows `nonstandard_style` lint on accessors/values generated as local
variables in `init!`.

Since the same warning will be reported by the compiler on the struct
field, having the extra warning for the generated accessor/value is
unnecessary and confusing.

Reported-by: Gary Guo <gary@garyguo.net>
Link: Rust-for-Linux#125
Closes: https://lore.kernel.org/rust-for-linux/DGTBJBIVFZ2K.2F1ZEFGY0G7NK@garyguo.net/
Fixes: f1b0c3c ("internal: init: remove #[disable_initialized_field_access]")
Signed-off-by: Mirko Adzic <adzicmirko97@gmail.com>
@Mirko-A
Copy link
Copy Markdown
Author

Mirko-A commented Apr 1, 2026

Tested with the following changes:

diff --git a/examples/big_struct_in_place.rs b/examples/big_struct_in_place.rs
index 80f89b5..5525cae 100644
--- a/examples/big_struct_in_place.rs
+++ b/examples/big_struct_in_place.rs
@@ -13,8 +13,8 @@ pub struct BigStruct {
     a: u64,
     b: u64,
     c: u64,
-    d: u64,
-    managed_buf: ManagedBuf,
+    NONSTANDARD_D: u64,
+    MANAGED_BUF: ManagedBuf,
 }

 #[derive(Debug)]
@@ -37,8 +37,8 @@ fn main() {
             a: 7,
             b: 186,
             c: 7789,
-            d: 34,
-            managed_buf <- ManagedBuf::new(),
+            NONSTANDARD_D: 34,
+            MANAGED_BUF <- ManagedBuf::new(),
         }))
         .unwrap();
         println!("{}", core::mem::size_of_val(&*buf));

Output before the patch:

$ cargo run --example big_struct_in_place
[...]
error: structure field `NONSTANDARD_D` should have a snake case name
  --> examples/big_struct_in_place.rs:16:5
   |
16 |     NONSTANDARD_D: u64,
   |     ^^^^^^^^^^^^^ help: convert the identifier to snake case: `nonstandard_d`
   |
   = note: `-D non-snake-case` implied by `-D warnings`
   = help: to override `-D warnings` add `#[allow(non_snake_case)]`

error: structure field `MANAGED_BUF` should have a snake case name
  --> examples/big_struct_in_place.rs:17:5
   |
17 |     MANAGED_BUF: ManagedBuf,
   |     ^^^^^^^^^^^ help: convert the identifier to snake case: `managed_buf`

error: variable `NONSTANDARD_D` should have a snake case name
  --> examples/big_struct_in_place.rs:40:28
   |
40 |             NONSTANDARD_D: 34,
   |                            ^^ help: convert the identifier to snake case: `nonstandard_d`

error: variable `NONSTANDARD_D` should have a snake case name
  --> examples/big_struct_in_place.rs:40:13
   |
40 |             NONSTANDARD_D: 34,
   |             ^^^^^^^^^^^^^ help: convert the identifier to snake case: `nonstandard_d`

error: variable `MANAGED_BUF` should have a snake case name
  --> examples/big_struct_in_place.rs:41:13
   |
41 |             MANAGED_BUF <- ManagedBuf::new(),
   |             ^^^^^^^^^^^ help: convert the identifier to snake case: `managed_buf`

error: could not compile `pin-init` (example "big_struct_in_place") due to 5 previous errors

Output after the patch:

$ cargo run --example big_struct_in_place
[...]
error: structure field `NONSTANDARD_D` should have a snake case name
  --> examples/big_struct_in_place.rs:16:5
   |
16 |     NONSTANDARD_D: u64,
   |     ^^^^^^^^^^^^^ help: convert the identifier to snake case: `nonstandard_d`
   |
   = note: `-D non-snake-case` implied by `-D warnings`
   = help: to override `-D warnings` add `#[allow(non_snake_case)]`

error: structure field `MANAGED_BUF` should have a snake case name
  --> examples/big_struct_in_place.rs:17:5
   |
17 |     MANAGED_BUF: ManagedBuf,
   |     ^^^^^^^^^^^ help: convert the identifier to snake case: `managed_buf`

error: could not compile `pin-init` (example "big_struct_in_place") due to 2 previous errors

I am still quite new to the process of contributing to Rust-For-Linux. Please let me know if I've made any mistakes or if I've missed something.

@nbdd0121
Copy link
Copy Markdown
Member

nbdd0121 commented Apr 1, 2026

Please add a test for this

@Mirko-A
Copy link
Copy Markdown
Author

Mirko-A commented Apr 1, 2026

Does something like this look okay?

@nbdd0121
Copy link
Copy Markdown
Member

nbdd0121 commented Apr 3, 2026

I think what you want is not a compile_fail test, but rather than the code (with allow on the struct definition side), compiles to completion without warnings? So this should just be a test in src/tests.

@Mirko-A Mirko-A force-pushed the mirko/allow-nonstandard-style-accessor branch 2 times, most recently from e90104e to 2d194be Compare April 4, 2026 09:16
@Mirko-A
Copy link
Copy Markdown
Author

Mirko-A commented Apr 4, 2026

Right, that sounds like a better approach.

I've amended the existing test commit to keep the branch clean, hopefully that's okay: 2d194be

So this should just be a test in src/tests.

I put the test under the existing tests directory in the crate root, I hope that's what you meant. If you specifically wanted a new directory in src, I'm happy to move it.

@Mirko-A
Copy link
Copy Markdown
Author

Mirko-A commented Apr 4, 2026

Two more things:

  1. The CI failure seems unrelated, running rustup run 1.78 cargo check on main produces the same error for me.
  2. Would you like me to add a changelog entry for this fix?

@nbdd0121
Copy link
Copy Markdown
Member

nbdd0121 commented Apr 4, 2026

main works fine for me. Did you not add RUSTC_BOOTSTRAP=1? Please do add a changelog entry.

@Mirko-A Mirko-A force-pushed the mirko/allow-nonstandard-style-accessor branch from e65a45e to c7a56c5 Compare April 4, 2026 15:20
Adds a test to make sure that no excess warnings are emitted when
dealing with non-standard field names in `init!`.

Signed-off-by: Mirko Adzic <adzicmirko97@gmail.com>
@Mirko-A Mirko-A force-pushed the mirko/allow-nonstandard-style-accessor branch from c7a56c5 to f7e5864 Compare April 4, 2026 15:35
Signed-off-by: Mirko Adzic <adzicmirko97@gmail.com>
@Mirko-A Mirko-A force-pushed the mirko/allow-nonstandard-style-accessor branch from f7e5864 to d4e1179 Compare April 6, 2026 12:01
Mirko-A added 3 commits April 8, 2026 12:33
Allows `nonstandard_style` lint on locally generated identifiers in
`pin_init!`.

Since the same warning will be reported by the compiler on the struct
definition, having the extra warning emitted at the macro call site is
unnecessary and confusing.

Suggested-by: Gary Guo <gary@garyguo.net>
Link: Rust-for-Linux#125
Signed-off-by: Mirko Adzic <adzicmirko97@gmail.com>
Expands the test to also cover usage of `pin_init!` (instead of only
`init!`) on structs with non-standard style field names.

Suggested-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Mirko Adzic <adzicmirko97@gmail.com>
Signed-off-by: Mirko Adzic <adzicmirko97@gmail.com>
@Mirko-A Mirko-A force-pushed the mirko/allow-nonstandard-style-accessor branch from 61c7294 to 7a0d2da Compare April 8, 2026 10:34
@Mirko-A
Copy link
Copy Markdown
Author

Mirko-A commented Apr 8, 2026

Sorry for the delay - I just pushed a few more commits. Hopefully that covers everything now.

I kept the new commits separate, so that it is easier to see the diffs. Please let me know if you want them squashed.

Copy link
Copy Markdown
Member

@nbdd0121 nbdd0121 left a comment

Choose a reason for hiding this comment

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

Please squash the commits. I think for this you can have three commits:

  • one for #[pin_data]
  • one for init!/pin_init!
  • one for the test

Also fix tag is wrong, it should be db96c51

//!
//! See: https://github.com/Rust-for-Linux/pin-init/issues/125

// Should be implied by crate lint settings, but just to be sure:
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.

This sentence doesn't need to be here; this is an integral part of the test.

Comment on lines +41 to +43
- `init!` and `pin_init!` no longer produce `nonstandard_style` group warnings at the
call site when used for structs with non-snake-case field names. Warnings on the
struct definition are unaffected.
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.

Suggested change
- `init!` and `pin_init!` no longer produce `nonstandard_style` group warnings at the
call site when used for structs with non-snake-case field names. Warnings on the
struct definition 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 definitions are unaffected.

actually, could you update code to use the specific lint, such as non_snake_case for suppression?

Also, could you squash the changelog update into the same commit that update the code please.

You'd also probably want a line about #[pin_data], to mention that lints can now be properly suppressed where it couldn't previously.

quote! {
// Allow `nonstandard_style` since the same warning is going to be reported for
// the struct field.
#[allow(nonstandard_style)]
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.

Hmm, actually this can cause lints to be suppressed on #value, which is provided by the user.

Code like

init!(foo: {
    let Foo = 1;
    1
})

will be incorrectly suppressed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants