-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Create 0000-unreachable-patterns-error.md #2058
Conversation
One potential situation where I imagine multiple matching patterns could be useful is in macro-generated code. However given that the warning is implemented as a lint it seems reasonable to make the lint deny by default (if the current rust stability guidelines allow that), which macro authors can selectively disable. |
It was an error, it was made a warning just recently, in Rust 1.16. |
I don't think this should be turned into an error. An unreachable pattern is generally indicative of bad code smell but it's not something that would cause a problem with the execution. It's unreachable but that's not a problem really. We make Non-Exhaustive patterns an error because in this case what happens if there's a match and nothing to handle it? The execution flow would fail to work properly. I can see value in this but it seems like it's better off as a warning lint as is and not an error as it will not cause the code to fail to work. Clippy for instance lints against bad code like this but doesn't make it an error. If you really want these to be an error you can just put |
Actually it is not as simple as it looks like. There is no way to switch on/off this for whole crate, |
It causes execution of branches that not expected to execute, And it often enough issue, for example: https://stackoverflow.com/search?q=%5Brust%5D+unreachable+pattern |
To be clear, this does switch it on/off for the whole crate; you're talking about packages, which have multiple crates in them. |
How about we improve the warnings in the presence of patterns where there's a local variable of the same name? We already have this for enum variants. |
I like rust because of it is solid as a rock to prevent errors. That's why if choice is between some corner corner case (rust-lang/rust#38069 ) and "solid as a rock" compile time check, then choose "solid as a rock" and for corner case use some corner work around. |
/cc @canndrew So what about reverting of rust-lang/rust#38069 ? I think consistent semantic of language is more importnat then handling of empty enum case. |
I don't differentiate much between errors and warnings. For me any diagnostic output means I cannot publish as it is. I even try to be warning free while developing (or at least only have fewer than 5 warnings).
That PR was made out of a concrete reason (consistency!). Please read the referenced issue that the PR fixes. There's a lot of discussion both ways, but it was decided that this is the best solution. If we make it a hard error again, we can't improve the rules in the compiler. Think about this case: match some_u8 {
0...100 => {},
101...255 => {},
_ => {}, // today this branch is necessary, due to a limitation
} In the future, that branch will turn into a warning, because it's statically known to be unreachable, but it's not an error right now. If we made it an error, we'd break perfectly valid code in the future. Making the compiler smarter should not result in previously working user code breaking. Have a look at #1229 where this exact same issue was discussed. If we make the compiler smarter, it might detect that you wrote
As it has been said before, if there's no clear winner, let the users decide. And you can already decide with |
@Dushistov it will always need to be possible to turn this error off for the sake of macro-generated code. It would be nice if we could make it error-by-default again though - and force people to use |
Please excuse my superficial knowledge. In my opinion this seems equivalent to the unused imports error in Go. It might be great if you are about to release your changes but really annoying if you're just poking around. Unlike Go, Rust does have warnings so perhaps this should stay as a warning. |
Having an unreachable pattern is a lot worse than having an unused import, because of Rust's decision to make consts in patterns and bindings in patterns use the same syntax! This means that the behavior of |
From my own experience with Rust development, having "unreachable patterns" be a warning is quite useful: Sometimes, during development, I find myself temporarily needing to switch off some branches every now and again. If this were turned into an error again that would become impossible. Note: om not talking about releasing any code with unreachable code in it; This is merely during development itself. Also, since it was an error and now is a warning, I think there should be a strong bias against changing it again, especially if there's no revolutionary new reason or use case driving the change. Flip flopping on any feature or bug seems like a very bad idea to me, in general. |
Thanks for the RFC! However, as @canndrew points out, there are good reasons to keep this within the lint system, and we hope to move it to error-by-default in the future (which I believe will largely address the aims of this RFC). As such, I'm going to go ahead and close. |
No description provided.