Qt-Generic-InitializeAllFieldsInConstructor

A constructor must initialize all data members of the class

Required inputs: IR

If a constructor leaves data members uninitialized, accessing those data members causes undefined behavior. This rule enforces that all data members are explicitly initialized in constructors. Uninitialized data contains garbage values, potentially leading to crashes, incorrect results, or security vulnerabilities.
Bad code (uninitialized member):
struct Widget {
    int width;
    int height;
    Widget() {
        width = 100;
        // ERROR: height not initialized
    }
};

void Use() {
    Widget w;
    int area = w.width * w.height;  // height is undefined garbage value
}
Bad code (compiler generated default constructor):
struct Person {
    std::string name;
    int age;
};

void Use() {
    Person p;
    std::cout << p.name << ": " << p.age << std::endl;
    // ERROR: p.age is uninitialized, undefined behavior
}
Good code (all members initialized):
struct Widget {
    int width;
    int height;
    Widget() {
        width = 100;
        height = 50;  // OK: all members initialized
    }
};

void Use() {
    Widget w;
    int area = w.width * w.height;  // Predictable result
}
Good code (using member initializer list):
struct Widget {
    int width;
    int height;
    Widget() : width(100), height(50) {}  // OK: member initializer list
};
Good code (non-static data member initializer):
struct Person {
    std::string name;
    int age = 0;
};

void Use() {
    Person p;
    std::cout << p.name << ": " << p.age << std::endl;
    // OK: p.age is initialized
}
Configuration notes:

If the configuration option report_missing_field_constructors is enabled, this rule will also warn when fields are implicitly initialized using their default constructor. If the configuration option report_missing_base_constructors is enabled, this rule will warn when a base class is implicitly initialized using its default constructor.

Possible Messages

Key

Text

Severity

Disabled

default_ctor_missing_fields

Compiler-generated constructor leaves some fields uninitialized.

None

False

implicit_field_init

Field is only implicitly initialized in constructor.

None

False

missing_base_class_init

Base class is not explicitly initialized in constructor.

None

False

missing_field_init

Field is not initialized in constructor.

None

False

Options

exclude_aggregates

exclude_aggregates : bool = False

Whether to exclude constructors of aggregate types.
 

include_empty_classes

include_empty_classes : bool = True

Whether initializations should be enforced for classes without non-static data members.
 

include_pure_virtual

include_pure_virtual : bool = True

Whether initialization should be enforced for base classes that contain only pure virtual methods (and compiler-generated methods if applicable).
 

init_functions

init_functions : set[bauhaus.analysis.config.FunctionName] = {'Init', 'init'}

Names of functions to be inspected as well when called directly from constructor.
 

inspect_directly_called_methods

inspect_directly_called_methods : bool = True

Inspect all methods directly called from constructor.
 

only_member_initializer_list

only_member_initializer_list : bool = False

Only inspect member initializer list and not the constructor body/methods.
 

report_missing_base_constructors

report_missing_base_constructors : bool = False

Enables detection of constructors which rely on implicit base constructor calls.
 

report_missing_field_constructors

report_missing_field_constructors : bool = False

Enables detection of constructors which rely on implicit field constructor calls.