[clang-tools-extra] [clang-tidy] Add bugprone-smart-ptr-initialization check (PR #181570)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 14 13:22:36 PDT 2026
================
@@ -0,0 +1,167 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "SmartPtrInitializationCheck.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/DeclCXX.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+namespace {
+
+const auto DefaultSharedPointers = "::std::shared_ptr;::boost::shared_ptr";
+const auto DefaultUniquePointers = "::std::unique_ptr";
+const auto DefaultDefaultDeleters = "::std::default_delete";
+
+} // namespace
+
+SmartPtrInitializationCheck::SmartPtrInitializationCheck(
+ StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ SharedPointers(utils::options::parseStringList(
+ Options.get("SharedPointers", DefaultSharedPointers))),
+ UniquePointers(utils::options::parseStringList(
+ Options.get("UniquePointers", DefaultUniquePointers))),
+ DefaultDeleters(utils::options::parseStringList(
+ Options.get("DefaultDeleters", DefaultDefaultDeleters))) {}
+
+void SmartPtrInitializationCheck::storeOptions(
+ ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "SharedPointers",
+ utils::options::serializeStringList(SharedPointers));
+ Options.store(Opts, "UniquePointers",
+ utils::options::serializeStringList(UniquePointers));
+ Options.store(Opts, "DefaultDeleters",
+ utils::options::serializeStringList(DefaultDeleters));
+}
+
+void SmartPtrInitializationCheck::registerMatchers(MatchFinder *Finder) {
+ const auto IsSharedPtr = hasAnyName(SharedPointers);
+ const auto IsUniquePtr = hasAnyName(UniquePointers);
+ const auto IsSmartPtr = anyOf(IsSharedPtr, IsUniquePtr);
+ const auto IsDefaultDeleter = hasAnyName(DefaultDeleters);
+
+ const auto IsSharedPtrRecord = cxxRecordDecl(IsSharedPtr);
+ const auto IsUniquePtrRecord = cxxRecordDecl(IsUniquePtr);
+ const auto IsSmartPtrRecord = cxxRecordDecl(IsSmartPtr);
+
+ auto ReleaseCallMatcher =
+ cxxMemberCallExpr(callee(cxxMethodDecl(hasName("release"))));
+
+ // Array automatically decays to pointer
+ auto PointerArg = expr(anyOf(hasType(pointerType()), hasType(arrayType())))
+ .bind("pointer-arg");
+
+ // Matcher for unique_ptr types with custom deleters
+ auto UniquePtrWithCustomDeleter = classTemplateSpecializationDecl(
+ IsUniquePtr, templateArgumentCountIs(2),
+ hasTemplateArgument(
+ 1, refersToType(
+ unless(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
+ classTemplateSpecializationDecl(IsDefaultDeleter))))))));
+
+ // Matcher for smart pointer constructors
+ // Exclude constructors with custom deleters:
+ // - shared_ptr with 2+ arguments (second is deleter)
+ // - unique_ptr with 2+ template args where second is not default_delete
+ auto HasCustomDeleter = anyOf(
+ allOf(hasDeclaration(cxxConstructorDecl(ofClass(IsSharedPtrRecord))),
+ hasArgument(1, anything())),
+ allOf(hasType(hasUnqualifiedDesugaredType(
+ recordType(hasDeclaration(UniquePtrWithCustomDeleter)))),
+ hasDeclaration(cxxConstructorDecl(ofClass(IsUniquePtrRecord)))));
+
+ auto SmartPtrConstructorMatcher = cxxConstructExpr(
+ hasDeclaration(
+ cxxConstructorDecl(ofClass(IsSmartPtrRecord.bind("method-parent")))
+ .bind("method-decl")),
+ hasArgument(0, PointerArg), unless(HasCustomDeleter),
+ unless(hasArgument(0, cxxNewExpr())),
+ unless(hasArgument(0, ReleaseCallMatcher)),
+ unless(hasArgument(0, conditionalOperator())));
----------------
vbvictor wrote:
I think you can write `unless(hasArgument(0, anyOf(...)))` or it's not possible due to types mismatch or something?
https://github.com/llvm/llvm-project/pull/181570
More information about the cfe-commits
mailing list