Skip to content

Avoid clippy::useless_conversion lint in macros #4944

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/4944.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes unintentional `clippy::useless_conversion` trigger by adjusting macro hygiene.
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
4 changes: 2 additions & 2 deletions pyo3-macros-backend/src/quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ pub(crate) fn ok_wrap(obj: TokenStream, ctx: &Ctx) -> TokenStream {
output_span,
} = ctx;
let pyo3_path = pyo3_path.to_tokens_spanned(*output_span);
let into = quote!(::core::convert::Into::<#pyo3_path::PyErr>::into);
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).map_err(#into)
}}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/ui/invalid_pyfunctions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ error: expected `&PyModule` or `Py<PyModule>` as first argument with `pass_modul
error[E0277]: the trait bound `&str: From<BoundRef<'_, '_, pyo3::types::PyModule>>` is not satisfied
--> tests/ui/invalid_pyfunctions.rs:33:14
|
31 | #[pyfunction(pass_module)]
| -------------------------- required by a bound introduced by this call
32 | fn first_argument_not_module<'a, 'py>(
33 | _string: &str,
| ^ the trait `From<BoundRef<'_, '_, pyo3::types::PyModule>>` is not implemented for `&str`
|
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/invalid_pymethods.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ error: macros cannot be used as items in `#[pymethods]` impl blocks
error[E0277]: the trait bound `i32: From<BoundRef<'_, '_, PyType>>` is not satisfied
--> tests/ui/invalid_pymethods.rs:46:45
|
43 | #[pymethods]
| ------------ required by a bound introduced by this call
...
46 | fn classmethod_wrong_first_argument(_x: i32) -> Self {
| ^^^ the trait `From<BoundRef<'_, '_, PyType>>` is not implemented for `i32`
|
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/invalid_result_conversion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,22 @@ error[E0277]: the trait bound `PyErr: From<MyError>` is not satisfied
`PyErr` implements `From<IntoInnerError<W>>`
and $N others
= note: required for `MyError` to implement `Into<PyErr>`

error[E0277]: the trait bound `PyErr: From<MyError>` is not satisfied
--> tests/ui/invalid_result_conversion.rs:21:1
|
21 | #[pyfunction]
| ^^^^^^^^^^^^^ the trait `From<MyError>` is not implemented for `PyErr`
|
= help: the following other types implement trait `From<T>`:
`PyErr` implements `From<AddrParseError>`
`PyErr` implements `From<DecodeUtf16Error>`
`PyErr` implements `From<DowncastError<'_, '_>>`
`PyErr` implements `From<DowncastIntoError<'_>>`
`PyErr` implements `From<FromUtf16Error>`
`PyErr` implements `From<FromUtf8Error>`
`PyErr` implements `From<Infallible>`
`PyErr` implements `From<IntoInnerError<W>>`
and $N others
= note: required for `MyError` to implement `Into<PyErr>`
= note: this error originates in the attribute macro `pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)
Loading