[PATCH] D49114: Add a clang-tidy check for "magic numbers"

Florin Iucha via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 9 20:08:44 PDT 2018


0x8000-0000 added inline comments.


================
Comment at: test/clang-tidy/readability-magic-numbers.cpp:38
+public:
+  TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(2), anotherConstant(val + val) {}
+  // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: magic number integer literal 2 [readability-magic-numbers]
----------------
Quuxplusone wrote:
> How come you diagnose `2 * x` but not `x + x` or `x << 1`? Do you have a rationale for why the former is worse than the latter?
No tool is perfect. The intent is not to unpack any craftily obfuscated code, but to gently guide the design and thinking and reviews..


================
Comment at: test/clang-tidy/readability-magic-numbers.cpp:56
+
+int ValueArray[] = {3, 5};
+// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 3 [readability-magic-numbers]
----------------
Quuxplusone wrote:
> Given that you're trying to diagnose certain kinds of static data arrays and not others, I'd be interested to see what you think ought to happen if you put `{3, 5}` in a context where it creates an `initializer_list` of static lifetime.
The principle I've followed until now is - if you're ultimately defining/constructing a constant object - then it should be acceptable. If you're just seeding the object and those values will change over the program's lifetime, it is better to seed them with named constants.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114





More information about the cfe-commits mailing list