Skip to content

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

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

Closed
Urgau opened this issue Dec 17, 2024 · 1 comment · Fixed by #13851
Closed

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

Urgau opened this issue Dec 17, 2024 · 1 comment · Fixed by #13851
Assignees
Labels
A-lint Area: New lints

Comments

@Urgau
Copy link
Member

Urgau commented Dec 17, 2024

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);
@Urgau Urgau added the A-lint Area: New lints label Dec 17, 2024
@samueltardieu
Copy link
Contributor

Interesting

@rustbot claim

github-merge-queue bot pushed a commit that referenced this issue Feb 28, 2025
changelog: [`manual_midpoint`]: new lint

Closes #13849
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants