https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111636
Bug ID: 111636
Summary: Wreturn-type not triggered with exceptions
Product: gcc
Version: 13.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: deco33000 at yandex dot com
Target Milestone: ---
Hi,
In some cases, gcc won't trigger a warning for non returning a value in a non
void function.
Godbolt:
https://godbolt.org/z/dzfsTnssr
I simplified the code, to expose the logic of the issue.
---------------------------
// Type your code here, or load an example.
#include <vector>
#include <string>
#include <stdexcept>
enum Example {
TRY_THIS,
OR_THAT
};
template <class T>
auto check_values(auto &&type) -> std::vector<std::string> {
std::vector<std::string> accepted;
if constexpr (std::is_same_v<T, int>) {
if (type != TRY_THIS) {
throw std::runtime_error("error");
}
accepted.emplace_back("1");
} else if constexpr (std::is_same_v<T, double>) {
accepted.emplace_back("2");
} else if constexpr (std::is_same_v<T, char>) {
accepted.emplace_back("3");
}
// return accepted_mimes;
}
once you comment
if (type != TRY_THIS) {
throw std::runtime_error("error");
}
it properly warns.
It is confusing/dangerous because it means that one can put an exception in the
code and have undefined behavior at runtime (happened to me this morning),
whereas the compiler should have caught the issue at compile time.
Thanks