Expression with identical operands
ID |
c.maintainability.redundant_expression |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Redundant Operation |
Language |
C / C++ |
Description
Both operands of this expression are identical, so the result is constant (e.g. x == x is always true, x - x is always zero). This is usually a typo where one operand should differ. Fix the intended operand or remove the redundant expression.
Rationale
Both operands of this expression are identical, so the result is constant (e.g. x == x is always true, x - x is always zero). This is usually a typo where one operand should differ. Fix the intended operand or remove the redundant expression.
The following code illustrates the pattern detected by this rule:
int TestSimpleEquivalent(int X, int Y) {
// FLAGGED: Expression with identical operands
if (X - X) return 1;
// FLAGGED: Expression with identical operands
if (X == X) return 1;
// FLAGGED: Expression with identical operands
if (X != X) return 1;
// FLAGGED: Expression with identical operands
if (X && X) return 1;
// FLAGGED: Expression with identical operands
if (X || X) return 1;