Skip to content
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

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

Open
Urgau opened this issue Dec 17, 2024 · 1 comment · May be fixed by #13851
Open

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

Urgau opened this issue Dec 17, 2024 · 1 comment · May be 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

@samueltardieu samueltardieu linked a pull request Dec 17, 2024 that will close this issue
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