-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Detect redundant always successful assert_eq!
#8567
Comments
Mh, sounds kinda hard, because when there is any kind of mutability or randomness involved, we can still end up with identical expressions that evaluate to completely different things :/ assert_eq!(
rand::thread_rng().gen::<u8>(),
rand::thread_rng().gen::<u8>()
) |
For sure if the expression use global mutable state it doesn't work, I was thinking of it primarily for
|
when |
@lucarlig In more complex cases of non-deterministic functions it might be useful to run the same function twice and assert that the result is the same. For example, testing that a function that internally memoizes some intermediate result gives the same output before and after memoization has kicked in. |
I think this could be a useful lint cause Rust allows shadowing variables, and it's too easy to forget the right name (especially after refactoring). This is a bug I found in tests that led me to this issue:
Not sure why not make this lint work simply for all cases, when the left and right sides are the same, and leave other corner cases to be enabled/disabled as usual per line/file. |
Makes sense @rustbot claim |
i think some parts of this lints overlap with eq_op lint, does it make sense to implement it as separate lint or expand that? |
Related: #13725 |
This should be partially covered by #13828, see the tests results in https://github.com/rust-lang/rust-clippy/actions/runs/12331203231/job/34417532284?pr=13828 Only that lint explicitly ignores
|
What it does
Detect if
assert_eq!
always returns succeeds because the same expression is on the left and right side, such as withassert_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:
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:
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 should simply be removed as it has no effect.
The text was updated successfully, but these errors were encountered: