-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Replace assert!(a==b)
with assert_eq!(a,b)
as part of bool_assert_comparison
lint
#13333
base: master
Are you sure you want to change the base?
Conversation
1cc3977
to
9fd422f
Compare
does this lint on |
tests/ui/assertions_on_constants.rs
Outdated
@@ -1,4 +1,4 @@ | |||
#![allow(non_fmt_panics, clippy::needless_bool, clippy::eq_op)] | |||
#![allow(non_fmt_panics, clippy::needless_bool, clippy::eq_op, clippy::bool_assert_comparison)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, it does!
given the sheer amount of pushback you gave me when you had to reorganize your code for linting reasons, I find it frankly astonishing that you would introduce a lint that forces every user of const fn
to do the exact same thing you were complaining about having to do: rearchitect their code purely for linting reasons if they want this lint to remain useful for their code that doesn't use const assertions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol, worry not, this lint needs feedback. So i guess you point is that i should use is_in_const_context
check in this lint so that it doesn't get triggered for const context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that seems like it would be sufficient, I think?
rustc has reliably gotten the user feedback of "these two things should not be the same lint" for basically every single lint expansion into a new category of code patterns. the fact that this new lint on some instances of |
any suggestions for the new lint name? And yes, if this (or any other) lint shows when its not applicable, its a false positive and should be fixed |
why not just |
Perhaps. Usecase could be |
While I very much like this idea, Clippy had a lint for this before. See #2156, especially the issues/PRs referenced at the bottom. |
Reading the other discussion, it seems there was hope that |
That's a good question. I've created https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/New.20lint.20or.20expand.20.60bool_assert_comparison.60.20rust-clippy.231333/near/468909875 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent start, I left comments, mostly related to your TODO comments :D
// TODO: is `cond.span.from_expansion()` correct / needed? | ||
if (node != BinOpKind::Eq && node != BinOpKind::Ne) || cond.span.from_expansion() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// TODO: is
cond.span.from_expansion()
correct / needed?
It should be correct 👍
|
||
let new_name = format!("{macro_name}_{}", if node == BinOpKind::Eq { "eq" } else { "ne" }); | ||
let msg = format!("replace it with `{new_name}!(..)`"); | ||
span_lint_and_then( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is_in_const_context
should be checked some time before here, and it would be good to have tests for it.
|diag| { | ||
let macro_name_span = cx.sess().source_map().span_until_char(macro_call.span, '!'); | ||
// TODO: should this be `cond.span`, expanded to include preceding whitespace? If so, how? | ||
let equality_span = Span::new(lhs.span.hi(), rhs.span.lo(), span.ctxt(), span.parent()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We know that the ==
actually comes from the source code, so I would probably use cond.span.with_lo(lhs.span.hi()).with_high(rhs.span.lo())
. But this should result in the same span, so either way should be fine :)
9fd422f
to
8f4d34e
Compare
Expand lint `bool_assert_comparison` to catch `assert!(a == b)` and `assert!(a != b)` (or their debug versions), and suggest to replace them with the corresponding `assert_eq!(a, b)`.
8f4d34e
to
42eb926
Compare
Expand lint
bool_assert_comparison
to catchassert!(a == b)
andassert!(a != b)
(or their debug versions), and suggest to replace them with the correspondingassert_eq!(a, b)
orassert_ne!(a, b)
.TODO:
bool_assert_comparison
or be a new lint (name?). Note that existing lint description fits well to the new functionality.style
? Note that IntelliJ has a similar lint built-in, and it shows up similar to styling guide.Closes #13252
changelog: [
bool_assert_comparison
]: now also convertsassert!(a==b)
intoassert_eq!(a,b)
and similar