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

Can rust provide a warning on unreachable if patterns? #15736

Closed
farcaller opened this issue Jul 17, 2014 · 6 comments
Closed

Can rust provide a warning on unreachable if patterns? #15736

farcaller opened this issue Jul 17, 2014 · 6 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@farcaller
Copy link
Contributor

Here's a bug generated by a simple copy-paste mistake:

fn init_flash_access(freq: u32) {
  let num_clocks: u32 = if freq > 100_000_000 { 6 } else
                        if freq > 80_000_000 { 5 } else
                        if freq > 60_000_000 { 4 } else
                        if freq > 40_000_000 { 3 } else
                        if freq > 40_000_000 { 2 } else
                        { 1 };
  let val = (num_clocks - 1) << 12;
  reg::FLASHCFG.set_value(val);
}

This function will never return 2, as 40_000_000 in the last line should actually be 20_000_000. Is there any way rust could warn me about that or any other applicable pattern to do it?

@huonw
Copy link
Member

huonw commented Jul 17, 2014

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.

@huonw huonw added the A-lint label Jul 17, 2014
@kud1ing
Copy link

kud1ing commented Jul 17, 2014

Just for reference, PVS-Studio has a check for this: http://www.viva64.com/en/d/0106/

@ftxqxd
Copy link
Contributor

ftxqxd commented Jul 17, 2014

While attempting an implementation of such a lint, I encountered a problem: cfg! macros are often used in if conditions, and a lint would be triggered by a chain like:

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 cfg!s would expand to false. Should this lint just ignore bare trues and falses (because presumably you know what you’re doing), or should such places manually disable the lint?

Also, this lint discovered what may be a bug in std: over here, $RP$ presumably stands for ‘raw pointer’, but a few lines later it’s used to mean ‘right parenthesis’. Assuming this is a bug, this really does show the use of such a lint.

@huonw
Copy link
Member

huonw commented Jul 17, 2014

@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 cfg! thing you describe.)

@ghost
Copy link

ghost commented Aug 14, 2014

@huonw That looks very interesting but this issue seems nonetheless out of scope of rustc? Could this be closed?

@ghost
Copy link

ghost commented Nov 11, 2014

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.

@ghost ghost closed this as completed Nov 11, 2014
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 13, 2023
…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
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

4 participants