Skip to content

Commit 2788c9a

Browse files
committed
Avoid clippy::useless_conversion lint in macros
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)
1 parent fe2d7f8 commit 2788c9a

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

pyo3-macros-backend/src/method.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -265,26 +265,20 @@ impl FnType {
265265
let slf: Ident = syn::Ident::new("_slf", Span::call_site());
266266
let pyo3_path = pyo3_path.to_tokens_spanned(*span);
267267
let ret = quote_spanned! { *span =>
268-
#[allow(clippy::useless_conversion)]
269-
::std::convert::Into::into(
270-
#pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &*(&#slf as *const _ as *const *mut _))
271-
.downcast_unchecked::<#pyo3_path::types::PyType>()
272-
)
268+
#pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &*(&#slf as *const _ as *const *mut _))
269+
.downcast_unchecked::<#pyo3_path::types::PyType>()
273270
};
274-
Some(quote! { unsafe { #ret }, })
271+
Some(quote! { unsafe { ::std::convert::Into::into(#ret) }, })
275272
}
276273
FnType::FnModule(span) => {
277274
let py = syn::Ident::new("py", Span::call_site());
278275
let slf: Ident = syn::Ident::new("_slf", Span::call_site());
279276
let pyo3_path = pyo3_path.to_tokens_spanned(*span);
280277
let ret = quote_spanned! { *span =>
281-
#[allow(clippy::useless_conversion)]
282-
::std::convert::Into::into(
283-
#pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &*(&#slf as *const _ as *const *mut _))
284-
.downcast_unchecked::<#pyo3_path::types::PyModule>()
285-
)
278+
#pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(#py, &*(&#slf as *const _ as *const *mut _))
279+
.downcast_unchecked::<#pyo3_path::types::PyModule>()
286280
};
287-
Some(quote! { unsafe { #ret }, })
281+
Some(quote! { unsafe { ::std::convert::Into::into(#ret) }, })
288282
}
289283
FnType::FnNew | FnType::FnStatic | FnType::ClassAttribute => None,
290284
}

pyo3-macros-backend/src/quotes.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ pub(crate) fn ok_wrap(obj: TokenStream, ctx: &Ctx) -> TokenStream {
1515
output_span,
1616
} = ctx;
1717
let pyo3_path = pyo3_path.to_tokens_spanned(*output_span);
18-
quote_spanned! { *output_span => {
18+
let converter = quote_spanned! { *output_span => {
1919
let obj = #obj;
20-
#[allow(clippy::useless_conversion)]
21-
#pyo3_path::impl_::wrap::converter(&obj).wrap(obj).map_err(::core::convert::Into::<#pyo3_path::PyErr>::into)
22-
}}
20+
#pyo3_path::impl_::wrap::converter(&obj).wrap(obj)
21+
}};
22+
quote! {
23+
#converter.map_err(::core::convert::Into::<#pyo3_path::PyErr>::into)
24+
}
2325
}
2426

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

0 commit comments

Comments
 (0)