Skip to content

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

Closed
@xTibor

Description

@xTibor

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

Metadata

Metadata

Assignees

No one assigned

    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