Variable assigned to itself

ID

c.correctness.self_assignment

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Redundant Operation

Language

C / C++

Description

This assignment sets a variable to itself, which has no effect. It is usually a typo (the right-hand side should be a different variable or expression) or leftover code. Remove the redundant assignment or fix the intended operand.

Rationale

This assignment sets a variable to itself, which has no effect. It is usually a typo (the right-hand side should be a different variable or expression) or leftover code. Remove the redundant assignment or fix the intended operand.

The following code illustrates the pattern detected by this rule:

// Adapted from cppcheck test/testother.cpp selfAssignment() cases.
void f(void)
{
    int x = 0;
    int y = 1;
    // FLAGGED: Variable assigned to itself
    x = x;
    x = y;
    x = 0;
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved