rust-lang / rust-lang/rust-clippy

Detect redundant always successful `assert_eq!`

Open
#8,567 9 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Detect if assert_eq! always returns succeeds because the same expression is on the left and right side, such as with assert_eq!(a,a).

These type of cases can happen when refactoring and search'n'replacing code, for example we've recently saw a case of this recently:

   assert_eq!(mem::size_of::<usize>(), mem::size_of::<usize>());

It would however be important that it only lints on the expressions being exactly identical, so if using a type alias in the above example that should not trigger this lint, example:

   type MyType = usize;
   assert_eq!(mem::size_of::<usize>(), mem::size_of::<MyType>());
Lint Name

redundant_assert

Category

pedantic

Advantage

Keeping an assert that has no effect is a code smell and negative value as can leave a false sense of safety in tests / coverage and is additional cognitive overhead without any positive effects.

Drawbacks

Likely none.

Probably not that common occurrence, but does happen.

Example
assert_eq!(mem::size_of::<usize>(), mem::size_of::<usize>());

Assert should simply be removed as it has no effect.

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

Begin by locating existing assert_eq! lint handling and test coverage in rust-clippy; the issue names the intended lint as redundant_assert. Confirm that it reports only exactly identical expressions, not equivalent expressions using a type alias, and that the redundant assertion can be removed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
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.