https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111399
Bug ID: 111399 Summary: Sanitizer code generation smarter than warnings Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: david at westcontrol dot com Target Milestone: --- Given this code : int sign(int x) { if (x < 0) return -1; if (x == 0) return 0; if (x > 0) return 1; } and compiled with "-O2 -Wall", gcc is unable to see that all possible cases for "x" are covered, so it generates a "control reaches end of non-void function [-Wreturn-type]" warning. It would be nice if gcc could see this is a false positive, but analysis and warnings can't be perfect. However, if I add the flag "-fsanitize=undefined", the compiler is smart enough to see that all cases are covered, and there is no call to __ubsan_handle_missing_return generated. If the sanitizer code generation can see that all cases are covered, why can't the -Wreturn-type warning detection? I'm guessing it comes down to the ordering of compiler passes and therefore the level of program analysis information available at that point. But perhaps the -Wreturn-type pass could be done later when the information is available?