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 @@ -14,6 +14,7 @@ #include "MagicNumbersCheck.h" #include "../utils/OptionsUtils.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/Type.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "llvm/ADT/STLExtras.h" #include @@ -36,6 +37,9 @@ if (Node.get()) return true; + if (Node.get() || Node.get()) + return true; + return llvm::any_of(Result.Context->getParents(Node), [&Result](const DynTypedNode &Parent) { return isUsedToInitializeAConstant(Result, Parent); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -192,6 +192,10 @@ ` check. Basic support for bit-field and integer members as a loop variable or upper limit were added. +- Improved :doc:`readability-magic-numbers + ` check. The check now allows for + magic numbers in type aliases such as ``using`` and ``typedef`` declarations. + Removed checks ^^^^^^^^^^^^^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/magic-numbers.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/magic-numbers.rst --- a/clang-tools-extra/docs/clang-tidy/checks/readability/magic-numbers.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/readability/magic-numbers.rst @@ -24,6 +24,16 @@ .. code-block:: c++ + template + struct CustomType { + T arr[N]; + }; + + struct OtherType { + CustomType container; + } + CustomType values; + double circleArea = 3.1415926535 * radius * radius; double totalCharge = 1.08 * itemPrice; @@ -40,6 +50,17 @@ .. code-block:: c++ + template + struct CustomType { + T arr[N]; + }; + + using containerType = CustomType; + struct OtherType { + containerType container; + } + containerType values; + double circleArea = M_PI * radius * radius; const double TAX_RATE = 0.08; // or make it variable and read from a file 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 @@ -101,6 +101,12 @@ * Clean code */ +/* + * No warning for type aliases + */ +using NumberInTypeAlias = ValueBucket; +typedef ValueBucket NumberInTypedef; + #define INT_MACRO 5 const int GoodGlobalIntConstant = 42;