llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

False positive cppcoreguidelines-pro-type-member-init for array and std::fill

Open chrchr-github opened this issue 2 years ago • 3 comments

Seems like a regression in clang-19:

#include <algorithm>
#include <iterator>

struct S {
	char a[100];
	S() {
		std::fill(std::begin(a), std::end(a), '\0');
	}
};
<source>:6:2: warning: constructor does not initialize these fields: a [cppcoreguidelines-pro-type-member-init]
    5 |         char a[100];
      |                    
      |                    {}
    6 |         S() {
      |         ^

https://godbolt.org/z/6WfzWGYr9

chrchr-github avatar Sep 06 '24 13:09 chrchr-github

@llvm/issue-subscribers-clang-tidy

Author: None (chrchr-github)

Seems like a regression in clang-19: ~~~c++ #include <algorithm> #include <iterator>

struct S { char a[100]; S() { std::fill(std::begin(a), std::end(a), '\0'); } };

<source>:6:2: warning: constructor does not initialize these fields: a [cppcoreguidelines-pro-type-member-init] 5 | char a[100]; |
| {} 6 | S() { | ^

https://godbolt.org/z/6WfzWGYr9
</details>

llvmbot avatar Sep 06 '24 14:09 llvmbot

Reduced:

#include <algorithm>
struct S {
	char a[100];
	S() {
          std::fill(a, a + 100, '\0');
    }
};

chrchr-github avatar Sep 06 '24 18:09 chrchr-github

According to the docs:

The check takes assignment of fields in the constructor body into account but generates false positives for fields initialized in methods invoked in the constructor body.

Looks like this is the intended behaviour for now in this kind of methods. this is still a very poor check IMO, hope it gets fixed in the future.

RiverDave avatar Feb 17 '25 20:02 RiverDave