Skip to content

Warn on assert!(always_false) #73435

Open

Description

@bugaevc

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

Activity

  1. matthiaskrgr commented on Jun 17, 2020

    @matthiaskrgr
    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

  2. bugaevc commented on Jun 17, 2020

    @bugaevc
    Author

    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.

  3. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jun 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions