-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Can rust provide a warning on unreachable if
patterns?
#15736
Comments
It seems reasonable to have a lint that can check for copy-paste errors (this is probably quite simple, just compare ASTs for equality), more general range analysis etc. is presumably NP-hard (seems like it would be easy to encode 3-SAT) or just plain undecidable, meaning it wouldn't be possible pick up every case. I will note that lints can now loaded as plugins (even via cargo, e.g., https://github.com/huonw/spellck), so it would be possible to have this as an external addition. |
Just for reference, PVS-Studio has a check for this: http://www.viva64.com/en/d/0106/ |
While attempting an implementation of such a lint, I encountered a problem: if cfg!(target_os = "windows") { ... }
else if cfg!(target_os = "macos") { ... }
else if cfg!(target_os = "linux") { ... } because at compile-time, at least two of the Also, this lint discovered what may be a bug in std: over here, |
@P1start oh, you tried implementing it too? Cool! I got something vaguely working here: https://github.com/huonw/copypasteck (requires #15737 to work). (Although I haven't run it on any non-trivial code, so I didn't encounter the |
@huonw That looks very interesting but this issue seems nonetheless out of scope of rustc? Could this be closed? |
I am going to close this issue. It is not feasible for the compiler to reason about arbitrary boolean expressions to determine which if clauses are not reachable. In addition, the narrow use case of warning on accidentally copy-pasted if clauses seems to be best served by a pluggable lint rather than a built-in one. |
…diagnostics, r=HKalbasi fix: add incorrect case diagnostics for module names Adds diagnostics for checking both inline and file module names are snake case. Closes rust-lang#15678
Here's a bug generated by a simple copy-paste mistake:
This function will never return 2, as
40_000_000
in the last line should actually be20_000_000
. Is there any way rust could warn me about that or any other applicable pattern to do it?The text was updated successfully, but these errors were encountered: