rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Reeneable should_assert_eq lint

Open ndmitchell opened this issue 4 years ago • 2 comments

Suggest replacing assert!(a == b) with assert_eq!(a, b). This is useful, because given the environment let a = 1, let b = 2, the displayed error changes between:

  • assert!(a == b) gives assertion failed: a == b
  • assert_eq!(a, b) gives assertion_failed: left = 1, right = 2

The latter is vastly more helpful at diagnosing test failures. This lint was removed in #2156 because of RFC 2011. It's hard to follow the implementation progress of that RFC, but the issue hasn't been updated since early 2018, and I got the above assertion failures using a 2020-10-15 toolchain, so assert_eq! is still preferable.

ndmitchell avatar Jan 25 '21 12:01 ndmitchell

One year later the situation hasn't get any better. I vote for reenabling this as well and letting it enabled up until a certain rust version.

Maybe one could use the MSRV and if it's lower than 1.xx it should lint that code, otherwise not.

hellow554 avatar Apr 04 '22 06:04 hellow554

Given that RFC 2011 hinges on an edition change, it will take at least until 2027/2028 for it to be generally available. Or later if the implementation misses that deadline.

In the mean-time however, attempting to re-enable the (deprecated) should_assert_eq lint will incorrectly claim that RFC 2011 already has been implemented, and do nothing else. Playground

This is the output of clippy (also note the missing backtick).

warning: lint `clippy::should_assert_eq` has been removed: `assert!(a == b)` can now print the values the same way `assert_eq!(a, b) can

I suggest to bring back the lint as allow-by-default.

polwel avatar Jun 18 '25 10:06 polwel