https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88401
Bug ID: 88401 Summary: -Wshift-overflow only works for const variables Product: gcc Version: 9.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: skvadrik at gmail dot com Target Milestone: --- Consider this program (1.cc): #include <stdint.h> void foo () { int32_t x = -32768; x << 31; const int32_t y = -32768; y << 31; } GCC detects shift overflow on y, but not on x: $ g++ -c 1.cc -Wshift-overflow=2 --std=c++11 -O3 1.cc: In function ‘void foo()’: 1.cc:9:5: warning: result of ‘(-32768 << 31)’ requires 47 bits to represent, but ‘int’ only has 32 bits [-Wshift-overflow=] y << 31; ~~^~~~~ It seems that very basic constant propagation would deduce x = -32768.