-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add lints against more manual integer ops where direct methods exist #12894
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
Comments
I'm going to start slowly working on this one, probably creating a separate PR for each new lint @rustbot claim |
Someone should reopen it, it looks like the "partially fixes #xxx" has (rightly) be understood by GitHub as "fixes #xxx". |
Add new lint `manual_is_power_of_two` Suggest using `is_power_of_two()` instead of the manual implementations for some basic cases Part of #12894 ---- changelog: new [`manual_is_power_of_two`] lint
With the stabilisation of |
What it does
Checks for more manual reimplementations of various integer operations that are available as methods. (This can, and probably should, be separated into different lints.)
An existing lint of this type is
manual_rem_euclid
, preferringrem_euclid
over((a % b) + b) % b
.div_ceil
:(a + (b - 1)) / b
[manual_div_ceil
]: init #12987checked_div
:b != 0
check beforea / b
checked_sub
:a >= b
check beforea - b
checked_ilog{|2|10}
:a > 0
check beforeilog{|2|10}
is_power_of_two
:count_ones() == 1
Add new lintmanual_is_power_of_two
#13327is_power_of_two
:a & (a - 1) == 0
Add new lintmanual_is_power_of_two
#13327ilog2
:CONST - a.leading_zeros()
ilog2
andilog10
:ilog(2)
andilog(10)
{count|leading|trailing}_{zeros|ones}
: any counterpart on!a
ora.reverse_bits()
rotate_{left|right}
:(a << n) | (a >> (BITS - n))
Implement a lint to replace manual bit rotations with rotate_left/rot… #12983(We can also add
midpoint
as replacement for the overflow-incorrect(a + b) / 2
, once that's stabilized.)Advantage
Drawbacks
Example
Could be written as:
The text was updated successfully, but these errors were encountered: