diff --git a/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp b/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp @@ -34,7 +34,7 @@ return AsDecl->isImplicit(); } - if (Node.get() != nullptr) + if (Node.get()) return true; return llvm::any_of(Result.Context->getParents(Node), @@ -119,25 +119,31 @@ bool MagicNumbersCheck::isConstant(const MatchFinder::MatchResult &Result, const Expr &ExprResult) const { - return llvm::any_of( - Result.Context->getParents(ExprResult), - [&Result](const DynTypedNode &Parent) { - if (isUsedToInitializeAConstant(Result, Parent)) - return true; - - // Ignore this instance, because this match reports the location - // where the template is defined, not where it is instantiated. - if (Parent.get()) - return true; - - // Don't warn on string user defined literals: - // std::string s = "Hello World"s; - if (const auto *UDL = Parent.get()) - if (UDL->getLiteralOperatorKind() == UserDefinedLiteral::LOK_String) - return true; - - return false; - }); + return llvm::any_of(Result.Context->getParents(ExprResult), + [&Result](const DynTypedNode &Parent) { + if (isUsedToInitializeAConstant(Result, Parent)) + return true; + + // Ignore this instance, because this matches an + // expanded class enumeration value. + if (Parent.get()) + return true; + + // Ignore this instance, because this match reports the + // location where the template is defined, not where it + // is instantiated. + if (Parent.get()) + return true; + + // Don't warn on string user defined literals: + // std::string s = "Hello World"s; + if (const auto *UDL = Parent.get()) + if (UDL->getLiteralOperatorKind() == + UserDefinedLiteral::LOK_String) + return true; + + return false; + }); } bool MagicNumbersCheck::isIgnoredValue(const IntegerLiteral *Literal) const { diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability-magic-numbers.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-magic-numbers.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/readability-magic-numbers.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability-magic-numbers.cpp @@ -215,3 +215,14 @@ return Total; } + +// prove that using enumerations values don't produce warnings (code by Pavel Kryukov) +enum class Letter : unsigned { + A, B, C, D, E, F, G, H, I, J +}; + +template struct holder { Letter letter = x; }; +template struct wrapper { using h_type = holder; }; + +template struct wrapper; +template struct wrapper;