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

Provide structured suggestion to use trait objects in some cases of if arm type divergence #120261

Merged
merged 3 commits into from
Jan 24, 2024

Commits on Jan 22, 2024

  1. Configuration menu
    Copy the full SHA
    390ef9b View commit details
    Browse the repository at this point in the history
  2. Suggest boxing if then expr if that solves divergent arms

    When encountering
    
    ```rust
    let _ = if true {
        Struct
    } else {
        foo() // -> Box<dyn Trait>
    };
    ```
    
    if `Struct` implements `Trait`, suggest boxing the then arm tail expression.
    
    Part of rust-lang#102629.
    estebank committed Jan 22, 2024
    Configuration menu
    Copy the full SHA
    ac56a2b View commit details
    Browse the repository at this point in the history

Commits on Jan 23, 2024

  1. Suggest boxing both arms of if expr if that solves divergent arms inv…

    …olving `impl Trait`
    
    When encountering the following
    
    ```rust
    // run-rustfix
    trait Trait {}
    struct Struct;
    impl Trait for Struct {}
    fn foo() -> Box<dyn Trait> {
        Box::new(Struct)
    }
    fn bar() -> impl Trait {
        Struct
    }
    fn main() {
        let _ = if true {
            Struct
        } else {
            foo() //~ ERROR E0308
        };
        let _ = if true {
            foo()
        } else {
            Struct //~ ERROR E0308
        };
        let _ = if true {
            Struct
        } else {
            bar() // impl Trait
        };
        let _ = if true {
            bar() // impl Trait
        } else {
            Struct
        };
    }
    ```
    
    suggest boxing both arms
    
    ```rust
        let _ = if true {
            Box::new(Struct) as Box<dyn Trait>
        } else {
            Box::new(bar())
        };
        let _ = if true {
            Box::new(bar()) as Box<dyn Trait>
        } else {
            Box::new(Struct)
        };
    ```
    estebank committed Jan 23, 2024
    Configuration menu
    Copy the full SHA
    34f4f3d View commit details
    Browse the repository at this point in the history