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 on assert!(always_false) #73435

Open
bugaevc opened this issue Jun 17, 2020 · 2 comments
Open

Warn on assert!(always_false) #73435

bugaevc opened this issue Jun 17, 2020 · 2 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bugaevc
Copy link

bugaevc commented Jun 17, 2020

It would be useful if rustc emitted a warning on assertions when the condition can be statically proven to always be false.

Example:

pub fn meh() {
    let always_false = 2 > 3;
    assert!(always_false);
    println!("foo");
}

Currently compiles cleanly without any warnings (playground) — neither about the always false assertion, nor about the unreachable statement.

The LLVM IR emitted:

; playground::meh
; Function Attrs: noreturn nonlazybind uwtable
define void @_ZN10playground3meh17h3a7dd68d372b246aE() unnamed_addr #3 {
start:
; call std::panicking::begin_panic
  tail call fastcc void @_ZN3std9panicking11begin_panic17h982033eda4eb16e4E()
  unreachable
}

suggests rustc is able to statically prove the assertion to be always false even before LLVM optimization is run.

I would expect to get something similar to this:

warning: assertion is always false: `always_false`
 --> meh.rs:3:15
  |
3 |     assert!(always_false);
  |             ^^^^^^^^^^^^ the assertion is always false                                  
  |
  = help: to unconditionally panic, use `panic!()` or `unreachable!()` 
  = note: `#[warn(assert_is_always_false)]` on by default

warning: unreachable statement
 --> meh.rs:4:5
  |
3 |     assert!(always_false);
  |     ---------------------- any code following this expression is unreachable
4 |     println!("foo");
  |     ^^^^^^^^^^^^^^^^ unreachable statement
  |
  = note: `#[warn(unreachable_code)]` on by default

Meta

This is not about any particular version or platform, but here it goes anyway:

rustc --version --verbose:

rustc 1.44.0 (49cae5576 2020-06-01)
binary: rustc
commit-hash: 49cae55760da0a43428eba73abcb659bb70cf2e4
commit-date: 2020-06-01
host: x86_64-unknown-linux-gnu
release: 1.44.0
LLVM version: 9.0
@jonas-schievink jonas-schievink added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jun 17, 2020
@matthiaskrgr
Copy link
Member

There's already a clippy lint that lints on literal true and false. rust-lang/rust-clippy#3582
It does not warn on constants or variables that can be const-folded though.
See also rust-lang/rust-clippy#3575

@bugaevc
Copy link
Author

bugaevc commented Jun 17, 2020

That's clearly related, but my request is about a different use-case, where I write an actual assertion that I think guards something, but due to some stupid mistake it doesn't, in which case I would like rustc to let me know. Explicitly and intentionally writing assert!(false) is different.

@Alexendoo Alexendoo added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 17, 2020
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. C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants