https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82100
Richard Hazlewood <gcc at hazlewoods dot net> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |gcc at hazlewoods dot net --- Comment #7 from Richard Hazlewood <gcc at hazlewoods dot net> --- Here's one that slipped through our net, that ought to have been obvious to the human reviewers... #include <iostream> #include <cstdlib> void fn(bool const b) { if (b) { std::cout << "side effect 1\n"; } else if (!b) { std::cout << "side effect 2\n"; } else { std::cout << "Some important thing\n"; } } int main() { for (auto n = 10; n--; ) fn(rand() & 1); } ==== g++ 15.1 -Wall -Wextra -pedantic -std=c++20 It does optimize away the unreachable code with -O2. (Not sure if it's just another variation of the OP, but didn't want to expand the unreachable issues.)