[clang-tools-extra] 4e27428 - [clang-tidy] Fix a crash in bugprone-std-namespace-modification (#214704)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Aug 8 05:17:00 PDT 2026
Author: Zeyi Xu
Date: 2026-08-08T20:16:55+08:00
New Revision: 4e274280c33edd820ab04d469d9edc1f9239d567
URL: https://github.com/llvm/llvm-project/commit/4e274280c33edd820ab04d469d9edc1f9239d567
DIFF: https://github.com/llvm/llvm-project/commit/4e274280c33edd820ab04d469d9edc1f9239d567.diff
LOG: [clang-tidy] Fix a crash in bugprone-std-namespace-modification (#214704)
Check the namespaces of template argument types directly from their
declarations. This avoids querying the AST parent map (since it may not
contain lambda closure types) and fixes the crash.
Fixes #213981
Added:
clang-tools-extra/test/clang-tidy/checkers/bugprone/std-namespace-modification-no-crash.cpp
Modified:
clang-tools-extra/clang-tidy/bugprone/StdNamespaceModificationCheck.cpp
clang-tools-extra/docs/ReleaseNotes.md
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/bugprone/StdNamespaceModificationCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StdNamespaceModificationCheck.cpp
index a623ed690697b..709de88a71f48 100644
--- a/clang-tools-extra/clang-tidy/bugprone/StdNamespaceModificationCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/StdNamespaceModificationCheck.cpp
@@ -34,6 +34,19 @@ AST_POLYMORPHIC_MATCHER_P(
Builder) != Args.end();
}
+AST_MATCHER(Decl, isInStdOrPosixNamespace) {
+ for (const auto *DC = dyn_cast<DeclContext>(&Node); DC;
+ DC = DC->getParent()) {
+ if (DC->isStdNamespace())
+ return true;
+
+ if (const auto *NS = dyn_cast<NamespaceDecl>(DC);
+ NS && NS->getName() == "posix" && NS->getParent()->isTranslationUnit())
+ return true;
+ }
+ return false;
+}
+
} // namespace
namespace clang::tidy::bugprone {
@@ -43,10 +56,11 @@ void StdNamespaceModificationCheck::registerMatchers(MatchFinder *Finder) {
hasDeclContext(namespaceDecl(hasAnyName("std", "posix"),
unless(hasParent(namespaceDecl())))
.bind("nmspc"));
+ // FIXME: Investigate why lambda closure declarations can be absent from the
+ // AST parent map.
const auto UserDefinedDecl =
namedDecl(anyOf(classTemplateDecl(), tagDecl()),
- hasAncestor(namespaceDecl(hasAnyName("std", "posix"),
- unless(hasParent(namespaceDecl())))));
+ hasDeclContext(isInStdOrPosixNamespace()));
const auto UserDefinedType = qualType(hasUnqualifiedDesugaredType(anyOf(
tagType(unless(hasDeclaration(UserDefinedDecl))),
templateSpecializationType(unless(hasDeclaration(UserDefinedDecl))))));
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index 28da9da42d8ce..5ded07934d906 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -102,6 +102,10 @@ infrastructure are described first, followed by tool-specific sections.
#### Changes in existing checks
+- Fixed a crash in {doc}`bugprone-std-namespace-modification
+ <clang-tidy/checks/bugprone/std-namespace-modification>` when checking
+ lambda closure types used as template arguments.
+
- Improved {doc}`cppcoreguidelines-pro-type-member-init
<clang-tidy/checks/cppcoreguidelines/pro-type-member-init>` check by treating
`std::array` the same as built-in arrays when `IgnoreArrays` option is enabled.
@@ -141,4 +145,3 @@ infrastructure are described first, followed by tool-specific sections.
### Improvements to pp-trace
### Clang-tidy Visual Studio plugin
-
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/std-namespace-modification-no-crash.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/std-namespace-modification-no-crash.cpp
new file mode 100644
index 0000000000000..bb2bf0eefeebb
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/std-namespace-modification-no-crash.cpp
@@ -0,0 +1,10 @@
+// RUN: %check_clang_tidy -std=c++20-or-later -expect-clang-tidy-error %s bugprone-std-namespace-modification %t
+
+template <class A, class B> struct O : A, B {};
+template <class T> void f() {
+ auto a = [] {};
+ auto b = [] {};
+ O(a, b)();
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: error: member 'operator()' found in multiple base classes of
diff erent types
+}
+template void f<int>();
More information about the cfe-commits
mailing list