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

Warn about overlapping patterns #5657

Closed
lkuper opened this issue Apr 1, 2013 · 12 comments
Closed

Warn about overlapping patterns #5657

lkuper opened this issue Apr 1, 2013 · 12 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@lkuper
Copy link
Contributor

lkuper commented Apr 1, 2013

We already check pattern matches for non-exhaustiveness. It would be nice to also produce a warning for overlapping match patterns ("over-exhaustiveness"). GHC, for example, does this.

@nikomatsakis
Copy link
Contributor

We do report useless patterns (those that cannot match because a prior
pattern must have matched it). Did you mean something else?

@lkuper
Copy link
Contributor Author

lkuper commented Apr 2, 2013

The unreachable pattern warning is good, but I'm imagining something that would warn any time the sets of things matched by each pattern were not disjoint. The point of the warning would be to say, "Be advised: the order in which these match arms are written is relevant!"

On the other hand, maybe this isn't right for Rust because we often write a catch-all _ => ... pattern at the end of matches, and this warning would always warn about that.

@nikomatsakis
Copy link
Contributor

Ah, I see! Well, you could make it default to off. Or perhaps we could somehow treat _ as a special case.

@lkuper
Copy link
Contributor Author

lkuper commented Apr 4, 2013

Having it default to off sounds like the right thing.

@catamorphism
Copy link
Contributor

far-future

@msullivan
Copy link
Contributor

I don't think that GHC warns about the order of match arms being relevant? Certainly ML compilers don't.

@lkuper
Copy link
Contributor Author

lkuper commented Jul 29, 2013

What GHC does is warn about a set of patterns being overlapping -- -fwarn-overlapping-patterns is on by default. And this is a little more strict than merely warning about unreachable patterns, which Rust does already.

The idea is that if you have a set of patterns such that in one order you'd get an unreachable pattern warning in Rust, but in a different order you wouldn't, then that code is not robust to reordering. Sometimes this is fine, but other times you might want to at least get a warning that the code is fragile.

@huonw
Copy link
Member

huonw commented Nov 10, 2013

Triage: no progess that I know of.

@huonw
Copy link
Member

huonw commented Jun 16, 2014

previous_comment.clone()

@steveklabnik
Copy link
Member

previous_comment.clone()

@Manishearth Manishearth added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. and removed A-diagnostics Area: Messages for errors, warnings, and lints labels Nov 20, 2015
@Manishearth
Copy link
Member

This would have to be a lint, since it's not strictly undesirable behavior. Perhaps this should be closed in favor of an issue on clippy since we're not adding new lints?

@steveklabnik
Copy link
Member

Since new lints have a big impact on users of rustc, the policy is that they should go through the RFC process like other user-facing changes. As such, I'm going to give this one a close, but if anyone comes across this ticket and wants this lint, consider adding it to clippy and/or writing up an RFC. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

7 participants