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

Make clippy happy with code emitted by impl_arg_param #2487

Closed
wants to merge 4 commits into from

Conversation

mejrs
Copy link
Member

@mejrs mejrs commented Jun 28, 2022

Fixes some bad error message when Rust can't find a type:

// missing PathBuf import
use pyo3::prelude::*;

#[pyclass]
pub struct S;

#[pymethods]
impl S {
    #[new]
    #[args(path = "None")]
    fn new(path: Option<PathBuf>) -> PyResult<Self> {
        todo!()
    }
}
-- snip --

error[E0282]: type annotations needed for `Option<T>`
 --> src\lib.rs:7:1
  |
7 | #[pymethods]
  | ^^^^^^^^^^^^ consider giving `arg0` the explicit type `Option<T>`, where the type parameter `T` is specified
  |
  = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info)

Some errors have detailed explanations: E0282, E0412.
For more information about an error, try `rustc --explain E0282`.

Fixes rust-lang/rust-clippy#8971

#[pyo3::pyfunction]
pub fn hash_djb2(_s: &str) -> i32 {
    0
}
warning: deref on an immutable reference
 --> src\lib.rs:3:22
  |
3 | pub fn hash_djb2(_s: &str) -> i32 {
  |                      ^^^^ help: if you would like to reborrow, try removing `&*`: `&str`
  |
  = note: `#[warn(clippy::borrow_deref_ref)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrow_deref_ref

warning: `my_module` (lib) generated 1 warning

@mejrs mejrs added the CI-no-fail-fast If one job fails, allow the rest to keep testing label Jun 28, 2022
Ok(quote_arg_span! {
let #arg_name = #arg_value_or_default?;
})
} else {
let ty = arg.ty;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might need a call to remove_lifetime. Can you add a test for a pyfunction with something like Vec<&'py PyAny> as an argument? I've also been wondering if it's possible for us to copy lifetimes from the function definition onto the generated pyfunction - I think that might also solve the problem but not sure.

let #arg_name = #borrow_tmp;
})
} else {
} else if cfg!(feature = "pyproto") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lack of remove_lifetime is probably why pyproto broke - it inserts a hidden lifetime to try and behave like lifetime elision :(. Super weird and so close to killing off now!

@davidhewitt
Copy link
Member

Thanks! I've paused on reviewing this one for a bit just because I've always viewed the deref juggling the macro does here as a bit janky. I finally had an idea how to introduce a PyFunctionArgument trait which should allow the macros to do less (relying on type inference instead).

I haven't quite finished off that branch, I'm hoping I'll get a chance to implement at the weekend and see what folks think.

@davidhewitt
Copy link
Member

Closing as superseded by #2503

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI-no-fail-fast If one job fails, allow the rest to keep testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't ignore clippy::borrow_deref_ref
2 participants