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

Arthur O'Dwyer via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 9 19:15:23 PDT 2018


Quuxplusone added a comment.

The cult of "no magic numbers" is horrible and we should be trying to //deprogram// its adherents, not create a whole new generation of them. I would be happy if this clang-tidy patch were quickly abandoned. //But//, it's just a clang-tidy check — it's easy for people who don't want it to ignore its existence — so I'll just plan to be in that group of people.



================
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]
----------------
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?


================
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]
----------------
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.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114





More information about the cfe-commits mailing list