Skip to content

Suggest replacing manual equality assertion with assert_eq! #14557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ulrichstark opened this issue Apr 5, 2025 · 2 comments
Open

Suggest replacing manual equality assertion with assert_eq! #14557

ulrichstark opened this issue Apr 5, 2025 · 2 comments
Labels
A-lint Area: New lints

Comments

@ulrichstark
Copy link

ulrichstark commented Apr 5, 2025

What it does

Suggests replacing assert!(matches!(a, b)) and assert!(a == b); with assert_eq!(a, b) if PartialEq and Debug is implemented

Advantage

  • communicates the intent more directly
  • provides better debugging output if the assertion fails

Example

Suppose ...

#[derive(PartialEq, Debug)]
enum Enum {
    A,
    B,
}

let value = Enum::A;

Then the following code

assert!(matches!(value, Enum::A));
// or
assert!(value == Enum::A);

could be written as:

assert_eq!(value, Enum::A);

Also similar case possible for assert_ne!, debug_assert_eq!, debug_assert_ne!

@ulrichstark ulrichstark added the A-lint Area: New lints label Apr 5, 2025
@mi2ebi
Copy link

mi2ebi commented Apr 5, 2025

When dealing with eg very long strings I sometimes prefer assert! specifically so I don't have to see them

@samueltardieu
Copy link
Contributor

Related: #6637

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

3 participants