Why does this trigger clippy::unit-arg?

Hi all,

I have the following code that triggers Clippy's unit-arg lint and I have absolutely no idea why :sweat_smile: . Can anybody explain this to me?

The code:

/// Observe RSSI values.
#[tracing::instrument(level = "info", err)]
pub fn monitor_rssi(update_tx: Sender<Update>) -> Result<()> {
    tracing::debug!("spawn btmon");
    let (btmon, btlines) = launch_btmon()?;

    thread::spawn(move || {
        // fn inner_loop(Child, impl Iterator, Sender<Update>) -> Result<()>
        if let Err(error) = inner_loop(btmon, btlines, update_tx) {
            tracing::error!(%error, "RSSI monitor failed!");
        }
    });

    Ok(())
}

The warning emitted by Clippy:

error: passing a unit value to a function
   --> src/bluetooth/rssi.rs:155:62
    |
155 |   pub fn monitor_rssi(update_tx: Sender<Update>) -> Result<()> {
    |  ______________________________________________________________^
156 | |     tracing::debug!("spawn btmon");
157 | |     let (btmon, btlines) = launch_btmon()?;
158 | |
...   |
165 | |     Ok(())
166 | | }
    | |_^
    |
    = note: `-D clippy::unit-arg` implied by `-D warnings`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unit_arg
help: move the expression in front of the call and replace it with the unit literal `()`
    |
155 | pub fn monitor_rssi(update_tx: Sender<Update>) -> Result<()> {
156 |     {
157 |         tracing::debug!("spawn btmon");
158 |         let (btmon, btlines) = launch_btmon()?;
159 | 
160 |         thread::spawn(move || {
  ...

I know I can switch Clippy of with `#[allow(clippy::unit-arg)]. I'm just curious why the lint is triggered.

tracing::instrument expands to code that contains the following snippet:

match { /* function body */ } {
    Ok(x) => Ok(x),
    Err(e) => { /* ... */ }
}

Because the function returns Result<(), E>, the type of x is (), which means that the Ok(x)is a call of the function Ok with a unit arg.

Thank you for the answer so #[allow(clippy::unit-arg)] is the way to go here.

I wish there would be an easy way to see the expanded code from a macro...

@matklad Maybe this would be a nice feature for the Rust analyzer?

I used tools > expand macros on the playground. There is also the cargo-expand crate.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.