Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warnings for cloning references in generated code #1676

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions axum-macros/src/from_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 _,
Expand Down
10 changes: 10 additions & 0 deletions axum-macros/tests/from_ref/pass/reference-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![deny(noop_method_call)]

use axum_macros::FromRef;

#[derive(FromRef)]
struct State {
inner: &'static str,
}

fn main() {}