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

modulo_arithmetic should not trigger if result is only checked to be zero or non-zero #12006

Closed
ronnodas opened this issue Dec 24, 2023 · 1 comment · Fixed by #12178
Closed
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@ronnodas
Copy link

ronnodas commented Dec 24, 2023

Summary

A common usage of % is to check exact divisibility, and I believe % and rem_euclid output 0 for exactly the same set of inputs. So a % b == 0 or a % b != 0 should not trigger the lint.

Lint Name

modulo_arithmetic

Reproducer

I tried this code:

let x = 12_isize;
let y = -4_isize;
if x % y == 0{
    println!("{x} is divisible by {y}");
}

I saw this happen: The lint modulo_arithmetic triggers and suggests using rem_euclid.

I expected to see this happen: Nothing.

Version

rustc 1.74.1 (a28077b28 2023-12-04)
binary: rustc
commit-hash: a28077b28a02b92985b3a3faecf92813155f1ea1
commit-date: 2023-12-04
host: x86_64-unknown-linux-gnu
release: 1.74.1
LLVM version: 17.0.4

Additional Labels

No response

@ronnodas ronnodas added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 24, 2023
@mdm
Copy link
Contributor

mdm commented Jan 12, 2024

@rustbot claim

bors added a commit that referenced this issue Jan 27, 2024
…ogiq

Don't warn about modulo arithmetic when comparing to zero

closes #12006

By default, don't warn about modulo arithmetic when comparing to zero. This behavior is configurable via `clippy.toml`.

See discussion [on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/.E2.9C.94.20Is.20issue.20.2312006.20worth.20implementing.3F)

changelog: [`modulo_arithmetic`]: By default don't lint when comparing the result of a modulo operation to zero.
@bors bors closed this as completed in 855aa08 Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants