[PATCH] D145778: [clang-tidy] Change readability-magic-numbers to allow numbers in type aliases.
Florian Humblot via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 10 02:31:06 PST 2023
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);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145778.504068.patch
Type: text/x-patch
Size: 3081 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230310/9bc5738e/attachment-0001.bin>
More information about the cfe-commits
mailing list