CL.ASSIGN.VOID

Void returned in assign operator=

This is a class-level (CL) checker that notifies you of a potentially limiting or unwise design choice in the parameter type of the assignment operator. Class-level checkers produce recommendations based on Scott Meyer's rules for effective C++ class construction.

The CL.ASSIGN.VOID checker looks for classes that contain assignment operators that return voids. When such a return type is used, certain language constructs are impossible to express. Whether those language constructs are desirable or otherwise is debatable, but following the basic template below guarantees that language consistency can be maintained regardless of type:

Copy
  class MyClass {
  public:
    MyClass& operator=(const MyClass&);
  };

Vulnerability and risk

There is no vulnerability exposed through this design choice, but the risk is that unfamiliar programmers will attempt to use language constructs that "should" work but are incompatible, and be faced with compiler warnings that may make little or no sense (given the usual complexity of C++ compiler output).

Vulnerable code example

Copy
    class C{
//...
    public:
      C(int);
      void operator=(const C&);
//...  
    }; 

In this example Klocwork finds a CL.ASSIGN.VOID error in line 5. The Klocwork warning allows you to re-assess the code and avoid a limiting design.

Fixed code example

Copy
    class C{
//...
      C& operator=(const C&);
//...  
    }; 

In the fixed example, the code avoids returning a void and complies with the class-construction rule.