Skip to content
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

Make SpanlessEq more consistent #10267

Closed
flip1995 opened this issue Jan 31, 2023 · 9 comments
Closed

Make SpanlessEq more consistent #10267

flip1995 opened this issue Jan 31, 2023 · 9 comments
Assignees
Labels
E-help-wanted Call for participation: Help is requested to fix this issue. good-first-issue These issues are a good way to get started with Clippy

Comments

@flip1995
Copy link
Member

flip1995 commented Jan 31, 2023

Description

The SpanlessEq::eq_expr implementation has a wildcard match arm that just returns false.

This is inconsistent with SpanlessHash, which implements hashing for each expressions type.

ExprKind::Unary(lop, le) => {
std::mem::discriminant(&lop).hash(&mut self.s);
self.hash_expr(le);
},
}

PartialEq and Hash implementations have to be consistent. The same should apply to SpanlessEq and SpanlessHash

Version

rustc 1.69.0-nightly (d7948c843 2023-01-26)
binary: rustc
commit-hash: d7948c843de94245c794e8c63dd4301a78bb5ba3
commit-date: 2023-01-26
host: x86_64-unknown-linux-gnu
release: 1.69.0-nightly
LLVM version: 15.0.7

Additional Labels

No response

@flip1995 flip1995 added good-first-issue These issues are a good way to get started with Clippy E-help-wanted Call for participation: Help is requested to fix this issue. labels Jan 31, 2023
@llogiq
Copy link
Contributor

llogiq commented Feb 1, 2023

Note that SpanlessEq is used in eq_op, so changing the default to true would introduce false positives there whenever new expr variants are added.

Also the consistency between Hash and Eq goes only one way: Two equal values must hash to the same hash, but two unequal values may hash to the same hash.

So I'd suggest seeking further consistency here is misguided. Perhaps we should add a comment stating that fact.

@flip1995
Copy link
Member Author

flip1995 commented Feb 1, 2023

so changing the default to true would introduce false positives there whenever new expr variants are added.

I don't suggest changing the default. We should remove the wildcard completely. My main motivation for this is, that then, when new ExprKinds are added in rustc, contributors are forced to also deal with this in eq_expr. Currently only hash_expr is updated.

I'm fine with as a first step to do a NFC and replace the wildcard with all missing expr kinds and keep => false for them.


uh, removing the wildcard completely might not even be possible, because we have to deal with different ExprKinds that are never equal.

@Jarcho
Copy link
Contributor

Jarcho commented Feb 1, 2023

Probably worth having the final pattern be (ExprKind::A | ExprKind::B | ..., _). At least that way adding a new variant will cause an error.

@Yoric
Copy link

Yoric commented Feb 10, 2023

I'd be happy to pick this issue as a way to familiarize myself with clippy's code.

But

uh, removing the wildcard completely might not even be possible, because we have to deal with different ExprKinds that are never equal.

Does this mean that this issue is actionable or not?

@flip1995
Copy link
Member Author

Does this mean that this issue is actionable or not?

What should be done is to check if every ExprKind is handled in the eq_expr implementation. And if not it should be handled.

To get rid of the wildcard, I think we can try Jarchos suggestion.

@Yoric
Copy link

Yoric commented Feb 10, 2023

I'll take a look later today.

Yoric added a commit to Yoric/rust-clippy that referenced this issue Feb 10, 2023
- Get rid of the wildcard `_ => false` in `SpanlessEq::eq_expr`.
- Make sure that `SpanlessEq::eq_expr` branches remain in alphabetical order.
Yoric added a commit to Yoric/rust-clippy that referenced this issue Feb 10, 2023
- Get rid of the wildcard `_ => false` in `SpanlessEq::eq_expr`.
- Make sure that `SpanlessEq::eq_expr` branches remain in alphabetical order.
@gernot-ohner
Copy link

@rustbot claim

bors added a commit that referenced this issue Nov 8, 2023
Make SpanlessEq more consistent

1) Remove wildcard as requested in #10267.
2) Implement `hir_utils::eq_expr` for `ExprKind::Closure`, `ExprKind::ConstBlock`, `ExprKind::InlineAsm` and `ExprKind::Yield`.
3) Reorder branches of `hir_utils::eq_expr` to be in alphabetical order.

---
changelog: none
@gernot-ohner
Copy link

I believe that this issue can now be closed since this PR was merged #11736.
I had just removed the tag to auto-close it because I believed that there was additional work to be done (& it later turned out that there wasn't).

@flip1995
Copy link
Member Author

Thanks! Yes this is all done now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-help-wanted Call for participation: Help is requested to fix this issue. good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

No branches or pull requests

5 participants