But such code might be generated by macros (or some code generator), in which case silent elimination of unnecessary code is expected and wanted behavior.
Why can't we say that the original code is wrong then? The whole point of having something be UB rather than implementation defined is because the language committee believes that it represents a bug in your program.