[clang-tools-extra] [clang-tidy] Add check 'bugprone-unsafe-to-allow-exceptions' (PR #176430)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Fri Feb 6 02:12:11 PST 2026
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>,
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/176430 at github.com>
================
@@ -0,0 +1,71 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "UnsafeToAllowExceptionsCheck.h"
+
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "llvm/ADT/StringSet.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+namespace {
+
+AST_MATCHER_P(FunctionDecl, hasAnyName, llvm::StringSet<>, Names) {
+ return Names.contains(Node.getNameAsString());
+}
+
+AST_MATCHER(FunctionDecl, isExplicitThrow) {
+ return isExplicitThrowExceptionSpec(Node.getExceptionSpecType()) &&
+ Node.getExceptionSpecSourceRange().isValid();
+}
+
+} // namespace
+
+UnsafeToAllowExceptionsCheck::UnsafeToAllowExceptionsCheck(
+ StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ RawCheckedSwapFunctions(
+ Options.get("CheckedSwapFunctions", "swap;iter_swap;iter_move")) {
+ llvm::SmallVector<StringRef, 4> CheckedSwapFunctionsVec;
+ RawCheckedSwapFunctions.split(CheckedSwapFunctionsVec, ";", -1, false);
+ CheckedSwapFunctions.insert_range(CheckedSwapFunctionsVec);
+}
+
+void UnsafeToAllowExceptionsCheck::storeOptions(
+ ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "CheckedSwapFunctions", RawCheckedSwapFunctions);
+}
+
+void UnsafeToAllowExceptionsCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(
+ functionDecl(allOf(isDefinition(), isExplicitThrow(),
+ anyOf(cxxDestructorDecl(),
+ cxxConstructorDecl(isMoveConstructor()),
+ cxxMethodDecl(isMoveAssignmentOperator()),
+ allOf(hasAnyName(CheckedSwapFunctions),
+ unless(parameterCountIs(0))),
+ isMain())))
+ .bind("f"),
+ this);
+}
+
+void UnsafeToAllowExceptionsCheck::check(
+ const MatchFinder::MatchResult &Result) {
+ const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("f");
+
+ if (!MatchedDecl)
+ return;
----------------
vbvictor wrote:
```suggestion
assert(MatchedDecl);
```
We expect ASTMatchers to work
https://github.com/llvm/llvm-project/pull/176430
More information about the cfe-commits
mailing list