Skip to content

let_unit_value: FP on conditionally unit values #15790

@ada4a

Description

@ada4a

Summary

The lint fires even if the type of the initializer is cfg-dependent

Lint Name

let_unit_value

Reproducer

I tried this code:

#![allow(unused)]
#![warn(clippy::let_unit_value)]

fn takes_str(_: &str) {}
fn takes_unit(_: ()) {}

fn foo() {
    let value = {
        #[cfg(test)]
        {
            "not a unit"
        }
    };
    
    #[cfg(test)]
    takes_str(value);
    #[cfg(not(test))]
    takes_unit(value);
}

I saw this happen:

warning: this let-binding has unit value
  --> src/main.rs:9:5
   |
 9 | /     let value = {
10 | |         #[cfg(test)]
11 | |         {
12 | |             "not a unit"
13 | |         }
14 | |     };
   | |______^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value
note: the lint level is defined here
  --> src/main.rs:2:9
   |
 2 | #![warn(clippy::let_unit_value)]
   |         ^^^^^^^^^^^^^^^^^^^^^^
help: omit the `let` binding and replace variable usages with `()`
   |
 9 ~     {
10 +         #[cfg(test)]
11 +         {
12 +             "not a unit"
13 +         }
14 +     };
15 |     
...
18 |     #[cfg(not(test))]
19 ~     takes_unit(());
   |

I expected to see this happen:
Nothing, because the type of value is not always ()

Version

rustc 1.90.0 (1159e78c4 2025-09-14)
binary: rustc
commit-hash: 1159e78c4747b02ef996e55082b704c09b970588
commit-date: 2025-09-14
host: x86_64-unknown-linux-gnu
release: 1.90.0
LLVM version: 20.1.8

Additional Labels

@rustbot label I-suggestion-causes-error

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 haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions