-
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
base: master
Are you sure you want to change the base?
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()
| && self.msrv.meets(cx, msrvs::ILOG2) | ||
| && !is_from_proc_macro(cx, expr) => |
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'd move this to the start of the function, since they're really not part of the 31 - x.leading_zeros() case.
Idem below
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.
That would remove some duplication, yes, but the problem is that both of these checks are rather expensive:
- the MSRV check requires walking up through all the attributes, and I think also going through the Clippy config
- the proc macro check is very expensive I'm told (though I'm not completely sure why), and so it should generally be the last
ifof any lint
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.
These checks aren't first in general as a perf thing. They're bad filters in general and neither are fast.
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 for the explanation, sorry for the noise.
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.
All good!
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. |
Revival of #13331
Part of #12894
changelog: [
manual_ilog2]: new lint