Comparison against the unit value is always true or always false
ID |
rust.correctness.unit_values_compared |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Suspicious Construct |
Language |
Rust |
Rationale
The unit type has exactly one value, so x == () is always true and x != () is always false regardless of what x did. This almost always means a method that returns () was called where a value-returning one was intended - sort instead of a sorted copy, push instead of a result - so the check that looks like it guards the code actually guards nothing, and the real outcome is never examined.
The following code illustrates the pattern detected by this rule:
pub fn sorted(values: &mut Vec<i32>) -> bool {
// FLAGGED: Comparison against the unit value is always true or always false
if values.sort() == () {
return true;
}
// FLAGGED: Comparison against the unit value is always true or always false
if () != values.clear() {
return true;
}
false
}