brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.4 KiB · 02d3dde Raw
52 lines · plain
1.. title:: clang-tidy - bugprone-copy-constructor-init2 3bugprone-copy-constructor-init4==============================5 6Finds copy constructors where the constructor doesn't call the copy constructor7of the base class.8 9.. code-block:: c++10 11    class Copyable {12    public:13      Copyable() = default;14      Copyable(const Copyable &) = default;15 16      int memberToBeCopied = 0;17    };18 19    class X2 : public Copyable {20      X2(const X2 &other) {} // Copyable(other) is missing21    };22 23Also finds copy constructors where the constructor of24the base class don't have parameter.25 26.. code-block:: c++27 28    class X3 : public Copyable {29      X3(const X3 &other) : Copyable() {} // other is missing30    };31 32Failure to properly initialize base class sub-objects during copy construction33can result in undefined behavior, crashes, data corruption, or other unexpected34outcomes. The check ensures that the copy constructor of a derived class35properly calls the copy constructor of the base class, helping to prevent bugs36and improve code quality.37 38 39Limitations40-----------41 42* It won't generate warnings for empty classes, as there are no class members43  (including base class sub-objects) to worry about.44 45* It won't generate warnings for base classes that have copy constructor46  private or deleted.47 48* It won't generate warnings for base classes that are initialized using other49  non-default constructor, as this could be intentional.50 51The check also suggests a fix-its in some cases.52