Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
//===----------------------------------------------------------------------===//

#include "InvalidEnumDefaultInitializationCheck.h"
// #include "../utils/Matchers.h"
// #include "../utils/OptionsUtils.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include <algorithm>
Expand All @@ -21,7 +19,7 @@ namespace {

AST_MATCHER(EnumDecl, isCompleteAndHasNoZeroValue) {
const EnumDecl *Definition = Node.getDefinition();
return Definition && Node.isComplete() &&
return Definition && Node.isComplete() && !Node.enumerators().empty() &&
std::none_of(Definition->enumerator_begin(),
Definition->enumerator_end(),
[](const EnumConstantDecl *Value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace clang::tidy::bugprone {
/// the enum has no enumerator with value of 0.
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/.html
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/invalid-enum-default-initialization.html
class InvalidEnumDefaultInitializationCheck : public ClangTidyCheck {
public:
InvalidEnumDefaultInitializationCheck(StringRef Name,
Expand Down
6 changes: 6 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ New checks
Finds potentially erroneous calls to ``reset`` method on smart pointers when
the pointee type also has a ``reset`` method.

- New :doc:`bugprone-invalid-enum-default-initialization
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep alphabetical order (by check name) in this list.

<clang-tidy/checks/bugprone/invalid-enum-default-initialization>` check.

Detect default initialization (to 0) of variables with ``enum`` type where
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Detect default initialization (to 0) of variables with ``enum`` type where
Detects default initialization (to 0) of variables with ``enum`` type where

the enum has no enumerator with value of 0.

New check aliases
^^^^^^^^^^^^^^^^^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
bugprone-invalid-enum-default-initialization
============================================

Detect default initialization (to 0) of variables with `enum` type where
Detect default initialization (to 0) of variables with ``enum`` type where
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Detect default initialization (to 0) of variables with ``enum`` type where
Detects default initialization (to 0) of variables with ``enum`` type where

the enum has no enumerator with value of 0.

In C++ a default initialization is performed if a variable is initialized with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,20 @@ S4 VarS4{};
// CHECK-NOTES: :8: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

enum class EnumFwd;

EnumFwd Fwd{};

enum class EnumEmpty {};

EnumEmpty Empty{};

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
};

Templ<Enum1> TemplVar;
Loading