Minor, mostly irrelevant, clippy mystery

I ran into something minor, but still surprising. Using this code:

fn main() {
  let md = std::fs::metadata("file.txt").unwrap();
  let size = md.len() as u64;
  println!("{size}");
}

And this section in Cargo.toml:

[lints.clippy]
all = { level = "deny", priority = -1 }
pedantic = { level = "warn", priority = -1 }
nursery = { level = "warn", priority = -1 }
cargo = { level = "warn", priority = -1 }

cargo_common_metadata = "allow"

On macos:

$ cargo version
cargo 1.81.0 (2dbb1af80 2024-08-20)
$ cargo clippy
    Checking playground v0.1.0 (/Volumes/ramdisk/playground)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.08s

On Linux:

$ cargo version
cargo 1.81.0 (2dbb1af80 2024-08-20)
$ cargo clippy
    Checking playground v0.1.0 (/tmp/playground)
error: casting to the same type is unnecessary (`u64` -> `u64`)
 --> src/main.rs:4:14
  |
4 |   let size = md.len() as u64;
  |              ^^^^^^^^^^^^^^^ help: try: `md.len()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
  = note: `-D clippy::unnecessary-cast` implied by `-D clippy::all`
  = help: to override `-D clippy::all` add `#[allow(clippy::unnecessary_cast)]`

error: could not compile `playground` (bin "playground") due to 1 previous error

Ignoring the fact that it, indeed, is a unnecessary cast -- why would this lint trigger on Linux, but not mac (both x86-64 platforms)? The md.len() returns u64 on both platforms.

1 Like

Oddly I don't not get that clippy lint error on my MacBook Pro M1 or ARM64 based Nvidia Jetson running Ubuntu when using the latest nightly Rust:

uname -a
Linux jetson-itc 4.9.201-tegra #1 SMP PREEMPT Fri Jul 9 08:56:29 PDT 2021 aarch64 aarch64 aarch64 GNU/Linux
cargo --version
cargo 1.83.0-nightly (a9a418d1a 2024-09-15)

I do get it using 1.81.0 stable on the Jetson though. Whatever it is seems to have been fixed.

But adding your clippy config to my current project showed up a lot of juicy things to fix and pointed out one place where I'm being sloppy in checking input data. Thanks!

2 Likes

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.