c++ - gcc does not warn "variable set but not used" -


i have mcve compiles without warning:

auto foo() -> void {     int unused = 0;     unused++; } 

for me expect error: variable ‘unused’ set not used [-werror=unused-but-set-variable].

this mcve compiles without warning:

auto foo() -> void {     int x;     int unused;     ( ; x < 100; x++ )  unused++; } 

here expect these 2 errors:

  1. error: variable ‘unused’ set not used [-werror=unused-but-set-variable].
  2. error: ‘unused’ , ‘x’ used uninitialized [-werror=uninitialized]

adding bar( unused ); above for loop forces gcc display warning regarding using uninitialized variable.

why gcc 4.9.3 not complaining in of both mcve's?

compile command: g++ -o3 -c -wall -wextra -werror -std=c++11 foo.cpp

compiling -o1 warning line for : error: ‘x’ may used uninitialized in function [-wmaybe-uninitialized]. other optimization levels not produce warning.

the variables not unused compiler. warning triggered if declare local variable (and initialize it) never use variable in statement.

so, in examples, variable unused declared, initialized (in first example) , used in 2nd statement (here reading , writing). variable x declared , used (but not initialized).

in second example, compiler should show "uninitialized" warning variable x. may compiler bug if warning shown -o1 only?


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -