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), @@ -125,8 +125,20 @@ 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. + // Ignore this instance, because this matches an + // expanded class enumeration value. + if (Parent.get() && + llvm::any_of( + Result.Context->getParents(Parent), + [](const DynTypedNode &GrandParent) { + return GrandParent.get() != + nullptr; + })) + 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; 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 synthetically + 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-todo.cpp =================================================================== --- /dev/null +++ clang-tools-extra/test/clang-tidy/checkers/readability-magic-numbers-todo.cpp @@ -0,0 +1,15 @@ +// RUN: %check_clang_tidy %s readability-magic-numbers %t -- +// XFAIL: * + +int ProcessSomething(int input); + +int DoWork() +{ + if (((int)4) > ProcessSomething(10)) + // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: 4 is a magic number; consider replacing it with a named constant [readability-magic-numbers] + return 0; + + return 0; +} + + 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 @@ -59,7 +59,7 @@ const int anotherConstant; }; -int ValueArray[] = {3, 5}; +int ValueArray[] = {3, 5, 0, 0, 0}; // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers] // CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers] @@ -183,7 +183,7 @@ const geometry::Rectangle mandelbrotCanvas{geometry::Point{-2.5, -1}, geometry::Dimension{3.5, 2}}; -// Simulate the macro magic in Google Test internal headers +// Simulate the macro magic in Google Test internal headers. class AssertionHelper { public: AssertionHelper(const char *Message, int LineNumber) : Message(Message), LineNumber(LineNumber) {} @@ -201,7 +201,7 @@ ASSERTION_HELPER("here and now"); } -// prove that integer literals introduced by the compiler are accepted silently +// Prove that integer literals introduced by the compiler are accepted silently. extern int ConsumeString(const char *Input); const char *SomeStrings[] = {"alpha", "beta", "gamma"}; @@ -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;