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

unnecessary-operation suggestion on [();1][<expr>] is werid #12817

Open
tesuji opened this issue May 18, 2024 · 0 comments
Open

unnecessary-operation suggestion on [();1][<expr>] is werid #12817

tesuji opened this issue May 18, 2024 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@tesuji
Copy link
Contributor

tesuji commented May 18, 2024

Summary

Because of my minimal rust version (1.56), I have to use the hack [(); 1][<expr>] in const block for const assertions.

Lint Name

unnecessary-operation

Reproducer

I tried this code:

const _: () = {
    use std::os::windows::io::RawHandle;
    let std_layout = Layout::new::<RawHandle>();
    let win_sys_layout = Layout::new::<usize>();
    // MSRV(Rust v1.57): use assert! instead
    [(); 1][std_layout.size() - win_sys_layout.size()];
};

I saw this happen:

error: unnecessary operation
  --> src/internals/c.rs:42:5
   |
42 |     [(); 1][std_layout.size() - win_sys_layout.size()];
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: statement can be written as: `assert!([(); 1].len() > std_layout.size() - win_sys_layout.size());`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
   = note: `-D clippy::unnecessary-operation` implied by `-D warnings`

I expected to see this happen: It's my fault for using nightly clippy for msrv, however I expect that clippy should suggest

assert!(std_layout.size() == win_sys_layout.size());

Version

rustc 1.80.0-nightly (1a7397988 2024-05-17)
binary: rustc
commit-hash: 1a7397988684934ae01a71f524bdfff24895d8cc
commit-date: 2024-05-17
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.4

Additional Labels

No response

@tesuji tesuji added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels May 18, 2024
@tesuji tesuji changed the title unnecessary-operation suggestion on [();1][<expr> is werid unnecessary-operation suggestion on [();1][<expr>] is werid May 18, 2024
@tesuji tesuji changed the title unnecessary-operation suggestion on [();1][<expr>] is werid unnecessary-operation suggestion on [();1][<expr>] is werid May 18, 2024
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 I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

1 participant