Skip to content

Commit d9faf43

Browse files
committed
Switch to anon-const for wrapping derived impls
Fixes a non_local_definitions warning in the derived code. warning: non-local `impl` definition, they should be avoided as they go against expectation --> tests/test_derive.rs:5:28 | 5 | #[derive(PartialEq, Debug, Serialize, Deserialize)] | ^^^^^^^^^ | = help: move this `impl` block outside the of the current constant `_IMPL_MINISERIALIZE_FOR_Tag` = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363> = note: the derive macro `Serialize` may come from an old version of the `mini_internal` crate, try updating your dependency with `cargo update -p mini_internal` = note: `#[warn(non_local_definitions)]` on by default = note: this warning originates in the derive macro `Serialize` (in Nightly builds, run with -Z macro-backtrace for more info)
1 parent 0240792 commit d9faf43

File tree

2 files changed

+6
-22
lines changed

2 files changed

+6
-22
lines changed

derive/src/de.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{attr, bound};
22
use proc_macro2::{Span, TokenStream};
33
use quote::quote;
44
use syn::{
5-
parse_quote, Data, DataEnum, DataStruct, DeriveInput, Error, Fields, FieldsNamed, Ident, Result,
5+
parse_quote, Data, DataEnum, DataStruct, DeriveInput, Error, Fields, FieldsNamed, Result,
66
};
77

88
pub fn derive(input: DeriveInput) -> Result<TokenStream> {
@@ -26,10 +26,6 @@ pub fn derive(input: DeriveInput) -> Result<TokenStream> {
2626
pub fn derive_struct(input: &DeriveInput, fields: &FieldsNamed) -> Result<TokenStream> {
2727
let ident = &input.ident;
2828
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
29-
let dummy = Ident::new(
30-
&format!("_IMPL_MINIDESERIALIZE_FOR_{}", ident),
31-
Span::call_site(),
32-
);
3329

3430
let fieldname = fields.named.iter().map(|f| &f.ident).collect::<Vec<_>>();
3531
let fieldty = fields.named.iter().map(|f| &f.ty);
@@ -46,7 +42,7 @@ pub fn derive_struct(input: &DeriveInput, fields: &FieldsNamed) -> Result<TokenS
4642

4743
Ok(quote! {
4844
#[allow(non_upper_case_globals)]
49-
const #dummy: () = {
45+
const _: () = {
5046
#[repr(C)]
5147
struct __Visitor #impl_generics #where_clause {
5248
__out: miniserde::__private::Option<#ident #ty_generics>,
@@ -117,10 +113,6 @@ pub fn derive_enum(input: &DeriveInput, enumeration: &DataEnum) -> Result<TokenS
117113
}
118114

119115
let ident = &input.ident;
120-
let dummy = Ident::new(
121-
&format!("_IMPL_MINIDESERIALIZE_FOR_{}", ident),
122-
Span::call_site(),
123-
);
124116

125117
let var_idents = enumeration
126118
.variants
@@ -141,7 +133,7 @@ pub fn derive_enum(input: &DeriveInput, enumeration: &DataEnum) -> Result<TokenS
141133

142134
Ok(quote! {
143135
#[allow(non_upper_case_globals)]
144-
const #dummy: () = {
136+
const _: () = {
145137
#[repr(C)]
146138
struct __Visitor {
147139
__out: miniserde::__private::Option<#ident>,

derive/src/ser.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{attr, bound};
22
use proc_macro2::{Span, TokenStream};
33
use quote::quote;
44
use syn::{
5-
parse_quote, Data, DataEnum, DataStruct, DeriveInput, Error, Fields, FieldsNamed, Ident, Result,
5+
parse_quote, Data, DataEnum, DataStruct, DeriveInput, Error, Fields, FieldsNamed, Result,
66
};
77

88
pub fn derive(input: DeriveInput) -> Result<TokenStream> {
@@ -26,10 +26,6 @@ pub fn derive(input: DeriveInput) -> Result<TokenStream> {
2626
fn derive_struct(input: &DeriveInput, fields: &FieldsNamed) -> Result<TokenStream> {
2727
let ident = &input.ident;
2828
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
29-
let dummy = Ident::new(
30-
&format!("_IMPL_MINISERIALIZE_FOR_{}", ident),
31-
Span::call_site(),
32-
);
3329

3430
let fieldname = &fields.named.iter().map(|f| &f.ident).collect::<Vec<_>>();
3531
let fieldstr = fields
@@ -46,7 +42,7 @@ fn derive_struct(input: &DeriveInput, fields: &FieldsNamed) -> Result<TokenStrea
4642

4743
Ok(quote! {
4844
#[allow(non_upper_case_globals)]
49-
const #dummy: () = {
45+
const _: () = {
5046
impl #impl_generics miniserde::Serialize for #ident #ty_generics #bounded_where_clause {
5147
fn begin(&self) -> miniserde::ser::Fragment {
5248
miniserde::ser::Fragment::Map(miniserde::__private::Box::new(__Map {
@@ -89,10 +85,6 @@ fn derive_enum(input: &DeriveInput, enumeration: &DataEnum) -> Result<TokenStrea
8985
}
9086

9187
let ident = &input.ident;
92-
let dummy = Ident::new(
93-
&format!("_IMPL_MINISERIALIZE_FOR_{}", ident),
94-
Span::call_site(),
95-
);
9688

9789
let var_idents = enumeration
9890
.variants
@@ -113,7 +105,7 @@ fn derive_enum(input: &DeriveInput, enumeration: &DataEnum) -> Result<TokenStrea
113105

114106
Ok(quote! {
115107
#[allow(non_upper_case_globals)]
116-
const #dummy: () = {
108+
const _: () = {
117109
impl miniserde::Serialize for #ident {
118110
fn begin(&self) -> miniserde::ser::Fragment {
119111
match self {

0 commit comments

Comments
 (0)