79 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -fexceptions -Wreturn-type -Wmissing-noreturn -verify=expected,cxx17 -std=c++17 %s2// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -fexceptions -Wreturn-type -Wmissing-noreturn -verify=expected,cxx23 -std=c++23 %s3 4namespace std {5 class string {6 public:7 string(const char*);8 };9 class runtime_error {10 public:11 runtime_error(const string&);12 };13}14 15// This function always throws. Suggest [[noreturn]].16void throwError(const std::string& msg) { // expected-warning {{function 'throwError' could be declared with attribute 'noreturn'}}17 throw std::runtime_error(msg);18}19 20// Using the [[noreturn]] attribute on lambdas is not available until C++23,21// so we should not emit the -Wmissing-noreturn warning on earlier standards.22// Clang supports the attribute on earlier standards as an extension, and emits23// the c++23-lambda-attributes warning.24void lambda() {25 auto l1 = [] () { throw std::runtime_error("ERROR"); }; // cxx23-warning {{function 'operator()' could be declared with attribute 'noreturn'}}26 auto l2 = [] [[noreturn]] () { throw std::runtime_error("ERROR"); }; // cxx17-warning {{an attribute specifier sequence in this position is a C++23 extension}}27}28 29// The non-void caller should not warn about missing return.30int ensureZero(int i) {31 if (i == 0) return 0;32 throwError("ERROR"); // no-warning33}34 35 36template <typename Ex>37[[noreturn]]38void tpl_throws(Ex const& e) {39 throw e;40}41 42[[noreturn]]43void tpl_throws_test() {44 tpl_throws(0);45}46 47[[gnu::noreturn]]48int gnu_throws() {49 throw 0;50}51 52[[noreturn]]53int cxx11_throws() {54 throw 0;55}56 57namespace GH167247 {58struct S1 {59 virtual ~S1() = default;60 virtual void m() {61 throw std::runtime_error("This method always throws");62 }63};64 65struct S2 {66 virtual ~S2() = default;67 68 virtual void m() final { // expected-warning {{function 'm' could be declared with attribute 'noreturn'}}69 throw std::runtime_error("This method always throws");70 }71};72 73struct S3 final : S1 {74 void m() { // expected-warning {{function 'm' could be declared with attribute 'noreturn'}}75 throw std::runtime_error("This method always throws");76 }77};78}79