florianhumblot created this revision. Herald added subscribers: PiotrZSL, carlosgalvezp, jeroen.dobbelaere, xazax.hun. Herald added a reviewer: njames93. Herald added a project: All. florianhumblot requested review of this revision. Herald added a project: clang-tools-extra. Herald added a subscriber: cfe-commits.
This commit changes the default behavior of the readability-magic-numbers check to allow "magic" numbers to be used in ``using`` and ``typedef`` declarations. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D145778 Files: clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/docs/clang-tidy/checks/readability/magic-numbers.rst clang-tools-extra/test/clang-tidy/checkers/readability/magic-numbers.cpp 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 @@ -101,6 +101,12 @@ * Clean code */ +/* + * No warning for type aliases + */ +using NumberInTypeAlias = ValueBucket<int, 25>; +typedef ValueBucket<char, 243> NumberInTypedef; + #define INT_MACRO 5 const int GoodGlobalIntConstant = 42; Index: clang-tools-extra/docs/clang-tidy/checks/readability/magic-numbers.rst =================================================================== --- clang-tools-extra/docs/clang-tidy/checks/readability/magic-numbers.rst +++ clang-tools-extra/docs/clang-tidy/checks/readability/magic-numbers.rst @@ -24,6 +24,16 @@ .. code-block:: c++ + template<typename T, size_t N> + struct CustomType { + T arr[N]; + }; + + struct OtherType { + CustomType<int, 30> container; + } + CustomType<int, 30> values; + double circleArea = 3.1415926535 * radius * radius; double totalCharge = 1.08 * itemPrice; @@ -40,6 +50,17 @@ .. code-block:: c++ + template<typename T, size_t N> + struct CustomType { + T arr[N]; + }; + + using containerType = CustomType<int, 30>; + 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 Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -192,6 +192,10 @@ <clang-tidy/checks/bugprone/too-small-loop-variable>` check. Basic support for bit-field and integer members as a loop variable or upper limit were added. +- Improved :doc:`readability-magic-numbers + <clang-tidy/checks/readability/magic-numbers>` check. The check now allows for + magic numbers in type aliases such as ``using`` and ``typedef`` declarations. + Removed checks ^^^^^^^^^^^^^^ 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 @@ -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 <algorithm> @@ -36,6 +37,9 @@ if (Node.get<EnumConstantDecl>()) return true; + if (Node.get<TypeAliasDecl>() || Node.get<TypedefNameDecl>()) + return true; + return llvm::any_of(Result.Context->getParents(Node), [&Result](const DynTypedNode &Parent) { return isUsedToInitializeAConstant(Result, Parent);
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 @@ -101,6 +101,12 @@ * Clean code */ +/* + * No warning for type aliases + */ +using NumberInTypeAlias = ValueBucket<int, 25>; +typedef ValueBucket<char, 243> NumberInTypedef; + #define INT_MACRO 5 const int GoodGlobalIntConstant = 42; Index: clang-tools-extra/docs/clang-tidy/checks/readability/magic-numbers.rst =================================================================== --- clang-tools-extra/docs/clang-tidy/checks/readability/magic-numbers.rst +++ clang-tools-extra/docs/clang-tidy/checks/readability/magic-numbers.rst @@ -24,6 +24,16 @@ .. code-block:: c++ + template<typename T, size_t N> + struct CustomType { + T arr[N]; + }; + + struct OtherType { + CustomType<int, 30> container; + } + CustomType<int, 30> values; + double circleArea = 3.1415926535 * radius * radius; double totalCharge = 1.08 * itemPrice; @@ -40,6 +50,17 @@ .. code-block:: c++ + template<typename T, size_t N> + struct CustomType { + T arr[N]; + }; + + using containerType = CustomType<int, 30>; + 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 Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -192,6 +192,10 @@ <clang-tidy/checks/bugprone/too-small-loop-variable>` check. Basic support for bit-field and integer members as a loop variable or upper limit were added. +- Improved :doc:`readability-magic-numbers + <clang-tidy/checks/readability/magic-numbers>` check. The check now allows for + magic numbers in type aliases such as ``using`` and ``typedef`` declarations. + Removed checks ^^^^^^^^^^^^^^ 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 @@ -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 <algorithm> @@ -36,6 +37,9 @@ if (Node.get<EnumConstantDecl>()) return true; + if (Node.get<TypeAliasDecl>() || Node.get<TypedefNameDecl>()) + return true; + return llvm::any_of(Result.Context->getParents(Node), [&Result](const DynTypedNode &Parent) { return isUsedToInitializeAConstant(Result, Parent);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits