You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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, preferring rem_euclid over ((a % b) + b) % b.
Alexendoo
changed the title
Add lint against more manual integer ops where direct methods exist
Add lints against more manual integer ops where direct methods exist
Jun 6, 2024
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
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: