Skip to content

Commit

Permalink
Avoid clippy::useless_conversion lint in macros
Browse files Browse the repository at this point in the history
Using `#[allow(clippy::useless_conversion)]` still causes failures if
the PyO3 user uses `#[forbid(clippy::useless_conversion)]`. Instead we
can ensure the `into` call is not within a `quote_spanned!` block,
allowing clippy to know this is macro code.

rust-lang/rust-clippy#14272 (comment)
  • Loading branch information
LilyFoote committed Feb 27, 2025
1 parent fe2d7f8 commit 2788c9a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
18 changes: 6 additions & 12 deletions pyo3-macros-backend/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,26 +265,20 @@ impl FnType {
let slf: Ident = syn::Ident::new("_slf", Span::call_site());
let pyo3_path = pyo3_path.to_tokens_spanned(*span);
let ret = quote_spanned! { *span =>
#[allow(clippy::useless_conversion)]
::std::convert::Into::into(
#pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &*(&#slf as *const _ as *const *mut _))
.downcast_unchecked::<#pyo3_path::types::PyType>()
)
#pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &*(&#slf as *const _ as *const *mut _))
.downcast_unchecked::<#pyo3_path::types::PyType>()
};
Some(quote! { unsafe { #ret }, })
Some(quote! { unsafe { ::std::convert::Into::into(#ret) }, })
}
FnType::FnModule(span) => {
let py = syn::Ident::new("py", Span::call_site());
let slf: Ident = syn::Ident::new("_slf", Span::call_site());
let pyo3_path = pyo3_path.to_tokens_spanned(*span);
let ret = quote_spanned! { *span =>
#[allow(clippy::useless_conversion)]
::std::convert::Into::into(
#pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &*(&#slf as *const _ as *const *mut _))
.downcast_unchecked::<#pyo3_path::types::PyModule>()
)
#pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &*(&#slf as *const _ as *const *mut _))
.downcast_unchecked::<#pyo3_path::types::PyModule>()
};
Some(quote! { unsafe { #ret }, })
Some(quote! { unsafe { ::std::convert::Into::into(#ret) }, })
}
FnType::FnNew | FnType::FnStatic | FnType::ClassAttribute => None,
}
Expand Down
10 changes: 6 additions & 4 deletions pyo3-macros-backend/src/quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ pub(crate) fn ok_wrap(obj: TokenStream, ctx: &Ctx) -> TokenStream {
output_span,
} = ctx;
let pyo3_path = pyo3_path.to_tokens_spanned(*output_span);
quote_spanned! { *output_span => {
let converter = quote_spanned! { *output_span => {
let obj = #obj;
#[allow(clippy::useless_conversion)]
#pyo3_path::impl_::wrap::converter(&obj).wrap(obj).map_err(::core::convert::Into::<#pyo3_path::PyErr>::into)
}}
#pyo3_path::impl_::wrap::converter(&obj).wrap(obj)
}};
quote! {
#converter.map_err(::core::convert::Into::<#pyo3_path::PyErr>::into)
}
}

pub(crate) fn map_result_into_ptr(result: TokenStream, ctx: &Ctx) -> TokenStream {
Expand Down

0 comments on commit 2788c9a

Please sign in to comment.