rust-lang / rust-lang/rust-clippy

from_over_into auto fix breaks type inference

Open
#14,012 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Trace the from_over_into lint and its suggested fix, then compile the provided From implementation reproducer. Done means accepting the fix no longer produces the reported type-inference error, with regression coverage for this reproducer.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.