Skip to content

Diagnostics Improvement: Same Body in Branches #79210

Open
@jamesmunns

Description

@jamesmunns

I tried this code:

#[derive(PartialEq, Eq, Debug)]
struct SortedEdge {
    a: usize,
    b: usize,
}

impl SortedEdge {
    fn new(a: usize, b: usize) -> SortedEdge {
        if b > a {
            SortedEdge { a, b }
        } else {
            // uh oh!
            // We really want:
            //   SortedEdge { a: b, b: a }
            SortedEdge { b, a }
        }
    }
}

fn main() {
    let x = SortedEdge::new(1, 2);
    let y = SortedEdge::new(2, 1);
    
    // The points should be equal after sorting indicies
    assert_eq!(x, y);
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=cc7599a28cfe7dd105f3f4d609c47b47

I expected to see this happen:

A compiler warning/diagnostic that both of my branches were equivalent.

Instead, this happened:

No warning was emitted

Meta

This happens on Rust 1.47.0 stable and Rust nightly 2020-11-18

CC @estebank, and the related discussion on twitter

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-control-flowArea: Control flowA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions