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

Diagnostics Improvement: Same Body in Branches #79210

Open
jamesmunns opened this issue Nov 19, 2020 · 2 comments
Open

Diagnostics Improvement: Same Body in Branches #79210

jamesmunns opened this issue Nov 19, 2020 · 2 comments
Labels
A-control-flow Area: Control flow A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@jamesmunns
Copy link
Member

jamesmunns commented Nov 19, 2020

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

@jamesmunns jamesmunns added the C-bug Category: This is a bug. label Nov 19, 2020
@jonas-schievink jonas-schievink added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels Nov 19, 2020
@matthiaskrgr
Copy link
Member

Clippy has a lint that is supposed to detect this https://rust-lang.github.io/rust-clippy/master/index.html#if_same_then_else but it does not catch this example (false negative I guess..)

@Y-Nak
Copy link
Contributor

Y-Nak commented Feb 22, 2021

The false-negative @matthiaskrgr pointed was indirectly solved in this PR.

@Dylan-DPC Dylan-DPC added C-bug Category: This is a bug. and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Jan 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-control-flow Area: Control flow A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants