My team just discovered (via a typo) that the rust ! operator performs a bitwise-negation for integer inputs. This is rarely what we're looking for, and so what we're really like is a way to either disable bitwise-negation completely for our crate, or maybe have a clippy or similar lint to warn/error on this.
In the case we actually did want bitwise negation, we could explicitly write out .not() using the std::ops::Not trait.
Does anyone know if this is possible? I looked through the clippy lints but didn't see this as an option.
This is sort of like the clippy::arithmetic restriction, but unfortunately that doesn't count bitwise negation (presumably since its main purpose is to avoid panics, and bitwise negation can't panic) so it can't be configured into doing what you want.
Indeed. As taught to us in school aged 13 or so back in the day.
A Boolean variable and a "bit" are the same thing as far as I can tell. TRUE or FALSE, HIGH or LOW, 0 or 1. The essence of the idea is that it is the minimum unit of information. Which we can of course represent in many ways.
Anyway, I have never seen a negation in Boolean Algebra. There is a "not" operator but that is not a negation.
Thank you all for your thoughts about terminology and boolean algebra. It seems that the answer is that this there doesn't exist a clippy lint for this already. I will consider submitting this as a proposed lint (and maybe a PR).
One way to "disable" the !-operator is to newtype all your domain-specific types. Don't use the builtin types (especially the numeric ones) directly, and don't implement std::ops::Deref for the newtypes either.
That will prevent any kind of operator usage on such types that aren't explicitly implemented, and it's stronger than a lint since the code won't compile with undesirable operators mixed in.
Can you say more about the context in which you hit this and it wasn't a compilation error? Critically, the easy "I came from C" mistake of writing if !x to check if x != 0 doesn't compile in Rust, thanks to Rust not having truthiness.
(That's why Rust doesn't need the !-vs-~ distinction that's common in other languages.)