Side effect in the right-hand operand of && or ||
ID |
c.maintainability.side_effect_in_short_circuit |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Control Flow |
Language |
C / C++ |
Description
Reports an increment, decrement, or assignment in the right-hand operand of a && or || expression.
Rationale
&& and || short-circuit: the right-hand operand is evaluated only when the left-hand one doesn’t already decide the result — && skips it once the left side is false, || skips it once the left side is true. A side effect placed there therefore runs conditionally, not on every evaluation of the expression, which is easy to misread as unconditional and easy to break the moment the left-hand condition changes.
The following code illustrates the pattern detected by this rule:
int count = 0;
bool ready = true;
int total = 0;
void increment_maybe_skipped()
{
// FLAGGED: Side effect in the right-hand operand of && or ||
if (ready && count++) {
total = count;
}
}