rust-lang / rust-lang/rust-clippy

let-binding has unit value fix breaks code

Open
#10,758 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

I have got this warning:

warning: this let-binding has unit value
  --> dyn_utils\src\any_store.rs:97:3
   |
97 |         let metadata = std::ptr::metadata(&value);
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `std::ptr::metadata(&value);`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value
   = note: `#[warn(clippy::let_unit_value)]` on by default

I tried the auto fixing, but it broke the code:

cargo clippy --fix --lib -p dyn_utils
Lint Name

let_unit_value

Reproducer

I tried this code:

pub struct AnyId<T: ?Sized> {
   metadata: <T as Pointee>::Metadata,
}

pub struct AnyStore {}

impl AnyStore {
   pub fn push<T: 'static>(value: T) -> AnyId<T> {
      let metadata = std::ptr::metadata(&value);
      AnyId {
	   metadata,
      }
   }
}

I saw this happen:

The following errors were reported:
error[E0425]: cannot find value `metadata` in this scope
   --> dyn_utils\src\any_store.rs:111:4
    |
111 |             metadata,
    |             ^^^^^^^^ not found in this scope
    |
help: consider importing one of these items
    |
1   | use core::ptr::metadata;
    |
1   | use std::fs::metadata;
    |
1   | use std::ptr::metadata;
    |

error: aborting due to previous error

For more information about this error, try `rustc --explain E0425`.
Original diagnostics will follow.

warning: this let-binding has unit value
  --> dyn_utils\src\any_store.rs:97:3
   |
97 |         let metadata = std::ptr::metadata(&value);
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `std::ptr::metadata(&value);`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value
   = note: `#[warn(clippy::let_unit_value)]` on by default

warning: `dyn_utils` (lib) generated 1 warning (run `cargo clippy --fix --lib -p dyn_utils` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 0.48s
Version
rustc 1.71.0-nightly (f9a6b7158 2023-05-05)
binary: rustc
commit-hash: f9a6b71580cd53dd4491d9bb6400f7ee841d9c22
commit-date: 2023-05-05
host: x86_64-pc-windows-msvc
release: 1.71.0-nightly
LLVM version: 16.0.2
Additional Labels

No response

Contributor guide

Open the contributing guide

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 reproducing the issue in dyn_utils/src/any_store.rs with the let_unit_value lint and cargo clippy --fix --lib -p dyn_utils. Inspect how the lint's suggestion turns the binding into a standalone expression, then verify the metadata field still compiles and the original warning is handled without the E0425 error.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.