Miscellaneous-NoConstOnRHS

Put constants on left side of equality comparisons in conditions

Required inputs: IR

When a variable is compared with a constant, put the constant on the left-hand side of the comparison. This coding style is known as "Yoda conditions", and helps prevent accidental assignments when the == operator in equality comparison is mistyped as the assignment operator =.

This rule reports == comparisons where the left-hand side is a mutable lvalue, and the right-hand side is either an rvalue or a const-qualified lvalue.

Example
if (10 == value) ...

Possible Messages

Key

Text

Severity

Disabled

mut_lvalue_should_be_rhs

Put mutable lvalue operand on right side of equality comparisons.

None

False

Options

only_report_compile_time_constant

only_report_compile_time_constant : bool = False

If set, only report a violation if the right-hand side is a compile-time constant.

If this option is false, will also report violations if the right-hand side is an rvalue (e.g. value == other_value + 1) or a const-qualified lvalue (e.g. value == *ptr_to_const).