oxidecomputer / usdt

Dust your Rust with USDT probes
Apache License 2.0
82 stars 10 forks source link

clippy::cast_lossless lint should be silenced #270

Open sunshowers opened 1 month ago

sunshowers commented 1 month ago

With Rust 1.80 and this code:

#[usdt::provider(provider = "nexus_db_queries")]
mod probes {
    // Fires before we start a search over a range for a VNI.
    //
    // Includes the starting VNI and the size of the range being searched.
    fn vni__search__range__start(
        _: &usdt::UniqueId,
        start_vni: u32,
        size: u32,
    ) {
    }

    // Fires when we successfully find a VNI.
    fn vni__search__range__found(_: &usdt::UniqueId, vni: u32) {}

    // Fires when we fail to find a VNI in the provided range.
    fn vni__search__range__empty(_: &usdt::UniqueId) {}
}

// ...

crate::probes::vni__search__range__start!(|| {
    (&id, u32::from(vni), VniSearchIter::STEP_SIZE)
});

Running cargo clippy with the clippy::cast_lossless lint enabled produces this warning:

asting `u32` to `#[usdt::provider(provider = "nexus_db_queries")]` may become silently lossy if you later change the type
   --> nexus/db-queries/src/lib.rs:24:1
    |
24  |   #[usdt::provider(provider = "nexus_db_queries")]
    |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
   ::: nexus/db-queries/src/db/datastore/vpc.rs:360:13
    |
360 | /             crate::probes::vni__search__range__start!(|| {
361 | |                 (&id, u32::from(vni), VniSearchIter::STEP_SIZE)
362 | |             });
    | |______________- in this macro invocation
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless
    = note: `-D clippy::cast-lossless` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(clippy::cast_lossless)]`
    = note: this error originates in the macro `crate::probes::vni__search__range__start` (in Nightly builds, run with -Z macro-backtrace for more info)
help: try
    |
24  + #[usdt::provider(provider = "nexus_db_queries")]::from(crate::probes::vni__search__range__start!(|| {
25  +                 (&id, u32::from(vni), VniSearchIter::STEP_SIZE)
26  +             }))
    |

error: casting `u32` to `#[usdt::provider(provider = "nexus_db_queries")]` may become silently lossy if you later change the type
   --> nexus/db-queries/src/lib.rs:24:1
    |
24  |   #[usdt::provider(provider = "nexus_db_queries")]
    |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
   ::: nexus/db-queries/src/db/datastore/vpc.rs:372:21
    |
372 | /                     crate::probes::vni__search__range__found!(|| {
373 | |                         (&id, u32::from(vpc.vni.0))
374 | |                     });
    | |______________________- in this macro invocation
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless
    = note: this error originates in the macro `crate::probes::vni__search__range__found` (in Nightly builds, run with -Z macro-backtrace for more info)
help: try
    |
24  + #[usdt::provider(provider = "nexus_db_queries")]::from(crate::probes::vni__search__range__found!(|| {
25  +                         (&id, u32::from(vpc.vni.0))
26  +                     }))
    |

From a quick look at cargo expand, it looks like this code is produced:

if is_enabled != 0 {
    let args = __usdt_private_args_lambda();
    let arg_0 = args.0.as_u64() as i64;
    let arg_1 = (*<_ as ::std::borrow::Borrow<
        u32,
    >>::borrow(&args.1) as i64);
    let arg_2 = (*<_ as ::std::borrow::Borrow<
        u32,
    >>::borrow(&args.2) as i64);

    // ...
}

I think this code is what's producing the warning.

Since this is autogenerated code, we should silence this lint. I think annotating with automatically_derived might work? If not then explicitly silencing this lint should do the job.

bnaecker commented 1 month ago

Agreed, let's silence this inside the generated code.