Bitwise vs logical negation use the same symbol

Hey,
I just found a bug in a project where, instead of doing:

// Making sure that the length of a is equal to 1
if a.len() != 1 {
    return Err(...);
}

they did:

// Making sure that the length of a is equal to 1
if !a.len() == 1 {
    return Err(...);
}

!a.len() returned the bitwise negation of a, effectively never returning the error unless a.len() == !1_usize.
(Neither the compiler nor cargo clippy return a warning, so no one noticed until now)

I was wondering, has it ever been discussed to introduce a different symbol for bitwise negation, such as ~ in C?

Rust is stable and tries to avoid churn, so deprecation and introduction of a new operator is unlikely.

However, this could probably be fixed with a warning.

1 Like

This was suggested as a clippy lint in this issue.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.