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

False positive on match_same_arms #860

Closed
llogiq opened this issue Apr 16, 2016 · 8 comments · Fixed by #8232
Closed

False positive on match_same_arms #860

llogiq opened this issue Apr 16, 2016 · 8 comments · Fixed by #8232
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have T-middle Type: Probably requires verifiying types

Comments

@llogiq
Copy link
Contributor

llogiq commented Apr 16, 2016

Example: Any kind of xor-like handling, e.g. let Foo be one variant of an enum:

match (x, y) {
    (Foo(_), Foo(_)) => None,
    (Foo(x), _) |
    (_, Foo(x)) => Some(x),
    _ => None
}

This will trigger the lint, because the position of the arms is not taken into account.

@regexident suggested to only lint adjacent same arms. Another option would be to check if those arms are disjunct and can be reordered without changing semantics, but that is obviously much much harder to do in the general case.

@Boddlnagg
Copy link

This lint also complained about this match, what I would consider to be a false positive:

match *self {
    SendError::InvalidData(msg) => msg,
    SendError::Other(msg) => msg
}

@mcarton
Copy link
Member

mcarton commented Jun 27, 2016

Why?
This can be written

match *self {
    SendError::InvalidData(msg) | SendError::Other(msg) => msg
}

@Boddlnagg
Copy link

Oh, right. I've never used | before, so I totally didn't think about it that way. Maybe the documentation could mention it, in case there are other people out there who don't think about this ;-)

@mcarton
Copy link
Member

mcarton commented Jun 27, 2016

@Boddlnagg
Copy link

Now this is really embarrassing, sorry. I wanted to report another clippy issue right now, but I'm going to sleep on it first.

@Manishearth
Copy link
Member

Eh, report away, false positives aren't a big deal.

And in this case we should be suggesting the |. Documentation is fine for extended things, but suggestions should be prominent 😄

@Manishearth
Copy link
Member

(when I say false positives I mean false positives when filing bugs, not clippy false positives )

@oli-obk oli-obk added C-bug Category: Clippy is not doing the correct thing T-middle Type: Probably requires verifiying types labels May 10, 2017
@liamcurry
Copy link

Might also be worth mentioning matching on enums with named fields of the same name, e.g.:

pub enum Test {
    A { field: String },
    B { field: String },
}

impl Test {
    /// `match_same_arms` warning
    fn field_bad(&self) -> &str {
        match self {
            Self::A { field } => field,
            Self::B { field } => field,
        }
    }
    /// no warning
    fn field_good(&self) -> &str {
        match self {
            Self::A { field } | Self::B { field } => field,
        }
    }
}

@phansch phansch added the I-false-positive Issue: The lint was triggered on code it shouldn't have label Dec 18, 2020
@bors bors closed this as completed in f07ee8a Mar 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have T-middle Type: Probably requires verifiying types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants