-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Closed
Labels
Description
This code:
#include <iostream>
#include <string>
struct Structy {
std::string foo;
int bar;
};
int main() {
auto *s = new Structy{
.foo = "foo",
.bar = 1,
};
std::cout << s->foo << s->bar;
delete s;
}produces the following static analyzer warning:
$ clang-tidy -p . qq.cpp
1 warning generated.
/home/rojer/allterco/shelly-ng3/qq.cpp:14:3: error: 1st function call argument is an uninitialized value [clang-analyzer-core.CallAndMessage,-warnings-as-errors]
14 | std::cout << s->foo << s->bar;
| ^ ~~~~~~
/home/rojer/allterco/shelly-ng3/qq.cpp:10:13: note: Uninitialized value stored to field 'bar'
10 | auto *s = new Structy{
| ^~~~~~~~~~~~
11 | .foo = "foo",
| ~~~~~~~~~~~~~
12 | .bar = 1,
| ~~~~~~~~~
13 | };
| ~
/home/rojer/allterco/shelly-ng3/qq.cpp:14:3: note: 1st function call argument is an uninitialized value
14 | std::cout << s->foo << s->bar;
| ^ ~~~~~~
1 warning treated as error
s->bar is of course explicitly initialized to 1, so the generated warning is a false-positive.
swapping field order to bar, foo makes it go away.
tested with latest main (20.0.0git ec353b7).