rust-lang/rust-clippy

useless op on lhs of integer division or modulo

Open

#1.324 geöffnet am 7. Nov. 2016

Auf GitHub ansehen
 (2 Kommentare) (0 Reaktionen) (1 zugewiesene Person)Rust (1.391 Forks)batch import
A-lintL-correctnessL-unnecessaryT-middlegood first issue

Repository-Metriken

Stars
 (10.406 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 19T 22h) (113 gemergte PRs in 30 T)

Beschreibung

(x + n + y) % n or (x + n + y) / n can simply be replaced by (x + y) % n or (x + y) / n + 1

this isn't true for the following computations on unsigned integers:

(x + n - y) % n or (x + n - y) / n

as they might go negative due to the subtraction. For explicitly evading INT_MAX issues, this lint will cause false positives, but that could be evaded by not linting if INT_MAX is mentioned somewhere near the operation.

most likely, whenever this lint triggers, it is a bug in the equation, so the suggestion doesn't fix the underlying issue, but it will raise awareness of it.

Contributor Guide