rust-lang / rust-lang/rust-clippy
Detect bitwise negation with `!` for integral types
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Make it possible to detect bitwise negation with ! applied to integral types other than bool.
Potential variant - use separate checks for signed and unsigned types. Binary negation for signed types is more likely to be a problem.
I made a dylint lint, it could be used as the base for the clippy implementation:
https://github.com/proski/no_integer_negation
Advantage
Most other programming languages use ! as logical negation. ! can be very misleading and cause hard to find bugs, especially when converting code from a different programming language. There are other ways to negate integers.
Drawbacks
Obviously, the existing code can be using the bitwise negation as intended. Clippy should provide a good suggestion for a replacement.
Example
let flag = 1i32;
let negative_flag = !flag;
Could be written as:
use std::ops::Not;
let flag = 1i32;
let negative_flag = flag.not();
Comparision with existing lints
I'm not aware of similar lints. However, #12916 is somewhat related.
Additional Context
The feature was previously discussed at https://users.rust-lang.org/t/can-i-disable-the-bitwise-negation-operator-for-a-crate/80567/17
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the proposed implementation in the linked proski/no_integer_negation dylint and the related discussion in issue #12916. Define how the lint should distinguish bool, signed, and unsigned integral types, and decide what replacement suggestion is appropriate; done means the examples are diagnosed with a clear suggestion while intentional bitwise negation remains explainable.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100