C and C++


LANG.FUNCS.ICS : Implicit Constructor Shadowing

Summary

The definition of a constructor for class or struct C has all of the following properties.

A constructor with these properties is a copy constructor for C.

Properties

Class Name Implicit Constructor Shadowing
Significance style
Mnemonic LANG.FUNCS.ICS
Categories
MisraC++2023 MisraC++2023:15.0.2 User-provided copy and move member functions of a class should have appropriate signatures
CWE CWE:1076 Insufficient Adherence to Expected Conventions
JSF++ JSF++:77.1 The definition of a member function shall not contain default arguments that produce a signature identical to that of the implicitly-declared copy constructor for the corresponding class/structure.
Availability Available for C++ only (not C).
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Implicit Constructor Shadowing"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

struct NoImpl {
    // This is a defining declaration for the copy constructor,
    // so there is no implicitly defined copy constructor.
    NoImpl(const NoImpl& x, int i=0){}                       // 'Implicit Constructor Shadowing' warning issued here
};

struct ImplShadowed {
    // There IS an implicitly defined copy constructor for this class,
    // because this declaration does not indicate the defaulted parameter.
    ImplShadowed(const ImplShadowed& x, int i);
};

// Definition matches signature of copy constructor, even though declaration doesn't.
// (parse error)
ImplShadowed::ImplShadowed(const ImplShadowed& x, int i=0){} // 'Implicit Constructor Shadowing' warning issued here

struct ImplOk {
    // There IS an implicitly defined copy constructor for this class,
    // but it is not shadowed by the definition of this constructor.
    ImplOk(const ImplOk& x, int i);                                    // Ok: no user confusion or parse issues. 
};

// Constructor definition does not have type signature matching that of the copy constructor.
ImplOk::ImplOk(const ImplOk& x, int i){}  

void my_init(NoImpl x, ImplShadowed y, ImplOk z){
    // Calls the explicitly declared/defined copy constructor.
    // This may not be what a human reader expects.
    auto ni_copy = NoImpl(x);            

    // Resolution conflict: matches both implicitly defined copy constructor and explicit definition.
    // (parse error)
    auto ishadow_copy = ImplShadowed(y);  

    // Calls the implicitly declared/defined copy constructor.
    auto iok_copy = ImplOk(z);            
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.