<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/59847>59847</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang-tidy] Inconsistent behavior for cppcoreguidelines-pro-type-member-init check
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          MaxWipfli
      </td>
    </tr>
</table>

<pre>
    `clang-tidy` has inconsistent behavior on the `cppcoreguidelines-pro-type-member-init`. See the following example.

---

In all cases, the `class Foo` contains a `char m_first` member variable, which is left uninitialized by the default constructor. (I have added an accessor for `m_first` such that it would be reachable from arbitrary code outside this file, since I wasn't sure if this affects the results.)

**Case 1**

```cpp
class Foo {
public:
    char first() const { return m_first; }
private:
 char m_first;
};
```

```console
$ clang-tidy -checks='cppcoreguidelines-*' foo.hpp
Error while trying to load a compilation database:
[...]
Running without flags.
[end of clang-tidy execution]
```

Thus, the uninitialized member is not detected by `clang-tidy`.

**Case 2 (with uninitialized second member)**
```cpp
class Foo {
public:
    char first() const { return m_first; }
private:
    char m_first;
    char m_second;
};
```
```console
$ clang-tidy -checks='cppcoreguidelines-*' foo.hpp
Error while trying to load a compilation database:
[...]
Running without flags.
[end of clang-tidy execution]
```
Nothing changes, still not detected.

**Case 3 (with default-initialized second member)**
```cpp
class Foo {
public:
    char first() const { return m_first; }
private:
    char m_first;
    char m_second { 0 };
};
```
```console
$ clang-tidy -checks='cppcoreguidelines-*' foo.hpp
Error while trying to load a compilation database:
[...]
Running without flags.
1 warning generated.
/home/max/Downloads/foo.hpp:1:7: warning: constructor does not initialize these fields: m_first [cppcoreguidelines-pro-type-member-init]
class Foo {
 ^
```
Now, the error is detected.

**Case 4 (with explicitly declared `= default` constructor)**
```cpp
class Foo {
public:
 Foo() = default;
    char first() const { return m_first; }
private:
    char m_first;
    char m_second;
};
```
```console
$ clang-tidy -checks='cppcoreguidelines-*' foo.hpp
Error while trying to load a compilation database:
[...]
Running without flags.
[end of clang-tidy execution]
```
Not detected.


**Case 5 (with explicitly declared `Foo() {}` constructor)**
```cpp
class Foo {
public:
    Foo() {}
    char first() const { return m_first; }
private:
    char m_first;
    char m_second;
};
```
```console
$ clang-tidy -checks='cppcoreguidelines-*' foo.hpp
Error while trying to load a compilation database:
[...]
Running without flags.
1 warning generated.
/home/max/Downloads/foo.hpp:3:5: warning: constructor does not initialize these fields: m_first [cppcoreguidelines-pro-type-member-init]
 Foo() {}
    ^
```
Now it is detected (presumably because this is not "the default constructor", and thus handled differently.

**Summary**
The uninitialized member `m_first` is only detected if either
* there is another, default-initialized member in the same class, or
* there is a explicitly defined `Foo () {}` default constructor (`= default` is not enough).

----

I am using `clang-tidy` version 15.0.6, downloaded from the release page of this GitHub repository.
```console
$ clang-tidy --version
LLVM (http://llvm.org/):
  LLVM version 15.0.6
  Optimized build.
 Default target: x86_64-unknown-linux-gnu
  Host CPU: tigerlake
``` 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWFtv27AV_jX0y4EFmfL1wQ9JXK8B2m1Yu-2xOKKOJK4UKfASx_v1A3Vx7MRp81CgRVfASGTz8Fy_84mH6JysNNGWLW7ZYjfB4Gtjtx_x8d-yLZWc5KY4btkyFQp1NfWyOLJlCjU6kFoY7aTzpD3kVOODNBaMBl8TxB1tK4ylKsiClNTkpq01U39sadpQk5OdSi09W6YJfCLqdpVGKXOQugJ6xKZVlLB0x9Kb_u90Oj3_eq8BlQKBjhzjdyezCp2DvTHRT2G0R6kdYLdUo4XmSymti3ahdwMe0ErMFUUlh1qKGqQDRaWHoKOLEpX8LxWQHzsbBZUYlI-6nbdBeGMTYHx9DzU-EGBRUAGoAYUg54yF0tho_cywC6IGX6MH6eFggiogJ7CEoo6OQGlNA2hz6S3aIwhTEJjgnSxinqSDUvbuOqkFwT0c0GnGVx5csASy7KWwLEl413ltyQXlXc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