diff --git a/axum-macros/src/from_ref.rs b/axum-macros/src/from_ref.rs index 72b9d440..89d3bac0 100644 --- a/axum-macros/src/from_ref.rs +++ b/axum-macros/src/from_ref.rs @@ -3,7 +3,7 @@ use quote::quote_spanned; use syn::{ parse::{Parse, ParseStream}, spanned::Spanned, - Field, ItemStruct, Token, + Field, ItemStruct, Token, Type, }; use crate::attr_parsing::{combine_unary_attribute, parse_attrs, Combine}; @@ -30,7 +30,11 @@ fn expand_field(state: &Ident, idx: usize, field: &Field) -> TokenStream { let span = field.ty.span(); let body = if let Some(field_ident) = &field.ident { - quote_spanned! {span=> state.#field_ident.clone() } + if matches!(field_ty, Type::Reference(_)) { + quote_spanned! {span=> state.#field_ident } + } else { + quote_spanned! {span=> state.#field_ident.clone() } + } } else { let idx = syn::Index { index: idx as _, diff --git a/axum-macros/tests/from_ref/pass/reference-types.rs b/axum-macros/tests/from_ref/pass/reference-types.rs new file mode 100644 index 00000000..d5d1d837 --- /dev/null +++ b/axum-macros/tests/from_ref/pass/reference-types.rs @@ -0,0 +1,10 @@ +#![deny(noop_method_call)] + +use axum_macros::FromRef; + +#[derive(FromRef)] +struct State { + inner: &'static str, +} + +fn main() {}