Skip to content

Warn on assert!(always_false) #73435

Open
@bugaevc

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

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