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

Description

Detects an equality or inequality comparison against the unit literal ().

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
}

Remediation

Check the value that matters instead of the unit: call the method that returns a result, or inspect the collection afterwards. If the call really is only performed for its side effect, drop the comparison and call it as a statement.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved