https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107566
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> --- At -O1 GCC does detect it: <source>: In function 'int main()': <source>:6:29: warning: iteration 3 invokes undefined behavior [-Waggressive-loop-optimizations] 6 | my_array[i] = i; | ~~~~~~~~~~~~^~~ <source>:5:26: note: within this loop 5 | for(int i = 0; i < 6; i++) | ~~^~~