Skip to content

New lint: manual reimplementation of .rem_euclid() #8883

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
xTibor opened this issue May 24, 2022 · 0 comments · Fixed by #9031
Closed

New lint: manual reimplementation of .rem_euclid() #8883

xTibor opened this issue May 24, 2022 · 0 comments · Fixed by #9031
Labels
A-lint Area: New lints

Comments

@xTibor
Copy link

xTibor commented May 24, 2022

What it does

Checks for manual reimplementations of the .rem_euclid() function.

Lint Name

No response

Category

complexity

Advantage

When people search for how to calculate positive modulos of integers, top Google results and Stack Overflow tend to feature the ((a % n) + n) % n method as a solution to the problem. Rust has a function to calculate this, .rem_euclid(). It would be great to suggest people using this function instead (only in cases where n can be proven as positive).

Drawbacks

No response

Example

let manual_1: i32 = ((value % 4) + 4) % 4;
let manual_2: i32 = (4 + (value % 4)) % 4;
let manual_3: i32 = (value % 4 + 4) % 4;
let manual_4: i32 = (4 + value % 4) % 4;

Could be written as:

let expected: i32 = value.rem_euclid(4);
@xTibor xTibor added the A-lint Area: New lints label May 24, 2022
@bors bors closed this as completed in e17864e Jun 24, 2022
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.

1 participant