Index: clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp +++ 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 { Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -185,6 +185,10 @@ The check now supports the ``IgnoreBitFieldsWidths`` option to suppress the warning for numbers used to specify bit field widths. + The check was updated to eliminate some false positives (such as using + class enumeration as non-type template parameters, or the syntethically + computed lengh of a static user string literal.) + - New :doc:`readability-make-member-function-const ` check. Index: clang-tools-extra/test/clang-tidy/checkers/readability-magic-numbers.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/readability-magic-numbers.cpp +++ 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 +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;