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

False Positive of unnecessary_fallible_conversions for pyo3 #12039

Closed
Xuanwo opened this issue Dec 29, 2023 · 8 comments
Closed

False Positive of unnecessary_fallible_conversions for pyo3 #12039

Xuanwo opened this issue Dec 29, 2023 · 8 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@Xuanwo
Copy link

Xuanwo commented Dec 29, 2023

Summary

rust-clippy with 1.75 could emit false positive of unnecessary_fallible_conversions for pyo3:

warning: use of a fallible conversion when an infallible one could be used
   --> bindings/python/src/file.rs:180:27
    |
180 |     pub fn __enter__(slf: Py<Self>) -> Py<Self> {
    |                           ^^ help: use: `From::from`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_fallible_conversions
    = note: `#[warn(clippy::unnecessary_fallible_conversions)]` on by default

warning: `opendal-python` (lib test) generated 1 warning
warning: `opendal-python` (lib) generated 1 warning (1 duplicate)

It's bit confused to raising unnecessary_fallible_conversions on API like this:

pub fn __enter__(slf: Py<Self>) -> Py<Self> {
    slf
}

Py is a transparent struct expose by pyo3: https://docs.rs/pyo3/latest/pyo3/struct.Py.html

Lint Name

unnecessary_fallible_conversions

Reproducer

I tried this code:

pub fn __enter__(slf: Py<Self>) -> Py<Self> {
    slf
}

I saw this happen:

warning: use of a fallible conversion when an infallible one could be used
   --> bindings/python/src/file.rs:180:27
    |
180 |     pub fn __enter__(slf: Py<Self>) -> Py<Self> {
    |                           ^^ help: use: `From::from`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_fallible_conversions
    = note: `#[warn(clippy::unnecessary_fallible_conversions)]` on by default

warning: `opendal-python` (lib test) generated 1 warning
warning: `opendal-python` (lib) generated 1 warning (1 duplicate)

I expected to see this happen: no warning

Version

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-gnu
release: 1.75.0
LLVM version: 17.0.6

Additional Labels

No response

@Xuanwo Xuanwo added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 29, 2023
@djc
Copy link
Contributor

djc commented Dec 29, 2023

I'm also seeing this. I guess this happens within the #[pymethods] expansion, and this lint isn't correcting for macro-generated code?

Also, notably, you can't just stick the allow on the function definition or even on the impl block, it apparently has to go the module level to be applied correctly.

@y21
Copy link
Member

y21 commented Dec 29, 2023

That's odd, all method lints already have a check if they're from a macro:

impl<'tcx> LateLintPass<'tcx> for Methods {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
if expr.span.from_expansion() {
return;
}

I guess #[pymethods] uses the span of the callsite, instead of definition site, which makes clippy think that this is something that the user wrote 🤔

@djc
Copy link
Contributor

djc commented Dec 29, 2023

@davidhewitt is there something on the PyO3 side that could be improved here?

@davidhewitt
Copy link

We've already "fixed" this (with an allow in the generated output) on PyO3 main. I'd intended to release 0.21 with this before 1.75 landed but we got caught up in some larger work which has delayed the release and I forgot about the time-sensitive nature of this. Let me look into a patch release for 0.20 asap, as 0.21 is realistically still a couple weeks off at best.

@davidhewitt
Copy link

PyO3/pyo3#3564

@djc
Copy link
Contributor

djc commented Dec 29, 2023

@davidhewitt awesome as always, thanks!

@davidhewitt
Copy link

Patch release is out, @Xuanwo please let me know if there's still an issue after upgrading.

@Xuanwo
Copy link
Author

Xuanwo commented Dec 31, 2023

Patch release is out, @Xuanwo please let me know if there's still an issue after upgrading.

Great! The patch resolved my issue.

@Xuanwo Xuanwo closed this as completed Dec 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

4 participants