Bug 48829 - g++ no warning initializing a variable using itself
Summary: g++ no warning initializing a variable using itself
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.4.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Wuninitialized 52167 101850
  Show dependency treegraph
 
Reported: 2011-04-29 22:02 UTC by matferib
Modified: 2021-08-11 05:15 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-08-18 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description matferib 2011-04-29 22:02:57 UTC
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?
Comment 1 Jonathan Wakely 2011-04-30 19:52:08 UTC
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.
Comment 2 Eric Gallager 2017-08-18 15:54:31 UTC
(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.
Comment 3 Eric Gallager 2018-11-20 13:02:09 UTC
Is the expectation here for the warning to come from -Wuninitialized, -Winit-self, or some other flag?
Comment 4 Jonathan Wakely 2018-11-21 10:10:52 UTC
I would expect the expression 5 + i to give a -Wuninitialized warning.
Comment 5 Jonathan Wakely 2018-11-21 10:11:41 UTC
Well, that one already does. So I'd expect std::string("str") + s and f(i) to do so as well.
Comment 6 Eric Gallager 2019-11-21 09:00:53 UTC
(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