Skip to content

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

Open
@tesuji

Description

@tesuji

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions