Skip to content

Suggest uN::midpoint over overflow prone (a + b) / 2 expressions #13849

Closed
@Urgau

Description

@Urgau

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

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);

Metadata

Metadata

Assignees

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions