-
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
Add [manual_ilog2
] lint
#13331
base: master
Are you sure you want to change the base?
Add [manual_ilog2
] lint
#13331
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Jarcho (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
1de3240
to
9a9bd69
Compare
let b: u64 = 543534; | ||
let _ = b.ilog2(); | ||
|
||
let _ = 64 - b.leading_zeros(); // No 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.
Is it check // No lint
Suppose no need to check this additionally
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.
The manual ilog2 implementation is bit_width - 1 - x.leading_zeros()
so this is just making sure that any other constant besides bit_width - 1
does not lint.
☔ The latest upstream changes (presumably #13247) made this pull request unmergeable. Please resolve the merge conflicts. |
839539b
to
8107d51
Compare
☔ The latest upstream changes (presumably #12987) made this pull request unmergeable. Please resolve the merge conflicts. |
8107d51
to
fc5f870
Compare
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 will need macro checks. in_external_macro
combined with context checks should work (span.ctxt()
should be the same for all involved expressions).
Otherwise just a couple of small nits and the test looks like it needs to be re-blessed..
if !self.msrv.meets(msrvs::MANUAL_ILOG2) { | ||
return; | ||
} | ||
let mut applicability = Applicability::MachineApplicable; |
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.
It's better to check the HIR tree first.
} | ||
|
||
if let ExprKind::MethodCall(method_name, reciever, args, _) = expr.kind | ||
&& method_name.ident.as_str() == "ilog" |
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.
The string check should be moved to after others. It requires locking the symbol table.
} | ||
} | ||
|
||
if let ExprKind::MethodCall(method_name, reciever, args, _) = expr.kind |
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.
Use [arg]
instead to save the length check later.
&& args.len() == 1 | ||
&& let ExprKind::Lit(lit) = args[0].kind | ||
&& let LitKind::Int(Pu128(2), _) = lit.node | ||
&& cx.typeck_results().expr_ty(reciever).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 a check for an inherent method rather than just a type check (could be a trait function with the same name). You can add is_inherent_method_call
.
&& let ExprKind::Lit(lit) = left.kind | ||
&& let LitKind::Int(Pu128(val), _) = lit.node | ||
&& let ExprKind::MethodCall(method_name, reciever, _, _) = right.kind | ||
&& method_name.ident.as_str() == "leading_zeros" |
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 will also need a check for an inherent method.
☔ The latest upstream changes (presumably #13322) made this pull request unmergeable. Please resolve the merge conflicts. |
a0ba2b5
to
bd65f17
Compare
☔ The latest upstream changes (presumably #13334) made this pull request unmergeable. Please resolve the merge conflicts. |
changelog: new lint: [
manual_ilog2
]Suggest using
ilog2()
instead of the manual implementations for some basic casesPart of #12894