xref: /llvm-project/clang-tools-extra/docs/clang-tidy/checks/readability/redundant-member-init.rst (revision 59e79f0de59d9e4576b6bf562de40a914702efd4)
1.. title:: clang-tidy - readability-redundant-member-init
2
3readability-redundant-member-init
4=================================
5
6Finds member initializations that are unnecessary because the same default
7constructor would be called if they were not present.
8
9Example
10-------
11
12.. code-block:: c++
13
14  // Explicitly initializing the member s and v is unnecessary.
15  class Foo {
16  public:
17    Foo() : s() {}
18
19  private:
20    std::string s;
21    std::vector<int> v {};
22  };
23
24Options
25-------
26
27.. option:: IgnoreBaseInCopyConstructors
28
29    Default is `false`.
30
31    When `true`, the check will ignore unnecessary base class initializations
32    within copy constructors, since some compilers issue warnings/errors when
33    base classes are not explicitly initialized in copy constructors. For example,
34    ``gcc`` with ``-Wextra`` or ``-Werror=extra`` issues warning or error
35    ``base class 'Bar' should be explicitly initialized in the copy constructor``
36    if ``Bar()`` were removed in the following example:
37
38.. code-block:: c++
39
40  // Explicitly initializing member s and base class Bar is unnecessary.
41  struct Foo : public Bar {
42    // Remove s() below. If IgnoreBaseInCopyConstructors!=0, keep Bar().
43    Foo(const Foo& foo) : Bar(), s() {}
44    std::string s;
45  };
46
47