oxidecomputer / oxidecomputer/usdt

clippy::cast_lossless lint should be silenced

Open
#270 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
145
Forks
15
Avg merge
14m
Merged PRs (30d)
6

Description

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.

Contributor guide

No contributing guide indexed for this repository

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 by expanding the #[usdt::provider] macro with cargo expand and reproduce the warning using cargo clippy with clippy::cast_lossless enabled. Inspect the generated casts shown in the issue and ensure the autogenerated code suppresses this lint without hiding warnings from caller code; done means the reported probe invocations no longer fail under -D warnings.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.