Skip to content

assertion_on_constants lint: false positive? #3765

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

Closed
sanmai-NL opened this issue Feb 15, 2019 · 5 comments · Fixed by #3989
Closed

assertion_on_constants lint: false positive? #3765

sanmai-NL opened this issue Feb 15, 2019 · 5 comments · Fixed by #3989
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing E-needs-test Call for participation: writing tests good first issue These issues are a good way to get started with Clippy

Comments

@sanmai-NL
Copy link
Contributor

The following code:

if let Some(data_sub) = &data.get(range) {
    *data_sub
} else {
    debug_assert!(false, "Out-of-bounds read. ");
}

Triggers assertion_on_constants for me. https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants explains that this assertion would be optimized out by the compiler. That shouldn't be so. A statically false assertion should become a panic, not be optimized away. Since this assertion is only triggered in debug builds, using a plain unreachable! or panic! isn't an equivalent alternative. So as long as debug_unreachable! isn't part of the standard library, this lint should not fire in cases like the above. Furthermore, the lint explanation should be corrected to say that (only) statically true assertions may be optimized out.

clippy 0.0.212 (6ce78d12 2019-01-29)
@phansch phansch added E-needs-test Call for participation: writing tests C-bug Category: Clippy is not doing the correct thing labels Feb 15, 2019
@phansch
Copy link
Member

phansch commented Feb 15, 2019

This is probably fixed by #3740, however that's not included in a nightly yet.

It might be good to add this test case to the UI tests: https://github.com/rust-lang/rust-clippy/blob/master/tests/ui/assertions_on_constants.rs

@phansch phansch added the good first issue These issues are a good way to get started with Clippy label Feb 15, 2019
@mcarton
Copy link
Member

mcarton commented Feb 25, 2019

Confirmed: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=eba556d590a3e9f3c718e6061130e0f5
This doesn't trigger any warning anymore and is being tested by said PR.

@mcarton mcarton closed this as completed Feb 25, 2019
@sanmai-NL
Copy link
Contributor Author

@mcarton: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=213800165b6b1581281c97c4d040110e still triggers a warning when I run Clippy against this kind of code locally too, and I just just updated to the latest nightly and Clippy component. You've closed the issue, were you aware of @phansch's observation? Can you please explain how the issue can be solved downstream for the programmer using Clippy?

@bjorn-ove
Copy link

This lint still triggers in the clippy version that comes with rustup in nightly-2019-04-12-x86_64-unknown-linux-gnu

[bjorn@devbox debug_assert_test]$ cat src/lib.rs 
pub fn panic_when_debug_build() {
    debug_assert!(false, "This is a debug build");
}

[bjorn@devbox debug_assert_test]$ cargo +nightly-2019-04-14-x86_64-unknown-linux-gnu clippy
    Checking debug_assert_test v0.1.0 (/home/bjorn_andersen/tmp/debug_assert_test)
warning: assert!(false) should probably be replaced
 --> src/lib.rs:2:5
  |
2 |     debug_assert!(false, "This is a debug build");
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(clippy::assertions_on_constants)] on by default
  = help: use panic!() or unreachable!()
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants
  = note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

    Finished dev [unoptimized + debuginfo] target(s) in 0.31s

@phansch phansch reopened this Apr 15, 2019
@flip1995 flip1995 self-assigned this Apr 15, 2019
@flip1995
Copy link
Member

cc #3948

This lint should not be triggered at all for debug_assert!(false). I will fix this ASAP.

bors added a commit that referenced this issue Apr 19, 2019
Don't trigger assertions_on_constants on debug_assert!(false)

Fixes #3948
Fixes #3765

changelog: Fix `debug_assert!` false positive on `assertions_on_constants` lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing E-needs-test Call for participation: writing tests good first issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants