Closed
Description
What it does
This lint is would suggest rewrite the not quite correct (a + b) / 2
expression to the (recently stabilized) uN::midpoint
/iN::midpoint
/fN::midpoint
functions, in order to prevent overflow of a + b
, which the midpoint
functions handle appropriately.
Advantage
- Eliminate a potential source of overflow
- Java’s binary search implementation and Mozilla had the overflow bug
- Bring awareness to the surprisingly subtleness of midpoint
Drawbacks
- Introduce a function call (but at the same the implementation is very small and the functions are inlined 🤷)
Example
let a = 10_i32;
let b = 10_000_i32;
let mid = (a + b) / 2;
Should be written as:
let a = 10_i32;
let b = 10_000_i32;
let mid = a.midpoint(b);