Skip to content

Commit c6b4a42

Browse files
committed
Emit a suggestion to explicitly mark the function with [[noreturn]] if -Wmissing-noreturn is enabled.
1 parent 1949536 commit c6b4a42

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,9 +654,8 @@ Improvements to Clang's diagnostics
654654
a call to a function that is trivially known to always throw (i.e., its
655655
body consists solely of a `throw` statement). This avoids certain
656656
false positives in exception-heavy code, though only simple patterns
657-
are currently recognized.
657+
are currently recognized. Additionally, if -Wmissing-noreturn is enabled, emit a suggestion to explicitly mark the function with [[noreturn]].
658658

659-
660659
Improvements to Clang's time-trace
661660
----------------------------------
662661

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,13 @@ void clang::inferNoReturnAttr(Sema &S, const Decl *D) {
19791979
if (!FD->hasAttr<NoReturnAttr>() && !FD->hasAttr<InferredNoReturnAttr>() &&
19801980
isKnownToAlwaysThrow(FD)) {
19811981
NonConstFD->addAttr(InferredNoReturnAttr::CreateImplicit(S.Context));
1982+
1983+
// Conditionally, emit the suggestion warning.
1984+
if (!Diags.isIgnored(diag::warn_suggest_noreturn_function,
1985+
FD->getLocation())) {
1986+
S.Diag(FD->getLocation(), diag::warn_suggest_noreturn_function)
1987+
<< 0 << FD;
1988+
}
19821989
}
19831990
}
19841991

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -fexceptions -Wreturn-type -Wmissing-noreturn -verify %s
2+
3+
namespace std {
4+
class string {
5+
public:
6+
string(const char*);
7+
};
8+
class runtime_error {
9+
public:
10+
runtime_error(const string&);
11+
};
12+
}
13+
14+
// This function always throws. Suggest [[noreturn]].
15+
void throwError(const std::string& msg) { // expected-warning {{function 'throwError' could be declared with attribute 'noreturn'}}
16+
throw std::runtime_error(msg);
17+
}
18+
19+
// The non-void caller should not warn about missing return.
20+
int ensureZero(int i) {
21+
if (i == 0) return 0;
22+
throwError("ERROR"); // no-warning
23+
}

0 commit comments

Comments
 (0)