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

useless op on lhs of integer division or modulo #1324

Open
oli-obk opened this issue Nov 7, 2016 · 1 comment
Open

useless op on lhs of integer division or modulo #1324

oli-obk opened this issue Nov 7, 2016 · 1 comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-correctness Lint: Belongs in the correctness lint group L-unnecessary Lint: Warn about unnecessary code T-middle Type: Probably requires verifiying types

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Nov 7, 2016

(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.

@oli-obk oli-obk added good-first-issue These issues are a good way to get started with Clippy T-middle Type: Probably requires verifiying types A-lint Area: New lints L-correctness Lint: Belongs in the correctness lint group L-unnecessary Lint: Warn about unnecessary code labels Nov 7, 2016
@llogiq
Copy link
Contributor

llogiq commented Nov 7, 2016

Only linting on unsigned integers is likely to reduce the number of false positives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-correctness Lint: Belongs in the correctness lint group L-unnecessary Lint: Warn about unnecessary code T-middle Type: Probably requires verifiying types
Projects
None yet
Development

No branches or pull requests

4 participants