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

from_over_into auto fix breaks type inference #14012

Open
zaneduffield opened this issue Jan 17, 2025 · 0 comments
Open

from_over_into auto fix breaks type inference #14012

zaneduffield opened this issue Jan 17, 2025 · 0 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

@zaneduffield
Copy link

Summary

Accepting the fix to convert impl Into to impl From causes a type inference error in the following example

struct WrapErr(String);

impl Into<WrapErr> for &str {
    fn into(self) -> WrapErr {
        todo!()
    }
}

// impl From<&str> for WrapErr {
//     fn from(val: &str) -> Self {
//         todo!()
//     }
// }

fn foo() -> Result<(), WrapErr> {
    None.ok_or("".into())?;
    Ok(())
}

I'm not 100% sure whether this is a clippy bug or a rustc bug.

Lint Name

from_over_into

Reproducer

I tried this code (as generated by the fix):

struct WrapErr(String);

impl From<&str> for WrapErr {
    fn from(val: &str) -> Self {
        todo!()
    }
}

fn foo() -> Result<(), WrapErr> {
    None.ok_or("".into())?;
    Ok(())
}

I saw this happen:

playground

error[E0283]: type annotations needed
  --> src/lib.rs:10:10
   |
10 |     None.ok_or("".into())?;
   |          ^^^^^    ---- type must be known at this point
   |          |
   |          cannot infer type of the type parameter `E` declared on the method `ok_or`
   |
   = note: cannot satisfy `_: From<&str>`
   = note: required for `&str` to implement `Into<_>`
help: consider specifying the generic argument
   |
10 |     None.ok_or::<E>("".into())?;
   |               +++++

error[E0283]: type annotations needed
  --> src/lib.rs:10:10
   |
10 |     None.ok_or("".into())?;
   |          ^^^^^           - type must be known at this point
   |          |
   |          cannot infer type of the type parameter `E` declared on the method `ok_or`
   |
note: multiple `impl`s satisfying `WrapErr: From<_>` found
  --> src/lib.rs:3:1
   |
3  | impl From<&str> for WrapErr {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: and another `impl` found in the `core` crate: `impl<T> From<T> for T;`
   = note: required for `Result<(), WrapErr>` to implement `FromResidual<Result<Infallible, _>>`
help: consider specifying the generic argument
   |
10 |     None.ok_or::<E>("".into())?;
   |               +++++

I expected to see this happen:
compile without errors

Version

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-pc-windows-msvc
release: 1.83.0
LLVM version: 19.1.1

Additional Labels

No response

@zaneduffield zaneduffield 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 Jan 17, 2025
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

1 participant