Skip to content

Commit fd2e048

Browse files
authored
[clang-tidy] ignore consteval function in ExceptionAnalyzer (#116643)
`ExceptionAnalyzer` can ignore `consteval` function even if it will throw exception. `consteval` function must produce compile-time constant. But throw statement cannot appear in constant evaluation. Fixed: #104457.
1 parent b62557a commit fd2e048

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,12 @@ bool isQualificationConvertiblePointer(QualType From, QualType To,
320320
} // namespace
321321

322322
static bool canThrow(const FunctionDecl *Func) {
323+
// consteval specifies that every call to the function must produce a
324+
// compile-time constant, which cannot evaluate a throw expression without
325+
// producing a compilation error.
326+
if (Func->isConsteval())
327+
return false;
328+
323329
const auto *FunProto = Func->getType()->getAs<FunctionProtoType>();
324330
if (!FunProto)
325331
return true;

clang-tools-extra/docs/ReleaseNotes.rst

+4
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ Changes in existing checks
162162
<clang-tidy/checks/bugprone/dangling-handle>` check to treat `std::span` as a
163163
handle class.
164164

165+
- Improved :doc:`bugprone-exception-escape
166+
<clang-tidy/checks/bugprone/exception-escape>` by fixing false positives
167+
when a consteval function with throw statements.
168+
165169
- Improved :doc:`bugprone-forwarding-reference-overload
166170
<clang-tidy/checks/bugprone/forwarding-reference-overload>` check by fixing
167171
a crash when determining if an ``enable_if[_t]`` was found.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %check_clang_tidy -std=c++20 %s bugprone-exception-escape %t -- \
2+
// RUN: -- -fexceptions -Wno-everything
3+
4+
namespace GH104457 {
5+
6+
consteval int consteval_fn(int a) {
7+
if (a == 0)
8+
throw 1;
9+
return a;
10+
}
11+
12+
int test() noexcept { return consteval_fn(1); }
13+
14+
} // namespace GH104457

0 commit comments

Comments
 (0)