This code issues a warning: g++ -Wall int i = 5 + i; warning: ‘i’ may be used uninitialized in this function This code does not: string s = string("str") + s; Neither this: string s(string("str") + s); Shouldnt the 2 last ones issue warnings too?
The string case calls a function (the overloaded operator+ or std::string) so is actually closer to: int f(int); int i = f(i); which doesn't warn either (although it should do, ideally) This is similar to PR 48483, maybe even a dup.
(In reply to matferib from comment #0) > This code issues a warning: > > g++ -Wall > int i = 5 + i; > warning: ‘i’ may be used uninitialized in this function > > This code does not: > string s = string("str") + s; > > Neither this: > string s(string("str") + s); > > Shouldnt the 2 last ones issue warnings too? (In reply to Jonathan Wakely from comment #1) > The string case calls a function (the overloaded operator+ or std::string) > so is actually closer to: > > int f(int); > int i = f(i); > > which doesn't warn either (although it should do, ideally) > > This is similar to PR 48483, maybe even a dup. So, I combined all the snippets into a single testcase, and g++ doesn't even warn on the first one anymore: $ cat 48829.cc #include <string> using namespace std; int i = 5 + i; string s = string("str") + s; string ss(string("str") + ss); int f(int); int ii = f(ii); $ /usr/local/bin/g++ -c -Wall -Wextra -pedantic -Wuninitialized -Winit-self -Weffc++ -O2 48829.cc $ Same with all other optimization levels I tried. So, confirmed.
Is the expectation here for the warning to come from -Wuninitialized, -Winit-self, or some other flag?
I would expect the expression 5 + i to give a -Wuninitialized warning.
Well, that one already does. So I'd expect std::string("str") + s and f(i) to do so as well.
(In reply to Jonathan Wakely from comment #4) > I would expect the expression 5 + i to give a -Wuninitialized warning. ok, making this block the -Wuninitialized meta-bug then