rust-lang / rust-lang/rust-clippy

let_unit_value when using constants to limit values of `const usize`

Open
#9,080 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

The pattern let _ = CONSTANT_VALUE is used to ensure that various consts conform to expected values. For example, I have a const that I need to be aligned to 4096 bytes.

This check is done by using the CONSTANT_VALUE during some const constructor and throwing away the result. However, Clippy reports that this code is unused, and needs to be discarded.

Furthermore, clippy appears to ignore #[allow(clippy::let_unit_value)].

Lint Name

let_unit_value

Reproducer

I tried this code:

#[repr(C, align(4096))]
pub struct Buffer<const N: usize = 4096> {
    data: [u8; N],
}

impl<const N: usize> Buffer<N> {
    // Ensure that `N` is a multiple of 4096
    const CHECK_ALIGNED: () = if N & 4095 != 0 {
        panic!("Buffer size must be a multiple of 4096")
    };

    pub const fn new() -> Self {
        #[allow(clippy::let_unit_value)]
        let _ = Self::CHECK_ALIGNED;
        Buffer { data: [0u8; N], }
    }
}

fn main() {
    let buf1 = Buffer::<4096>::new();
    // let buf2 = Buffer::<4097>::new();
    let buf3 = Buffer::<8192>::new();
    println!("Size of buf1: {}", core::mem::size_of_val(&buf1));
    println!("Size of buf3: {}", core::mem::size_of_val(&buf3));
}

I saw this happen:

warning: this let-binding has unit value
  --> src/main.rs:14:9
   |
14 |         let _ = Self::CHECK_ALIGNED;
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `Self::CHECK_ALIGNED;`
   |
   = note: `#[warn(clippy::let_unit_value)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value

I expected to see this happen:

No warning

Version
rustc 1.62.0 (a8314ef7d 2022-06-27)
binary: rustc
commit-hash: a8314ef7d0ec7b75c336af2c9857bfaf43002bfc
commit-date: 2022-06-27
host: x86_64-pc-windows-msvc
release: 1.62.0
LLVM version: 14.0.5
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the let_unit_value lint and reproduce the reported warning using the Rust example in this issue. Investigate why the lint warns on let _ = Self::CHECK_ALIGNED and ignores the local allow attribute; done means the reproducer produces no warning while the lint still handles its other cases correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.