Skip to main content

IAR Embedded Workbench for RX 5.20

RED-self-assign

In this section:
Synopsis

In a C++ class member function, a variable is assigned to itself.

Enabled by default

Yes

Severity/Certainty

Low/High

lowhigh.png
Full description

In a C++ class member function, a variable is assigned to itself. This error might be harder to identify than in an ordinary C function, because variables might be qualified by this, and thus refer to class members.

Coding standards
CWE 480

Use of Incorrect Operator

Code examples

The following code example fails the check and will give a warning:

class A {
public :
  int x;
  void f(void) { this->x = x; }  //self-assignment
};

int main(void) {
  A *a = new A();
  a->f();
  return 0;
}

The following code example passes the check and will not give a warning about this issue:

class A {
public :
  int x,y;
  void f(void) { this->x = y; }
};

int main(void) {
  A *a = new A();
  a->f();
  return 0;
}