Skip to content

Commit 3cde185

Browse files
Mirko-Anbdd0121
authored andcommitted
internal: suppress non_snake_case lint in [pin_]init!
Changes how `[pin_]init!` handles `InitializerKind::Value` to avoid creating a local variable that can cause `non_snake_case` lint. Allows `non_snake_case` lint on local variables generated elsewhere in `[pin_]init!`. Since the same warning will be reported by the compiler on the struct definition, having the extra warning for the generated local variables 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: db96c51 ("add references to previously initialized fields") Co-developed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Gary Guo <gary@garyguo.net> Signed-off-by: Mirko Adzic <adzicmirko97@gmail.com>
1 parent 8ebc4e8 commit 3cde185

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4141
check by `#[pin_data]`.
4242
- `#[pin_data]` no longer produces additional `non_snake_case` warnings if field names
4343
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.
4446

4547
## [0.0.10] - 2025-08-19
4648

internal/src/init.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,26 @@ fn init_fields(
240240
};
241241
let init = match kind {
242242
InitializerKind::Value { ident, value } => {
243-
let mut value_ident = ident.clone();
244-
let value_prep = value.as_ref().map(|value| &value.1).map(|value| {
245-
// Setting the span of `value_ident` to `value`'s span improves error messages
246-
// when the type of `value` is wrong.
247-
value_ident.set_span(value.span());
248-
quote!(let #value_ident = #value;)
249-
});
243+
let (value_prep, value_ident) = match value.as_ref() {
244+
None => {
245+
// This is of short-hand syntax, so just use the identifier directly.
246+
(None, ident.clone())
247+
}
248+
Some((_, value)) => {
249+
// We have an expression. Evaluate it to a local variable first, outside
250+
// `unsafe {}` block.
251+
//
252+
// Setting the span of `value_ident` to `value`'s span improves error messages
253+
// when the type of `value` is wrong.
254+
let value_ident = format_ident!("value", span = value.span());
255+
(
256+
Some(quote! {
257+
let #value_ident = #value;
258+
}),
259+
value_ident,
260+
)
261+
}
262+
};
250263
// Again span for better diagnostics
251264
let write = quote_spanned!(ident.span()=> ::core::ptr::write);
252265
quote! {
@@ -333,7 +346,9 @@ fn init_fields(
333346
};
334347

335348
#(#cfgs)*
336-
#[allow(unused_variables)]
349+
// Allow `non_snake_case` since the same warning is going to be reported for
350+
// the struct field.
351+
#[allow(unused_variables, non_snake_case)]
337352
let #ident = #accessor;
338353
});
339354
guards.push(guard);

0 commit comments

Comments
 (0)