Skip to content

unit_cmp: don't lint on explicitly written units #15559

@jieyouxu

Description

@jieyouxu

Summary

Sometimes it's useful to be able to explicitly compare against () in test assertions (e.g. generated code). IMO clippy's unit_cmp should not lint on explicitly written () within assertions, as e.g.

assert_eq!(field, ());

is more obvious intention-wise than

let () = field;

Lint Name

unit_cmp

Reproducer

I tried this code:

fn main() {
    struct Generated { field: () }
    assert_eq!(Generated { field: () }.field, ());
}

I saw this happen:

error: `assert_eq` of unit values detected. This will always succeed
 --> src/main.rs:3:5
  |
3 |     assert_eq!(Generated { field: () }.field, ());
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unit_cmp
  = note: `#[deny(clippy::unit_cmp)]` on by default

I expected to see this happen:

  • No errors because RHS () is explicitly spelled out, and user intention is clear.

Version

rustc 1.89.0 (29483883e 2025-08-04)
binary: rustc
commit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2
commit-date: 2025-08-04
host: aarch64-apple-darwin
release: 1.89.0
LLVM version: 20.1.7

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions