https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/184009
>From 271d65957b3de308421f353ba3cc7028f99aaf3c Mon Sep 17 00:00:00 2001 From: Victor Baranov <[email protected]> Date: Sun, 1 Mar 2026 18:08:25 +0300 Subject: [PATCH 1/2] [clang-tidy] Add new check misc-use-braced-initialization --- .../CppCoreGuidelinesTidyModule.cpp | 3 + .../clang-tidy/misc/CMakeLists.txt | 1 + .../clang-tidy/misc/MiscTidyModule.cpp | 3 + .../misc/UseBracedInitializationCheck.cpp | 160 +++++ .../misc/UseBracedInitializationCheck.h | 34 + .../clang-tidy/utils/LexerUtils.h | 15 + clang-tools-extra/docs/ReleaseNotes.rst | 11 + .../use-braced-initialization.rst | 15 + .../docs/clang-tidy/checks/list.rst | 2 + .../checks/misc/use-braced-initialization.rst | 66 ++ .../clang-tidy/checkers/Inputs/Headers/string | 10 +- .../misc/use-braced-initialization-cxx20.cpp | 211 ++++++ .../misc/use-braced-initialization.cpp | 603 ++++++++++++++++++ 13 files changed, 1131 insertions(+), 3 deletions(-) create mode 100644 clang-tools-extra/clang-tidy/misc/UseBracedInitializationCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/misc/UseBracedInitializationCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/use-braced-initialization.rst create mode 100644 clang-tools-extra/docs/clang-tidy/checks/misc/use-braced-initialization.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/use-braced-initialization-cxx20.cpp create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/use-braced-initialization.cpp diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp index fab4f92be22b6..e60e43c5e5697 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp @@ -11,6 +11,7 @@ #include "../bugprone/NarrowingConversionsCheck.h" #include "../misc/NonPrivateMemberVariablesInClassesCheck.h" #include "../misc/UnconventionalAssignOperatorCheck.h" +#include "../misc/UseBracedInitializationCheck.h" #include "../modernize/AvoidCArraysCheck.h" #include "../modernize/MacroToEnumCheck.h" #include "../modernize/UseDefaultMemberInitCheck.h" @@ -133,6 +134,8 @@ class CppCoreGuidelinesModule : public ClangTidyModule { CheckFactories.registerCheck<SpecialMemberFunctionsCheck>( "cppcoreguidelines-special-member-functions"); CheckFactories.registerCheck<SlicingCheck>("cppcoreguidelines-slicing"); + CheckFactories.registerCheck<misc::UseBracedInitializationCheck>( + "cppcoreguidelines-use-braced-initialization"); CheckFactories.registerCheck<modernize::UseDefaultMemberInitCheck>( "cppcoreguidelines-use-default-member-init"); CheckFactories.registerCheck<UseEnumClassCheck>( diff --git a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt index e34b0cf687be3..215243bae96aa 100644 --- a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt @@ -45,6 +45,7 @@ add_clang_library(clangTidyMiscModule STATIC UnusedParametersCheck.cpp UnusedUsingDeclsCheck.cpp UseAnonymousNamespaceCheck.cpp + UseBracedInitializationCheck.cpp UseInternalLinkageCheck.cpp LINK_LIBS diff --git a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp index f8550b30b9789..3abd74ff94f31 100644 --- a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp @@ -34,6 +34,7 @@ #include "UnusedParametersCheck.h" #include "UnusedUsingDeclsCheck.h" #include "UseAnonymousNamespaceCheck.h" +#include "UseBracedInitializationCheck.h" #include "UseInternalLinkageCheck.h" namespace clang::tidy { @@ -90,6 +91,8 @@ class MiscModule : public ClangTidyModule { "misc-unused-using-decls"); CheckFactories.registerCheck<UseAnonymousNamespaceCheck>( "misc-use-anonymous-namespace"); + CheckFactories.registerCheck<UseBracedInitializationCheck>( + "misc-use-braced-initialization"); CheckFactories.registerCheck<UseInternalLinkageCheck>( "misc-use-internal-linkage"); } diff --git a/clang-tools-extra/clang-tidy/misc/UseBracedInitializationCheck.cpp b/clang-tools-extra/clang-tidy/misc/UseBracedInitializationCheck.cpp new file mode 100644 index 0000000000000..7a9465e12581f --- /dev/null +++ b/clang-tools-extra/clang-tidy/misc/UseBracedInitializationCheck.cpp @@ -0,0 +1,160 @@ +//===----------------------------------------------------------------------===// +// +// 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 "UseBracedInitializationCheck.h" +#include "../utils/LexerUtils.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/DeclCXX.h" +#include "clang/AST/ExprCXX.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::misc { + +namespace { + +AST_MATCHER_P(VarDecl, hasInitStyle, VarDecl::InitializationStyle, Style) { + return Node.getInitStyle() == Style; +} + +AST_MATCHER(Type, isDependentType) { return Node.isDependentType(); } + +AST_MATCHER(CXXConstructExpr, noMacroParens) { + const SourceRange Range = Node.getParenOrBraceRange(); + return Range.isValid() && !Range.getBegin().isMacroID() && + !Range.getEnd().isMacroID(); +} + +AST_MATCHER(Expr, isCXXParenListInitExpr) { + return isa<CXXParenListInitExpr>(Node); +} + +/// Matches CXXConstructExpr whose target class has any constructor +/// taking 'std::initializer_list'. When such a constructor exists, braced +/// initialization may call it instead of the intended constructor. +AST_MATCHER(CXXConstructExpr, constructsTypeWithInitListCtor) { + const CXXRecordDecl *RD = Node.getConstructor()->getParent(); + if (!RD || !RD->hasDefinition()) + return false; + return llvm::any_of(RD->ctors(), [](const CXXConstructorDecl *Ctor) { + if (Ctor->getNumParams() == 0) + return false; + const QualType FirstParam = + Ctor->getParamDecl(0)->getType().getNonReferenceType(); + const auto *Record = FirstParam->getAsCXXRecordDecl(); + if (!Record || !Record->getDeclName().isIdentifier() || + Record->getName() != "initializer_list" || !Record->isInStdNamespace()) + return false; + // [dcl.init.list] p2: all other params must have defaults. + for (unsigned I = 1; I < Ctor->getNumParams(); ++I) + if (!Ctor->getParamDecl(I)->hasDefaultArg()) + return false; + return true; + }); +} + +} // namespace + +void UseBracedInitializationCheck::registerMatchers(MatchFinder *Finder) { + auto GoodCtor = allOf( + noMacroParens(), unless(constructsTypeWithInitListCtor()), + unless(isInTemplateInstantiation()), unless(isListInitialization())); + auto GoodCtorExpr = cxxConstructExpr(GoodCtor).bind("ctor"); + auto GoodVar = + allOf(unless(hasType(isDependentType())), unless(hasType(autoType()))); + + // Variable declarations: Simple w(1), Takes t({1, 2}) + Finder->addMatcher(varDecl(hasInitStyle(VarDecl::CallInit), + hasInitializer(ignoringImplicit(GoodCtorExpr)), + GoodVar), + this); + + // Scalar direct-init: int x(42), double d(3.14) + Finder->addMatcher( + varDecl(hasInitStyle(VarDecl::CallInit), + hasInitializer(unless(ignoringImplicit(cxxConstructExpr()))), + GoodVar) + .bind("scalar"), + this); + + Finder->addMatcher(cxxFunctionalCastExpr(has(GoodCtorExpr)), this); + Finder->addMatcher(cxxTemporaryObjectExpr(GoodCtor).bind("ctor"), this); + Finder->addMatcher(cxxNewExpr(has(GoodCtorExpr)), this); + + if (getLangOpts().CPlusPlus20) { + auto GoodPLE = expr(isCXXParenListInitExpr()).bind("ple"); + Finder->addMatcher(varDecl(hasInitStyle(VarDecl::ParenListInit), + hasInitializer(GoodPLE), GoodVar) + .bind("var_ple"), + this); + Finder->addMatcher(cxxFunctionalCastExpr(has(GoodPLE)).bind("cast_ple"), + this); + Finder->addMatcher(cxxNewExpr(has(GoodPLE)).bind("new_ple"), this); + } +} + +void UseBracedInitializationCheck::check( + const MatchFinder::MatchResult &Result) { + SourceLocation DiagLoc; + SourceLocation LParen; + SourceLocation RParen; + + if (const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructExpr>("ctor")) { + DiagLoc = Ctor->getBeginLoc(); + LParen = Ctor->getParenOrBraceRange().getBegin(); + RParen = Ctor->getParenOrBraceRange().getEnd(); + } else if (const auto *Var = Result.Nodes.getNodeAs<VarDecl>("scalar")) { + assert(Var->hasInit()); + const SourceManager &SM = *Result.SourceManager; + const LangOptions &LangOpts = Result.Context->getLangOpts(); + + const std::optional<Token> LTok = + utils::lexer::findPreviousTokenSkippingComments( + Var->getInit()->getBeginLoc(), SM, LangOpts); + if (!LTok || LTok->isNot(tok::l_paren) || LTok->getLocation().isMacroID()) + return; + + const std::optional<Token> RTok = + utils::lexer::findNextTokenSkippingComments(Var->getInit()->getEndLoc(), + SM, LangOpts); + if (!RTok || RTok->isNot(tok::r_paren) || RTok->getLocation().isMacroID()) + return; + + DiagLoc = Var->getLocation(); + LParen = LTok->getLocation(); + RParen = RTok->getLocation(); + } else if (const auto *PLE = + Result.Nodes.getNodeAs<CXXParenListInitExpr>("ple")) { + LParen = PLE->getBeginLoc(); + RParen = PLE->getEndLoc(); + if (const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var_ple")) + DiagLoc = Var->getLocation(); + else if (const auto *Cast = + Result.Nodes.getNodeAs<CXXFunctionalCastExpr>("cast_ple")) + DiagLoc = Cast->getBeginLoc(); + else if (const auto *New = Result.Nodes.getNodeAs<CXXNewExpr>("new_ple")) + DiagLoc = New->getBeginLoc(); + else + llvm_unreachable("No context for CXXParenListInitExpr"); + } else { + llvm_unreachable("No matches found"); + } + + if (LParen.isMacroID() || RParen.isMacroID()) + return; + + diag(DiagLoc, + "use braced initialization instead of parenthesized initialization") + << FixItHint::CreateReplacement(LParen, "{") + << FixItHint::CreateReplacement(RParen, "}"); +} + +} // namespace clang::tidy::misc diff --git a/clang-tools-extra/clang-tidy/misc/UseBracedInitializationCheck.h b/clang-tools-extra/clang-tidy/misc/UseBracedInitializationCheck.h new file mode 100644 index 0000000000000..a010558ab1d2a --- /dev/null +++ b/clang-tools-extra/clang-tidy/misc/UseBracedInitializationCheck.h @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_USEBRACEDINITIALIZATIONCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_USEBRACEDINITIALIZATIONCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::misc { + +/// Suggests replacing parenthesized initialization with braced +/// initialization. +/// +/// For the user-facing documentation see: +/// https://clang.llvm.org/extra/clang-tidy/checks/misc/use-braced-initialization.html +class UseBracedInitializationCheck : public ClangTidyCheck { +public: + UseBracedInitializationCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { + return LangOpts.CPlusPlus11; + } +}; + +} // namespace clang::tidy::misc + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_USEBRACEDINITIALIZATIONCHECK_H diff --git a/clang-tools-extra/clang-tidy/utils/LexerUtils.h b/clang-tools-extra/clang-tidy/utils/LexerUtils.h index 38123ae14cff7..259012a7b528b 100644 --- a/clang-tools-extra/clang-tidy/utils/LexerUtils.h +++ b/clang-tools-extra/clang-tidy/utils/LexerUtils.h @@ -93,6 +93,21 @@ SourceLocation findNextAnyTokenKind(SourceLocation Start, } } +// Finds previous token, possibly a comment. +inline std::optional<Token> +findPreviousTokenIncludingComments(SourceLocation Start, + const SourceManager &SM, + const LangOptions &LangOpts) { + return Lexer::findPreviousToken(Start, SM, LangOpts, true); +} + +// Finds previous token that's not a comment. +inline std::optional<Token> +findPreviousTokenSkippingComments(SourceLocation Start, const SourceManager &SM, + const LangOptions &LangOpts) { + return Lexer::findPreviousToken(Start, SM, LangOpts, false); +} + // Finds next token, possibly a comment. inline std::optional<Token> findNextTokenIncludingComments(SourceLocation Start, const SourceManager &SM, diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 6bdc0ae7bdcc8..8a9d03127a890 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -124,6 +124,12 @@ New checks ``llvm::to_vector(llvm::make_filter_range(...))`` that can be replaced with ``llvm::map_to_vector`` and ``llvm::filter_to_vector``. +- New :doc:`misc-use-braced-initialization + <clang-tidy/checks/misc/use-braced-initialization>` check. + + Suggests replacing parenthesized initialization with braced + initialization. + - New :doc:`modernize-use-string-view <clang-tidy/checks/modernize/use-string-view>` check. @@ -151,6 +157,11 @@ New checks New check aliases ^^^^^^^^^^^^^^^^^ +- New alias :doc:`cppcoreguidelines-use-braced-initialization + <clang-tidy/checks/cppcoreguidelines/use-braced-initialization>` to + :doc:`misc-use-braced-initialization + <clang-tidy/checks/misc/use-braced-initialization>` was added. + Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/use-braced-initialization.rst b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/use-braced-initialization.rst new file mode 100644 index 0000000000000..3deb40f96f1cc --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/use-braced-initialization.rst @@ -0,0 +1,15 @@ +.. title:: clang-tidy - cppcoreguidelines-use-braced-initialization +.. meta:: + :http-equiv=refresh: 5;URL=../misc/use-braced-initialization.html + +cppcoreguidelines-use-braced-initialization +=========================================== + +This check implements `ES.23 +<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es23-prefer-the--initializer-syntax>`_ +from the C++ Core Guidelines. + +The `cppcoreguidelines-use-braced-initialization` check is an alias, +please see +:doc:`misc-use-braced-initialization<../misc/use-braced-initialization>` +for more information. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index c475870ed7b31..66dce8f492e80 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -287,6 +287,7 @@ Clang-Tidy Checks :doc:`misc-unused-parameters <misc/unused-parameters>`, "Yes" :doc:`misc-unused-using-decls <misc/unused-using-decls>`, "Yes" :doc:`misc-use-anonymous-namespace <misc/use-anonymous-namespace>`, + :doc:`misc-use-braced-initialization <misc/use-braced-initialization>`, "Yes" :doc:`misc-use-internal-linkage <misc/use-internal-linkage>`, "Yes" :doc:`modernize-avoid-bind <modernize/avoid-bind>`, "Yes" :doc:`modernize-avoid-c-arrays <modernize/avoid-c-arrays>`, @@ -587,6 +588,7 @@ Check aliases :doc:`cppcoreguidelines-noexcept-move-operations <cppcoreguidelines/noexcept-move-operations>`, :doc:`performance-noexcept-move-constructor <performance/noexcept-move-constructor>`, "Yes" :doc:`cppcoreguidelines-noexcept-swap <cppcoreguidelines/noexcept-swap>`, :doc:`performance-noexcept-swap <performance/noexcept-swap>`, "Yes" :doc:`cppcoreguidelines-non-private-member-variables-in-classes <cppcoreguidelines/non-private-member-variables-in-classes>`, :doc:`misc-non-private-member-variables-in-classes <misc/non-private-member-variables-in-classes>`, + :doc:`cppcoreguidelines-use-braced-initialization <cppcoreguidelines/use-braced-initialization>`, :doc:`misc-use-braced-initialization <misc/use-braced-initialization>`, "Yes" :doc:`cppcoreguidelines-use-default-member-init <cppcoreguidelines/use-default-member-init>`, :doc:`modernize-use-default-member-init <modernize/use-default-member-init>`, "Yes" :doc:`fuchsia-header-anon-namespaces <fuchsia/header-anon-namespaces>`, :doc:`misc-anonymous-namespace-in-header <misc/anonymous-namespace-in-header>`, :doc:`fuchsia-multiple-inheritance <fuchsia/multiple-inheritance>`, :doc:`misc-multiple-inheritance <misc/multiple-inheritance>`, diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc/use-braced-initialization.rst b/clang-tools-extra/docs/clang-tidy/checks/misc/use-braced-initialization.rst new file mode 100644 index 0000000000000..4df9480b614b0 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/misc/use-braced-initialization.rst @@ -0,0 +1,66 @@ +.. title:: clang-tidy - misc-use-braced-initialization + +misc-use-braced-initialization +============================== + +Suggests replacing parenthesized initialization with braced initialization. + +Braced initialization has several advantages over parenthesized initialization: + +- **Catches narrowing conversions.** ``int x{3.14}`` is a compile-time error, + while ``int x(3.14)`` silently truncates. +- **Uniform syntax.** Braces work consistently for aggregates, containers, and + constructors, giving a single initialization style across all types. + +For example: + +.. code-block:: c++ + + struct Matrix { + Matrix(int rows, int cols); + }; + + // Variable declarations: + Matrix m(3, 4); // -> Matrix m{3, 4}; + int n(42); // -> int n{42}; + + // Copy initialization: + Matrix m = Matrix(3, 4); // -> Matrix m = Matrix{3, 4}; + + // Temporary objects: + use(Matrix(3, 4)); // -> use(Matrix{3, 4}); + + // New expressions: + auto *p = new Matrix(3, 4); // -> auto *p = new Matrix{3, 4}; + +The check skips cases where changing from ``()`` to ``{}`` would alter program +semantics: + +- Types that have any constructor accepting ``std::initializer_list``, since + braced initialization would prefer that overload and silently change + semantics. +- Direct-initialized ``auto`` variables, where deduction rules may differ + between C++ standards. +- Expressions in macro expansions. + +.. note:: + + Braced initialization prohibits implicit narrowing conversions. + In some cases the suggested fix may introduce a compiler error. + + .. code-block:: c++ + + struct Foo { + Foo(short n); + }; + + int n = 10; + Foo f(n); // OK: implicit narrowing allowed with () + Foo f{n}; // error: narrowing conversion from 'int' to 'short' + +References +---------- + +This check corresponds to the CERT C++ Core Guidelines rule +`C++ Core Guidelines ES.23 +<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es23-prefer-the--initializer-syntax>`_. diff --git a/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/string b/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/string index fc197d0afa714..6ff5afb4d781c 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/string +++ b/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/string @@ -4,6 +4,7 @@ // For size_t #include "string.h" #include "memory" +#include "initializer_list" typedef unsigned __INT16_TYPE__ char16; typedef unsigned __INT32_TYPE__ char32; @@ -26,6 +27,7 @@ struct basic_string { basic_string(const C *p, size_type count); basic_string(const C *b, const C *e); basic_string(size_t, C); + basic_string(std::initializer_list<C>); operator basic_string_view<C, T>() const; ~basic_string(); @@ -181,14 +183,16 @@ bool operator!=(const char*, const std::string_view&); size_t strlen(const char* str); -namespace literals { -namespace string_literals { +#if __cplusplus >= 201402L +inline namespace literals { +inline namespace string_literals { string operator""s(const char *, size_t); } -namespace string_view_literals { +inline namespace string_view_literals { string_view operator""sv(const char *, size_t); } } +#endif } #endif // _STRING_ diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/use-braced-initialization-cxx20.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/use-braced-initialization-cxx20.cpp new file mode 100644 index 0000000000000..9da81b5a7d743 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/use-braced-initialization-cxx20.cpp @@ -0,0 +1,211 @@ +// RUN: %check_clang_tidy -std=c++20-or-later %s misc-use-braced-initialization %t + +struct Agg { + int a, b; +}; + +struct AggDefault { + int a = 0; + int b; +}; + +struct Nested { + Agg x; + int y; +}; + +struct Takes { + Takes(Agg); +}; + +struct Simple { + Simple(int); +}; + +void basic_aggregate() { + Agg d(1, 2); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use braced initialization instead of parenthesized initialization [misc-use-braced-initialization] + // CHECK-FIXES: Agg d{1, 2}; +} + +void aggregate_default_member() { + AggDefault ad(1, 2); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use braced initialization + // CHECK-FIXES: AggDefault ad{1, 2}; +} + +void nested_aggregate_braced_inner() { + Nested n(Agg{1, 2}, 3); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization + // CHECK-FIXES: Nested n{Agg{1, 2}, 3}; +} + +void nested_aggregate_paren_inner() { + Nested n(Agg(1, 2), 3); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: use braced initialization + // CHECK-FIXES: Nested n{Agg{1, 2}, 3}; +} + +void aggregate_multi_decl() { + Agg a(1, 2), b(3, 4); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:16: warning: use braced initialization + // CHECK-FIXES: Agg a{1, 2}, b{3, 4}; +} + +void aggregate_temporary() { + Agg(1, 2); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use braced initialization + // CHECK-FIXES: Agg{1, 2}; +} + +void aggregate_temporary_cast_to_void() { + (void)Agg(1, 2); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use braced initialization + // CHECK-FIXES: (void)Agg{1, 2}; +} + +void aggregate_auto() { + auto d = Agg(1, 2); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use braced initialization + // CHECK-FIXES: auto d = Agg{1, 2}; +} + +Agg return_aggregate() { + return Agg(1, 2); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization + // CHECK-FIXES: return Agg{1, 2}; +} + +void func_arg(Agg); +void aggregate_as_argument() { + func_arg(Agg(1, 2)); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use braced initialization + // CHECK-FIXES: func_arg(Agg{1, 2}); +} + +void designated_as_arg() { + Takes t({.a = 1, .b = 2}); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use braced initialization + // CHECK-FIXES: Takes t{{[{][{]}}.a = 1, .b = 2{{[}][}]}}; +} + +void aggregate_new() { + Agg *p = new Agg(1, 2); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use braced initialization + // CHECK-FIXES: Agg *p = new Agg{1, 2}; + (void)p; +} + +void lambda_capture_init() { + auto f = [s = Simple(1)](){}; + // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use braced initialization + // CHECK-FIXES: auto f = [s = Simple{1}](){}; +} + +void ternary_arg(bool c) { + Simple s(c ? 1 : 2); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization + // CHECK-FIXES: Simple s{c ? 1 : 2}; +} + +struct L1 { + int a, b; +}; + +struct L2 { + L1 x; + int y; +}; + +struct L3 { + L2 m; + int z; +}; + +struct L4 { + L3 n; + int w; +}; + +void nested_agg_two_levels() { + L2 v(L1(1, 2), 3); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: use braced initialization + // CHECK-FIXES: L2 v{L1{1, 2}, 3}; +} + +void nested_agg_three_levels() { + L3 v(L2(L1(1, 2), 3), 4); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-3]]:11: warning: use braced initialization + // CHECK-FIXES: L3 v{L2{L1{1, 2}, 3}, 4}; +} + +void nested_agg_four_levels() { + L4 v(L3(L2(L1(1, 2), 3), 4), 5); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-3]]:11: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-4]]:14: warning: use braced initialization + // CHECK-FIXES: L4 v{L3{L2{L1{1, 2}, 3}, 4}, 5}; +} + +void nested_agg_temporary() { + (void)L3(L2(L1(1, 2), 3), 4); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-3]]:15: warning: use braced initialization + // CHECK-FIXES: (void)L3{L2{L1{1, 2}, 3}, 4}; +} + +void nested_agg_new() { + L3 *p = new L3(L2(L1(1, 2), 3), 4); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:18: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-3]]:21: warning: use braced initialization + // CHECK-FIXES: L3 *p = new L3{L2{L1{1, 2}, 3}, 4}; + (void)p; +} + +// Mixed: some levels already braced, only paren levels get fixed. +void nested_agg_mixed() { + L3 v(L2{L1(1, 2), 3}, 4); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:11: warning: use braced initialization + // CHECK-FIXES: L3 v{L2{L1{1, 2}, 3}, 4}; +} + +void nested_agg_mixed_inner_braced() { + L3 v(L2(L1{1, 2}, 3), 4); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: use braced initialization + // CHECK-FIXES: L3 v{L2{L1{1, 2}, 3}, 4}; +} + +void already_braced() { + Agg d{1, 2}; +} + +void already_braced_temporary() { + Agg{1, 2}; +} + +void new_already_braced() { + Agg *p = new Agg{1, 2}; + (void)p; +} + +void copy_init() { + Agg d = {1, 2}; +} + +void designated_already_braced() { + Agg d{.a = 1, .b = 2}; +} + +void designated_copy_init() { + Agg d = {.a = 1, .b = 2}; +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/use-braced-initialization.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/use-braced-initialization.cpp new file mode 100644 index 0000000000000..8d21561d57da8 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/use-braced-initialization.cpp @@ -0,0 +1,603 @@ +// RUN: %check_clang_tidy -std=c++11-or-later %s misc-use-braced-initialization %t \ +// RUN: -- -- -I %S/../Inputs/Headers + +#include <string> +#include <vector> + +struct Simple { + Simple(int); + Simple(int, double); + Simple(const Simple &); +}; + +struct Explicit { + explicit Explicit(int); +}; + +struct Aggregate { + int a, b; +}; + +struct Takes { + Takes(Aggregate); +}; + +struct TwoAggregates { + TwoAggregates(Aggregate, Aggregate); +}; + +struct WithDefault { + WithDefault(int, int = 0); +}; + +struct HasDefault { + HasDefault(); +}; + +struct Outer { + struct Inner { + Inner(int); + }; +}; + +namespace ns { +struct Ns { + Ns(int); +}; +} // namespace ns + +#define MAKE_SIMPLE(x) Simple w(x) +#define WRAP_PARENS(x) (x) +#define TYPE_ALIAS Simple + +void basic_single_arg() { + Simple w(1); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization instead of parenthesized initialization [misc-use-braced-initialization] + // CHECK-FIXES: Simple w{1}; +} + +void basic_multiple_args() { + Simple w(1, 2.0); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization + // CHECK-FIXES: Simple w{1, 2.0}; +} + +void explicit_ctor() { + Explicit e(42); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use braced initialization + // CHECK-FIXES: Explicit e{42}; +} + +void copy_construction() { + Simple w1(1); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization + // CHECK-FIXES: Simple w1{1}; + Simple w2(w1); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization + // CHECK-FIXES: Simple w2{w1}; +} + +void static_local() { + static Simple sw(1); + // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use braced initialization + // CHECK-FIXES: static Simple sw{1}; +} + +void const_variable() { + const Simple cw(1); + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use braced initialization + // CHECK-FIXES: const Simple cw{1}; +} + +void default_args_ctor() { + WithDefault m(1); + // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use braced initialization + // CHECK-FIXES: WithDefault m{1}; +} + +void nested_type() { + Outer::Inner oi(1); + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use braced initialization + // CHECK-FIXES: Outer::Inner oi{1}; +} + +void namespaced_type() { + ns::Ns g(1); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization + // CHECK-FIXES: ns::Ns g{1}; +} + +void for_loop_init() { + for (Simple fw(1);;) { + // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use braced initialization + // CHECK-FIXES: for (Simple fw{1};;) { + break; + } +} + +void expression_arg() { + Simple w(1 + 2); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization + // CHECK-FIXES: Simple w{1 + 2}; +} + +void variable_arg(int x) { + Simple w(x); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization + // CHECK-FIXES: Simple w{x}; +} + +void ternary_arg(bool c) { + Simple s(c ? 1 : 2); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization + // CHECK-FIXES: Simple s{c ? 1 : 2}; +} + +void multi_decl_class() { + Simple a(1), b(2); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:16: warning: use braced initialization + // CHECK-FIXES: Simple a{1}, b{2}; +} + +Simple global_simple(1); +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use braced initialization +// CHECK-FIXES: Simple global_simple{1}; + +namespace ns_scope { +Simple ns_var(2); +// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use braced initialization +// CHECK-FIXES: Simple ns_var{2}; +} // namespace ns_scope + +// Macro wraps only the type name; parens are in user code, safe to fix. +void macro_type_only() { + TYPE_ALIAS w(1); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use braced initialization + // CHECK-FIXES: TYPE_ALIAS w{1}; +} + +void scalar_int() { + int x(42); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use braced initialization + // CHECK-FIXES: int x{42}; +} + +void scalar_double() { + double d(3.14); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization + // CHECK-FIXES: double d{3.14}; +} + +void scalar_expression(int a) { + int y(a + 1); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use braced initialization + // CHECK-FIXES: int y{a + 1}; +} + +void scalar_pointer() { + int *p(nullptr); + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use braced initialization + // CHECK-FIXES: int *p{nullptr}; +} + +void multi_decl_scalar() { + int a(1), b(2); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:13: warning: use braced initialization + // CHECK-FIXES: int a{1}, b{2}; +} + +void temporary_single_arg() { + Simple(1); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use braced initialization + // CHECK-FIXES: Simple{1}; +} + +void temporary_multi_arg() { + Simple(1, 2.0); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use braced initialization + // CHECK-FIXES: Simple{1, 2.0}; +} + +void copy_init_rhs() { + Simple w = Simple(1); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use braced initialization + // CHECK-FIXES: Simple w = Simple{1}; +} + +void auto_copy_init() { + auto w = Simple(1); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use braced initialization + // CHECK-FIXES: auto w = Simple{1}; +} + +Simple return_simple() { + return Simple(1); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization + // CHECK-FIXES: return Simple{1}; +} + +void func_arg(Simple); +void simple_as_argument() { + func_arg(Simple(1)); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use braced initialization + // CHECK-FIXES: func_arg(Simple{1}); +} + +void new_multi_arg() { + Simple *p = new Simple(1, 2.0); + // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use braced initialization + // CHECK-FIXES: Simple *p = new Simple{1, 2.0}; + (void)p; +} + +void braced_arg() { + Takes tp({1, 2}); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use braced initialization + // CHECK-FIXES: Takes tp{{[{][{]}}1, 2{{[}][}]}}; +} + +void braced_constructed_arg() { + Takes tp(Aggregate{1, 2}); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use braced initialization + // CHECK-FIXES: Takes tp{Aggregate{1, 2}}; +} + +void multiple_braced_args() { + TwoAggregates t({1, 2}, {3, 4}); + // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use braced initialization + // CHECK-FIXES: TwoAggregates t{{[{][{]}}1, 2}, {3, 4{{[}][}]}}; +} + +void temporary_braced_arg() { + (void)Takes({1, 2}); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use braced initialization + // CHECK-FIXES: (void)Takes{{[{][{]}}1, 2{{[}][}]}}; +} + +void new_braced_arg() { + Takes *p = new Takes({1, 2}); + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use braced initialization + // CHECK-FIXES: Takes *p = new Takes{{[{][{]}}1, 2{{[}][}]}}; + (void)p; +} + +void class_comment_before_parens() { + Simple w /*comment*/ (1); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization + // CHECK-FIXES: Simple w /*comment*/ {1}; +} + +void class_comment_inside_parens() { + Simple w(/*comment*/ 1); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use braced initialization + // CHECK-FIXES: Simple w{/*comment*/ 1}; +} + +void scalar_comment_before_parens() { + int x /*comment*/ (42); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use braced initialization + // CHECK-FIXES: int x /*comment*/ {42}; +} + +void scalar_comment_inside_parens() { + int x(/*comment*/ 42); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use braced initialization + // CHECK-FIXES: int x{/*comment*/ 42}; +} + +void scalar_comment_after_init() { + int x(42 /*comment*/); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use braced initialization + // CHECK-FIXES: int x{42 /*comment*/}; +} + +struct L1 { + int a, b; + L1(int, int); +}; + +struct L2 { + L1 x; + int y; + L2(L1, int); +}; + +struct L3 { + L2 m; + int z; + L3(L2, int); +}; + +struct L4 { + L3 n; + int w; + L4(L3, int); +}; + +void nested_ctors_two_levels() { + L2 v(L1(1, 2), 3); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: use braced initialization + // CHECK-FIXES: L2 v{L1{1, 2}, 3}; +} + +void nested_ctors_three_levels() { + L3 v(L2(L1(1, 2), 3), 4); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-3]]:11: warning: use braced initialization + // CHECK-FIXES: L3 v{L2{L1{1, 2}, 3}, 4}; +} + +void nested_ctors_four_levels() { + L4 v(L3(L2(L1(1, 2), 3), 4), 5); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-3]]:11: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-4]]:14: warning: use braced initialization + // CHECK-FIXES: L4 v{L3{L2{L1{1, 2}, 3}, 4}, 5}; +} + +void nested_ctors_temporary() { + (void)L3(L2(L1(1, 2), 3), 4); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:12: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-3]]:15: warning: use braced initialization + // CHECK-FIXES: (void)L3{L2{L1{1, 2}, 3}, 4}; +} + +void nested_ctors_new() { + L3 *p = new L3(L2(L1(1, 2), 3), 4); + // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:18: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-3]]:21: warning: use braced initialization + // CHECK-FIXES: L3 *p = new L3{L2{L1{1, 2}, 3}, 4}; + (void)p; +} + +// Mixed: some levels already braced, only paren levels get fixed. +void nested_ctors_mixed() { + L3 v(L2{L1(1, 2), 3}, 4); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:11: warning: use braced initialization + // CHECK-FIXES: L3 v{L2{L1{1, 2}, 3}, 4}; +} + +void nested_ctors_mixed_inner_braced() { + L3 v(L2(L1{1, 2}, 3), 4); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use braced initialization + // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: use braced initialization + // CHECK-FIXES: L3 v{L2{L1{1, 2}, 3}, 4}; +} + +void already_braced() { + Simple w{1}; +} + +void already_braced_temporary() { + Simple{1}; +} + +void new_already_braced() { + Simple *p = new Simple{1}; + (void)p; +} + +void scalar_already_braced() { + int x{42}; +} + +void direct_auto() { + auto w(1); +} + +void scalar_auto() { + auto x(42); +} + +void scalar_copy_init() { + int x = 42; +} + +void default_construction() { + HasDefault d; +} + +void macro_full_decl() { + MAKE_SIMPLE(1); +} + +void macro_wraps_parens() { + Simple w WRAP_PARENS(1); +} + +template <typename T> +void template_dependent() { + T t(1); +} + +template <typename T> +void template_instantiated(int x) { + T t(x); +} + +template <typename T> +void template_instantiated2(T x) { + auto t(x); +} + +template <typename T> +void template_temporary_single() { + (void)T(1); +} + +template <typename T> +void template_temporary_multi() { + (void)T(1, 2.0); +} + +template <typename T> +T template_return() { + return T(1); +} + +template <typename T> +T *template_new_expr() { + return new T(1); +} + +void force_instantiation() { + template_instantiated<Simple>(1); + template_instantiated2<Simple>(1); + template_temporary_single<Simple>(); + template_temporary_multi<Simple>(); + (void)template_return<Simple>(); + delete template_new_expr<Simple>(); +} + +struct InitListByValue { + InitListByValue(std::initializer_list<int>); + InitListByValue(int); + InitListByValue(int, int); +}; + +void il_by_value() { + InitListByValue x(1); +} + +void il_by_value_multi() { + InitListByValue x(1, 2); +} + +struct InitListConstRef { + InitListConstRef(const std::initializer_list<int> &); + InitListConstRef(int); +}; + +void il_const_ref() { + InitListConstRef x(1); +} + +struct InitListMutableRef { + InitListMutableRef(std::initializer_list<int> &); + InitListMutableRef(int); +}; + +void il_mutable_ref() { + InitListMutableRef x(1); +} + +struct InitListRvalueRef { + InitListRvalueRef(std::initializer_list<int> &&); + InitListRvalueRef(int); +}; + +void il_rvalue_ref() { + InitListRvalueRef x(1); +} + +struct InitListVolatileRef { + InitListVolatileRef(volatile std::initializer_list<int> &); + InitListVolatileRef(int); +}; + +void il_volatile_ref() { + InitListVolatileRef x(1); +} + +struct InitListConstVolatileRef { + InitListConstVolatileRef(const volatile std::initializer_list<int> &); + InitListConstVolatileRef(int); +}; + +void il_const_volatile_ref() { + InitListConstVolatileRef x(1); +} + +struct InitListDefaults { + InitListDefaults(std::initializer_list<int>, int = 0, double = 1.0); + InitListDefaults(int); +}; + +void il_other_params_defaulted() { + InitListDefaults x(1); +} + +void il_std_string() { + std::string s("hello"); +} + +void il_std_string_count_char() { + std::string s(3, 'a'); +} + +void il_std_vector() { + std::vector<int> v(5); +} + +void il_std_vector_count_value() { + std::vector<int> v(5, 1); +} + +void il_std_vector_temporary() { + std::vector<int>(5); +} + +void il_std_vector_already_braced() { + std::vector<int> v{1, 2, 3}; +} + +void il_new_std_vector() { + std::vector<int> *p = new std::vector<int>(5); + (void)p; +} + +void il_braced_arg() { + InitListByValue x({1, 2, 3}); +} + +struct InitListSecondParam { + InitListSecondParam(int, std::initializer_list<int>); + InitListSecondParam(int, int); +}; + +void il_not_first_param() { + InitListSecondParam x(1, 2); + // CHECK-MESSAGES: :[[@LINE-1]]:23: warning: use braced initialization + // CHECK-FIXES: InitListSecondParam x{1, 2}; +} + +struct InitListPointer { + InitListPointer(std::initializer_list<int> *); + InitListPointer(int); +}; + +void il_pointer() { + InitListPointer x(1); + // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use braced initialization + // CHECK-FIXES: InitListPointer x{1}; +} + +struct InitListNoDefaults { + InitListNoDefaults(std::initializer_list<int>, int); + InitListNoDefaults(int); +}; + +void il_other_params_no_defaults() { + InitListNoDefaults x(1); + // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use braced initialization + // CHECK-FIXES: InitListNoDefaults x{1}; +} + +struct TemplateCtor { + TemplateCtor(int); + template <class T> TemplateCtor(T); +}; + +void il_template_ctor() { + TemplateCtor x(1); + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use braced initialization + // CHECK-FIXES: TemplateCtor x{1}; +} >From 3d67c45f35f6c448384abcdd042bf1466562639e Mon Sep 17 00:00:00 2001 From: Baranov Victor <[email protected]> Date: Sun, 1 Mar 2026 18:11:05 +0300 Subject: [PATCH 2/2] Apply suggestion from @vbvictor --- .../docs/clang-tidy/checks/misc/use-braced-initialization.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc/use-braced-initialization.rst b/clang-tools-extra/docs/clang-tidy/checks/misc/use-braced-initialization.rst index 4df9480b614b0..c19b0f038786e 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/misc/use-braced-initialization.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/misc/use-braced-initialization.rst @@ -61,6 +61,6 @@ semantics: References ---------- -This check corresponds to the CERT C++ Core Guidelines rule +This check corresponds to the C++ Core Guidelines rule `C++ Core Guidelines ES.23 <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es23-prefer-the--initializer-syntax>`_. _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
