Open ivan-shrimp opened 3 months ago
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".
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
https://github.com/rust-lang/rust-clippy/pull/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
https://github.com/rust-lang/rust-clippy/pull/13327is_power_of_two
:a & (a - 1) == 0
https://github.com/rust-lang/rust-clippy/pull/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))
https://github.com/rust-lang/rust-clippy/pull/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: