Same Subexpression

ID

csharp.same_subexpression

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

CSharp

Tags

duplicate-code, reliability

Description

Reports a logical expression (&& or ||) whose operands include the same sub-expression more than once. Examples: a && a, a || a, a && b && a. The repeated operand is redundant and most often indicates a copy-paste mistake.

Rationale

Boolean logic short-circuits, so a duplicate operand cannot change the result. The pattern suggests the author intended a different operand the second time, so the rule should be treated as a likely bug rather than a stylistic concern.

if (a && a)                    // FLAW — duplicate operand
{
}

if (a || a)                    // FLAW — duplicate operand
{
}

if (a && b && a)               // FLAW — `a` appears twice
{
}

if (a && b)                    // OK — distinct operands
{
}

Remediation

Decide which operand the duplicate was meant to be. Replace it with the correct expression or, when the duplicate is genuinely redundant, drop it.