Issue 134440
Summary [clang-tidy] readability-redundant-member-init fails to account for explicit user-provided default constructors
Labels clang-tidy
Assignees
Reporter connorsmacd-hh
    The `readability-redundant-member-init` check issues warnings on initializers even when the relevant default constructor is **marked `explicit`** and is **user-provided**.

Example code ([godbolt here](https://godbolt.org/z/YPqKPvGz1)) with clang-tidy args `-checks=-*,readability-redundant-member-init`:

```C++
struct A
{
 explicit A() {}
};

struct B
{
    A a1 {}; // Compiles, but tidy detects a redundant member init
    A a2; // No warning from tidy, but does not compile
};

auto b = B{};
```

In the above example, clang-tidy flags the `a1` line with:

```
<source>:8:10: warning: initializer for member 'a1' is redundant [readability-redundant-member-init]
    8 |     A a1 {}; // Compiles, but tidy detects a redundant member init
```

When I do what tidy wants in the `a2` line, this fails to compile with:

```
<source>:12:12: error: chosen constructor is explicit in copy-initialization
   12 | auto b = B{};
      | ^
<source>:3:14: note: explicit constructor declared here
    3 | explicit A() {}
      |              ^
<source>:9:7: note: in implicit initialization of field 'a2' with omitted initializer
    9 |     A a2; // No warning from tidy, but does not compile
      |       ^
1 error generated.
Compiler returned: 1
```

Interestingly, clang-tidy no longer warns if the constructor is instead explicitly defaulted, i.e., `explicit A() = default;`.

For context, I encountered this issue when working with `boost::circular_buffer`'s default constructor ([docs here](https://www.boost.org/doc/libs/1_87_0/doc/html/doxygen/boost_circular_buffer_c___reference/classboost_1_1circular__buffer.html#doxygen.boost_circular_buffer_c___reference.classboost_1_1circular__buffer_1a00b4cb2a7dd9c100008c05ba18c43575)):

```C++
explicit circular_buffer(const allocator_type & alloc = allocator_type()) noexcept;
```

It seems to me that `readability-redundant-member-init` should ignore instances where the initializer is invoking an explicit default constructor. Is that correct or am I doing something wrong?
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to