Skip to content

Using into() in the From trait without having implemented Into should generate a compilation error. #11150

Closed
@ThibaultEbroussard

Description

@ThibaultEbroussard

Code

fn main() {
    #[derive(Debug)]
    struct Number(f32);

    impl From<f32> for Number {
        fn from(value: f32) -> Self {
            value.into()
        }
    }

    let value: Number = (10.0).into();
    println!("{value:?}");
}

Current output

Compiling number v0.1.0 (/home/number)
    Finished dev [unoptimized + debuginfo] target(s) in 0.30s
     Running `target/debug/number`

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
[1]    25988 IOT instruction (core dumped)  cargo run

Desired output

The compiler should detect that we are calling Into for f32 trait which is automatically implemented by From for Number trait in our implementation of the From for Number trait.

Rationale and extra context

No response

Other cases

No response

Anything else?

No response

Activity

riking

riking commented on Jul 12, 2023

@riking

Implementation would be a deny-by-default lint. Not sure whether to recommend Clippy first or straight into rustc.

Noratrieb

Noratrieb commented on Jul 13, 2023

@Noratrieb
Member

I agree that this should live in clippy instead of rustc, @matthiaskrgr do you want to move the issue :)?

y21

y21 commented on Sep 6, 2023

@y21
Member

I think a more general version of this is #11032. The blanket Into impl just has U::from(self) in its implementation, so this is basically the same idea: from -> into -> from -> ...

added a commit that references this issue on Mar 13, 2024

Auto merge of #12459 - y21:unconditional_recursion_from_into, r=Jarcho

73be486
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @riking@y21@Noratrieb@ThibaultEbroussard

      Issue actions

        Using into() in the From trait without having implemented Into should generate a compilation error. · Issue #11150 · rust-lang/rust-clippy