https://github.com/felix642 updated https://github.com/llvm/llvm-project/pull/159220
From 8177f228f3d428336d802139eebff70426067b5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= <felix-antoine.constan...@comact.com> Date: Tue, 16 Sep 2025 21:10:25 -0400 Subject: [PATCH 1/7] =?UTF-8?q?[clang-tidy]=C2=A0New=20Option=20Invalid=20?= =?UTF-8?q?Enum=20Default=20Initialization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added a new Option IgnoredEnums to bugprone invalid enum default initialization to limit the scope of the analysis. This is needed to remove warnings on enums like std::errc where the enum doesn't define a value of 0, but is still used to check if some function calls like std::from_chars are executed correctly. --- .../InvalidEnumDefaultInitializationCheck.cpp | 18 +++++- .../InvalidEnumDefaultInitializationCheck.h | 4 ++ clang-tools-extra/docs/ReleaseNotes.rst | 4 ++ .../invalid-enum-default-initialization.rst | 5 ++ .../invalid-enum-default-initialization.cpp | 56 +++++++++++-------- 5 files changed, 61 insertions(+), 26 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.cpp index 1e657888b0fc0..55bcec27b1863 100644 --- a/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.cpp @@ -7,6 +7,8 @@ //===----------------------------------------------------------------------===// #include "InvalidEnumDefaultInitializationCheck.h" +#include "../utils/Matchers.h" +#include "../utils/OptionsUtils.h" #include "clang/AST/ASTContext.h" #include "clang/AST/TypeVisitor.h" #include "clang/ASTMatchers/ASTMatchFinder.h" @@ -88,12 +90,22 @@ class FindEnumMember : public TypeVisitor<FindEnumMember, bool> { InvalidEnumDefaultInitializationCheck::InvalidEnumDefaultInitializationCheck( StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + : ClangTidyCheck(Name, Context), + IgnoredEnums(utils::options::parseStringList( + Options.get("IgnoredEnums", "::std::errc"))) {} + +void InvalidEnumDefaultInitializationCheck::storeOptions( + ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "IgnoredEnums", + utils::options::serializeStringList(IgnoredEnums)); +} void InvalidEnumDefaultInitializationCheck::registerMatchers( MatchFinder *Finder) { - auto EnumWithoutZeroValue = enumType( - hasDeclaration(enumDecl(isCompleteAndHasNoZeroValue()).bind("enum"))); + auto EnumWithoutZeroValue = enumType(hasDeclaration( + enumDecl(isCompleteAndHasNoZeroValue(), + unless(matchers::matchesAnyListedName(IgnoredEnums))) + .bind("enum"))); auto EnumOrArrayOfEnum = qualType(hasUnqualifiedDesugaredType( anyOf(EnumWithoutZeroValue, arrayType(hasElementType(qualType( diff --git a/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.h b/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.h index b9b4f20d111fc..af4a97ae12cec 100644 --- a/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.h @@ -24,6 +24,10 @@ class InvalidEnumDefaultInitializationCheck : public ClangTidyCheck { ClangTidyContext *Context); void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + void storeOptions(ClangTidyOptions::OptionMap &Opts) override; + +private: + const std::vector<StringRef> IgnoredEnums; }; } // namespace clang::tidy::bugprone diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 3f403c42a168a..a678be2dbb4e9 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -219,6 +219,10 @@ Changes in existing checks <clang-tidy/checks/bugprone/infinite-loop>` check by adding detection for variables introduced by structured bindings. +- Improved :doc:`bugprone-invalid-enum-default-initialization + <clang-tidy/checks/bugprone/invalid-enum-default-initialization>` with new + ``IgnoredEnums`` option to ignore some enums during analysis. + - Improved :doc:`bugprone-narrowing-conversions <clang-tidy/checks/bugprone/narrowing-conversions>` check by fixing false positive from analysis of a conditional expression in C. diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst index a3bd2b6d85c37..92a51d5ec0a31 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst @@ -70,3 +70,8 @@ enum type) are set to 0. enum Enum1 Array3[2][2] = {{Enum1_A, Enum1_A}}; // warn: elements of second array are initialized to 0 struct Struct1 S1 = {1}; // warn: element 'b' is initialized to 0 + +.. option:: IgnoredContainers + +Semicolon-separated list of enums regexp for which this check won't be +enforced. Default is `::std::errc`. diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/invalid-enum-default-initialization.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/invalid-enum-default-initialization.cpp index eb3d5632eaef7..54e37fa32e187 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/invalid-enum-default-initialization.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/invalid-enum-default-initialization.cpp @@ -1,4 +1,5 @@ -// RUN: %check_clang_tidy -std=c++17 %s bugprone-invalid-enum-default-initialization %t +// RUN: %check_clang_tidy -check-suffixes=,DEFAULT -std=c++17 %s bugprone-invalid-enum-default-initialization %t +// RUN: %check_clang_tidy -std=c++17 %s bugprone-invalid-enum-default-initialization %t -- -config="{CheckOptions: {bugprone-invalid-enum-default-initialization.IgnoredEnums: '::MyEnum'}}" enum class Enum0: int { A = 0, @@ -24,10 +25,10 @@ Enum0 E0_6{Enum0::B}; Enum1 E1_1{}; // CHECK-NOTES: :[[@LINE-1]]:11: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator -// CHECK-NOTES: :8:12: note: enum is defined here +// CHECK-NOTES: :9:12: note: enum is defined here Enum1 E1_2 = Enum1(); // CHECK-NOTES: :[[@LINE-1]]:14: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator -// CHECK-NOTES: :8:12: note: enum is defined here +// CHECK-NOTES: :9:12: note: enum is defined here Enum1 E1_3; Enum1 E1_4{0}; Enum1 E1_5{Enum1::A}; @@ -35,44 +36,44 @@ Enum1 E1_6{Enum1::B}; Enum2 E2_1{}; // CHECK-NOTES: :[[@LINE-1]]:11: warning: enum value of type 'Enum2' initialized with invalid value of 0, enum doesn't have a zero-value enumerator -// CHECK-NOTES: :13:6: note: enum is defined here +// CHECK-NOTES: :14:6: note: enum is defined here Enum2 E2_2 = Enum2(); // CHECK-NOTES: :[[@LINE-1]]:14: warning: enum value of type 'Enum2' initialized with invalid value of 0, enum doesn't have a zero-value enumerator -// CHECK-NOTES: :13:6: note: enum is defined here +// CHECK-NOTES: :14:6: note: enum is defined here void f1() { static Enum1 S; // FIMXE: warn for this? Enum1 A; Enum1 B = Enum1(); // CHECK-NOTES: :[[@LINE-1]]:13: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator - // CHECK-NOTES: :8:12: note: enum is defined here + // CHECK-NOTES: :9:12: note: enum is defined here int C = int(); } void f2() { Enum1 A{}; // CHECK-NOTES: :[[@LINE-1]]:10: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator - // CHECK-NOTES: :8:12: note: enum is defined here + // CHECK-NOTES: :9:12: note: enum is defined here Enum1 B = Enum1(); // CHECK-NOTES: :[[@LINE-1]]:13: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator - // CHECK-NOTES: :8:12: note: enum is defined here + // CHECK-NOTES: :9:12: note: enum is defined here Enum1 C[5] = {{}}; // CHECK-NOTES: :[[@LINE-1]]:16: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator - // CHECK-NOTES: :8:12: note: enum is defined here + // CHECK-NOTES: :9:12: note: enum is defined here // CHECK-NOTES: :[[@LINE-3]]:17: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator - // CHECK-NOTES: :8:12: note: enum is defined here + // CHECK-NOTES: :9:12: note: enum is defined here Enum1 D[5] = {}; // FIMXE: warn for this? // CHECK-NOTES: :[[@LINE-1]]:16: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator - // CHECK-NOTES: :8:12: note: enum is defined here + // CHECK-NOTES: :9:12: note: enum is defined here } struct S1 { Enum1 E_1{}; // CHECK-NOTES: :[[@LINE-1]]:12: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator - // CHECK-NOTES: :8:12: note: enum is defined here + // CHECK-NOTES: :9:12: note: enum is defined here Enum1 E_2 = Enum1(); // CHECK-NOTES: :[[@LINE-1]]:15: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator - // CHECK-NOTES: :8:12: note: enum is defined here + // CHECK-NOTES: :9:12: note: enum is defined here Enum1 E_3; Enum1 E_4; Enum1 E_5; @@ -80,10 +81,10 @@ struct S1 { S1() : E_3{}, // CHECK-NOTES: :[[@LINE-1]]:8: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator - // CHECK-NOTES: :8:12: note: enum is defined here + // CHECK-NOTES: :9:12: note: enum is defined here E_4(), // CHECK-NOTES: :[[@LINE-1]]:8: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator - // CHECK-NOTES: :8:12: note: enum is defined here + // CHECK-NOTES: :9:12: note: enum is defined here E_5{Enum1::B} {} }; @@ -110,22 +111,22 @@ struct S5 { S2 VarS2{}; // CHECK-NOTES: :[[@LINE-1]]:9: warning: enum value of type 'Enum1' initialized with invalid value of 0 -// CHECK-NOTES: :8:12: note: enum is defined here +// CHECK-NOTES: :9:12: note: enum is defined here // CHECK-NOTES: :[[@LINE-3]]:9: warning: enum value of type 'Enum2' initialized with invalid value of 0 -// CHECK-NOTES: :13:6: note: enum is defined here +// CHECK-NOTES: :14:6: note: enum is defined here S3 VarS3{}; // CHECK-NOTES: :[[@LINE-1]]:10: warning: enum value of type 'Enum1' initialized with invalid value of 0 -// CHECK-NOTES: :8:12: note: enum is defined here +// CHECK-NOTES: :9:12: note: enum is defined here // CHECK-NOTES: :[[@LINE-3]]:10: warning: enum value of type 'Enum2' initialized with invalid value of 0 -// CHECK-NOTES: :13:6: note: enum is defined here +// CHECK-NOTES: :14:6: note: enum is defined here S4 VarS4{}; // CHECK-NOTES: :[[@LINE-1]]:10: warning: enum value of type 'Enum1' initialized with invalid value of 0 -// CHECK-NOTES: :8:12: note: enum is defined here +// CHECK-NOTES: :9:12: note: enum is defined here // CHECK-NOTES: :[[@LINE-3]]:10: warning: enum value of type 'Enum2' initialized with invalid value of 0 -// CHECK-NOTES: :13:6: note: enum is defined here +// CHECK-NOTES: :14:6: note: enum is defined here S5 VarS5{}; // CHECK-NOTES: :[[@LINE-1]]:10: warning: enum value of type 'Enum1' initialized with invalid value of 0 -// CHECK-NOTES: :8:12: note: enum is defined here +// CHECK-NOTES: :9:12: note: enum is defined here enum class EnumFwd; @@ -139,7 +140,16 @@ template<typename T> struct Templ { T Mem1{}; // CHECK-NOTES: :[[@LINE-1]]:9: warning: enum value of type 'Enum1' initialized with invalid value of 0 - // CHECK-NOTES: :8:12: note: enum is defined here + // CHECK-NOTES: :9:12: note: enum is defined here }; Templ<Enum1> TemplVar; + +enum MyEnum { + A = 1, + B +}; + +MyEnum MyEnumVar{}; +// CHECK-NOTES-DEFAULT: :[[@LINE-1]]:17: warning: enum value of type 'MyEnum' initialized with invalid value of 0, enum doesn't have a zero-value enumerator +// CHECK-NOTES-DEFAULT: :148:6: note: enum is defined here From d9f998b01e672aef35678b0649f1a500070ffd53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= <felix-antoine.constan...@comact.com> Date: Wed, 17 Sep 2025 11:04:41 -0400 Subject: [PATCH 2/7] =?UTF-8?q?fixup!=20[clang-tidy]=C2=A0New=20Option=20I?= =?UTF-8?q?nvalid=20Enum=20Default=20Initialization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Code review --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- .../checks/bugprone/invalid-enum-default-initialization.rst | 6 +++--- .../bugprone/invalid-enum-default-initialization.cpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index a678be2dbb4e9..f7eb616bef938 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -221,7 +221,7 @@ Changes in existing checks - Improved :doc:`bugprone-invalid-enum-default-initialization <clang-tidy/checks/bugprone/invalid-enum-default-initialization>` with new - ``IgnoredEnums`` option to ignore some enums during analysis. + `IgnoredEnums` option to ignore some enums during analysis. - Improved :doc:`bugprone-narrowing-conversions <clang-tidy/checks/bugprone/narrowing-conversions>` check by fixing diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst index 92a51d5ec0a31..6db5c4a0e75f6 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst @@ -71,7 +71,7 @@ enum type) are set to 0. struct Struct1 S1 = {1}; // warn: element 'b' is initialized to 0 -.. option:: IgnoredContainers +.. option:: IgnoredEnums -Semicolon-separated list of enums regexp for which this check won't be -enforced. Default is `::std::errc`. + Semicolon-separated list of regexes specifying enums for which this check won't be + enforced. Default is `::std::errc`. diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/invalid-enum-default-initialization.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/invalid-enum-default-initialization.cpp index 54e37fa32e187..345f0d3857569 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/invalid-enum-default-initialization.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/invalid-enum-default-initialization.cpp @@ -1,5 +1,5 @@ -// RUN: %check_clang_tidy -check-suffixes=,DEFAULT -std=c++17 %s bugprone-invalid-enum-default-initialization %t -// RUN: %check_clang_tidy -std=c++17 %s bugprone-invalid-enum-default-initialization %t -- -config="{CheckOptions: {bugprone-invalid-enum-default-initialization.IgnoredEnums: '::MyEnum'}}" +// RUN: %check_clang_tidy -check-suffixes=,DEFAULT -std=c++17-or-later %s bugprone-invalid-enum-default-initialization %t +// RUN: %check_clang_tidy -std=c++17-or-later %s bugprone-invalid-enum-default-initialization %t -- -config="{CheckOptions: {bugprone-invalid-enum-default-initialization.IgnoredEnums: '::MyEnum'}}" enum class Enum0: int { A = 0, From e1df40020274335624ee256dd4453838ff31fd8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= <felix-antoine.constan...@comact.com> Date: Wed, 17 Sep 2025 11:58:01 -0400 Subject: [PATCH 3/7] =?UTF-8?q?fixup!=20[clang-tidy]=C2=A0New=20Option=20I?= =?UTF-8?q?nvalid=20Enum=20Default=20Initialization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Code Review --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- .../checks/bugprone/invalid-enum-default-initialization.rst | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index f7eb616bef938..b922d064bf52b 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -221,7 +221,7 @@ Changes in existing checks - Improved :doc:`bugprone-invalid-enum-default-initialization <clang-tidy/checks/bugprone/invalid-enum-default-initialization>` with new - `IgnoredEnums` option to ignore some enums during analysis. + `IgnoredEnums` option to ignore specified enums during analysis. - Improved :doc:`bugprone-narrowing-conversions <clang-tidy/checks/bugprone/narrowing-conversions>` check by fixing diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst index 6db5c4a0e75f6..5344a289cc8fd 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst @@ -71,6 +71,10 @@ enum type) are set to 0. struct Struct1 S1 = {1}; // warn: element 'b' is initialized to 0 + +Options +------- + .. option:: IgnoredEnums Semicolon-separated list of regexes specifying enums for which this check won't be From 68ce85eebbb9366c8eb83762b07fa7bbcb2c5fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= <felix-antoine.constan...@comact.com> Date: Sun, 21 Sep 2025 12:27:13 -0400 Subject: [PATCH 4/7] =?UTF-8?q?fixup!=20[clang-tidy]=C2=A0New=20Option=20I?= =?UTF-8?q?nvalid=20Enum=20Default=20Initialization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implicitely add std::errc to the IgnoredEnums --- .../bugprone/InvalidEnumDefaultInitializationCheck.cpp | 4 +++- .../bugprone/InvalidEnumDefaultInitializationCheck.h | 2 +- .../bugprone/invalid-enum-default-initialization.cpp | 9 +++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.cpp index 55bcec27b1863..c061ea5eba269 100644 --- a/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.cpp @@ -92,7 +92,9 @@ InvalidEnumDefaultInitializationCheck::InvalidEnumDefaultInitializationCheck( StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context), IgnoredEnums(utils::options::parseStringList( - Options.get("IgnoredEnums", "::std::errc"))) {} + Options.get("IgnoredEnums", ""))) { + IgnoredEnums.emplace_back("::std::errc"); + } void InvalidEnumDefaultInitializationCheck::storeOptions( ClangTidyOptions::OptionMap &Opts) { diff --git a/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.h b/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.h index af4a97ae12cec..e7c19071d2135 100644 --- a/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.h @@ -27,7 +27,7 @@ class InvalidEnumDefaultInitializationCheck : public ClangTidyCheck { void storeOptions(ClangTidyOptions::OptionMap &Opts) override; private: - const std::vector<StringRef> IgnoredEnums; + std::vector<StringRef> IgnoredEnums; }; } // namespace clang::tidy::bugprone diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/invalid-enum-default-initialization.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/invalid-enum-default-initialization.cpp index 345f0d3857569..85ff481aae301 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/invalid-enum-default-initialization.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/invalid-enum-default-initialization.cpp @@ -153,3 +153,12 @@ enum MyEnum { MyEnum MyEnumVar{}; // CHECK-NOTES-DEFAULT: :[[@LINE-1]]:17: warning: enum value of type 'MyEnum' initialized with invalid value of 0, enum doesn't have a zero-value enumerator // CHECK-NOTES-DEFAULT: :148:6: note: enum is defined here + +namespace std { + enum errc { + A = 1, + B + }; +} + +std::errc err{}; From c0d713e856d566bbd49dc9012072ee20c838e17c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= <felix-antoine.constan...@comact.com> Date: Mon, 22 Sep 2025 17:55:08 -0400 Subject: [PATCH 5/7] =?UTF-8?q?fixup!=20fixup!=20[clang-tidy]=C2=A0New=20O?= =?UTF-8?q?ption=20Invalid=20Enum=20Default=20Initialization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Clang-format --- .../bugprone/InvalidEnumDefaultInitializationCheck.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.cpp index c061ea5eba269..4fc1b3b99ece4 100644 --- a/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/InvalidEnumDefaultInitializationCheck.cpp @@ -91,10 +91,10 @@ class FindEnumMember : public TypeVisitor<FindEnumMember, bool> { InvalidEnumDefaultInitializationCheck::InvalidEnumDefaultInitializationCheck( StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context), - IgnoredEnums(utils::options::parseStringList( - Options.get("IgnoredEnums", ""))) { - IgnoredEnums.emplace_back("::std::errc"); - } + IgnoredEnums( + utils::options::parseStringList(Options.get("IgnoredEnums", ""))) { + IgnoredEnums.emplace_back("::std::errc"); +} void InvalidEnumDefaultInitializationCheck::storeOptions( ClangTidyOptions::OptionMap &Opts) { From ce4db01cf16278b9def9cd1273acb29a74ad4784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= <felix-antoine.constan...@comact.com> Date: Wed, 24 Sep 2025 19:51:38 -0400 Subject: [PATCH 6/7] =?UTF-8?q?fixup!=20[clang-tidy]=C2=A0New=20Option=20I?= =?UTF-8?q?nvalid=20Enum=20Default=20Initialization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Improved documentation --- .../checks/bugprone/invalid-enum-default-initialization.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst index 5344a289cc8fd..5f0383652deab 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst @@ -19,6 +19,9 @@ The check emits a warning only if an ``enum`` variable is default-initialized value of 0. The type can be a scoped or non-scoped ``enum``. Unions are not handled by the check (if it contains a member of enumeration type). +Note that the ``enum`` ``std::errc`` is ignored because it is expected to be default +initialized, despite not defining an enumerator with the value 0. + .. code-block:: c++ enum class Enum1: int { From 10f5dd2b1408f37e1a4deca192fbfd746ac7403a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix-Antoine=20Constantin?= <felix-antoine.constan...@comact.com> Date: Thu, 25 Sep 2025 12:32:36 -0400 Subject: [PATCH 7/7] =?UTF-8?q?fixup!=20fixup!=20[clang-tidy]=C2=A0New=20O?= =?UTF-8?q?ption=20Invalid=20Enum=20Default=20Initialization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Documentation --- .../checks/bugprone/invalid-enum-default-initialization.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst index 5f0383652deab..45cb878383a7d 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/invalid-enum-default-initialization.rst @@ -19,8 +19,8 @@ The check emits a warning only if an ``enum`` variable is default-initialized value of 0. The type can be a scoped or non-scoped ``enum``. Unions are not handled by the check (if it contains a member of enumeration type). -Note that the ``enum`` ``std::errc`` is ignored because it is expected to be default -initialized, despite not defining an enumerator with the value 0. +Note that the ``enum`` ``std::errc`` is always ignored because it is expected to +be default initialized, despite not defining an enumerator with the value 0. .. code-block:: c++ _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits