Skip to content

Clippy gives unit_arg warning when using black_box #12707

Closed
@xobs

Description

@xobs

Summary

Consider the following program:

use std::{hint, thread, time};
fn main() {
    hint::black_box(thread::sleep(time::Duration::from_millis(10)));
}

The black_box() call is used to request that the function call is not removed. In this case, sleep() does not return anything. Even so, in order to give the black_box hint, it must be passed to the function.

The suggestion is to remove the black box, which is not an option.

Lint Name

unit_arg

Reproducer

I tried this code:

use std::{hint, thread, time};
fn main() {
    hint::black_box(thread::sleep(time::Duration::from_millis(10)));
}

I saw this happen:

    Checking playground v0.0.1 (/playground)
warning: passing a unit value to a function
 --> src/main.rs:3:5
  |
3 |     hint::black_box(thread::sleep(time::Duration::from_millis(10)));
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unit_arg
  = note: `#[warn(clippy::unit_arg)]` on by default
help: move the expression in front of the call and replace it with the unit literal `()`
  |
3 ~     thread::sleep(time::Duration::from_millis(10));
4 ~     hint::black_box(());
  |

I expected to see this happen:

No warning, because the point of black_box is to be a hint.

Version

0.1.79 (2024-04-22 7f2fc33)

From https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=dbf98b9c0525acfe5280671a77fdf304

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