-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(manual_ilog2): new lint #15865
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
feat(manual_ilog2): new lint #15865
Conversation
|
@Jarcho you might be interested in this as you reviewed the original PR |
|
r? clippy |
clippy_lints/src/manual_ilog2.rs
Outdated
| // Whether `leading_zeros` is an inherent method, i.e. doesn't come from some unrelated trait | ||
| && cx.ty_based_def(right).opt_parent(cx).is_impl(cx) |
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.
I believe this wouldn't catch that since a trait impl is also a DefKind::Impl. I think it's fine without the check though since inherent methods come first in method resolution
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 looks like you're right; I'll remove the call then.
The reason I wrote it that way is that that's what is_inherent_method_call was replaced with in https://github.com/rust-lang/rust-clippy/pull/15682/files#diff-9d3f62135af85bbb426d0e358666790f639f6777c8dc504a7527e2bc7f830fb1R741, and so I assumed it would only match, well, inherent method calls. @Jarcho maybe this should be a separate method, something like is_inherent_impl?
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.
TypeckResults always has things resolved to either the inherent item or the trait item, but never a trait impl item. Checking that the parent is an impl is enough because of this.
This comment has been minimized.
This comment has been minimized.
clippy_lints/src/manual_ilog2.rs
Outdated
| && ilog.ident.name == sym::ilog | ||
| && let ExprKind::Lit(lit) = two.kind | ||
| && let LitKind::Int(Pu128(2), _) = lit.node | ||
| && cx.typeck_results().expr_ty(recv).is_integral() |
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.
This should be the adjusted type. You want the type passed in to the method.
This comment has been minimized.
This comment has been minimized.
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.
Could be a separate PR, but it'd be quite easy to also lint x.checked_ilog(2) into x.checked_ilog2()
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
ac35c0e to
d9cc76a
Compare
clippy_lints/src/manual_ilog2.rs
Outdated
| /// Checks for expressions like `31 - x.leading_zeros()` which are manual | ||
| /// reimplementations of `x.ilog2()` |
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.
I think it's worth mentioning the type here so that it's clear the 31 is for u32 only
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.
Added a (somewhat wordy) note
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.
Thanks!
|
I was looking at the wrong diff |
Revival of #13331
Part of #12894
changelog: [
manual_ilog2]: new lint