Debian Codegraph @

clang/test/SemaCXX/wmissing-noreturn-suggestion.cpp

raw
1 // RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -fexceptions -Wreturn-type -Wmissing-noreturn -verify=expected,cxx17 -std=c++17 %s
2 // RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -fexceptions -Wreturn-type -Wmissing-noreturn -verify=expected,cxx23 -std=c++23 %s
3
4 namespace 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]].
16 void 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 emits
23 // the c++23-lambda-attributes warning.
24 void 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.
30 int ensureZero(int i) {
31 if (i == 0) return 0;
32 throwError("ERROR"); // no-warning
33 }
34
35
36 template <typename Ex>
37 [[noreturn]]
38 void tpl_throws(Ex const& e) {
39 throw e;
40 }
41
42 [[noreturn]]
43 void tpl_throws_test() {
44 tpl_throws(0);
45 }
46
47 [[gnu::noreturn]]
48 int gnu_throws() {
49 throw 0;
50 }
51
52 [[noreturn]]
53 int cxx11_throws() {
54 throw 0;
55 }
56
57 namespace GH167247 {
58 struct S1 {
59 virtual ~S1() = default;
60 virtual void m() {
61 throw std::runtime_error("This method always throws");
62 }
63 };
64
65 struct 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
73 struct 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 }