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

Florin Iucha via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 18 17:06:24 PDT 2018


0x8000-0000 marked 8 inline comments as done.
0x8000-0000 added inline comments.


================
Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:33
+
+      return -3; // FILENOTFOUND
+   }
----------------
Quuxplusone wrote:
> I notice you changed `-1` to `-3`. Is `return -1` not an example of the "no magic numbers" rule? If not, why not — and can you put something in the documentation about it?
> At least add a unit test proving that `return -1;` is accepted without any diagnostic, if that's the intended behavior.
-1 is parsed as "UnaryOperator" - "IntegerLiteral" 1. I think -1 should be acceptable by default.


================
Comment at: test/clang-tidy/readability-magic-numbers.cpp:16
+void BuggyFunction() {
+  int BadLocalInt = 6;
+  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: magic number integer literal 6 [readability-magic-numbers]
----------------
Quuxplusone wrote:
> Please add test cases immediately following this one, for
> 
>     const int BadLocalConstInt = 6;
>     constexpr int BadLocalConstexprInt = 6;
>     static const int BadLocalStaticConstInt = 6;
>     static constexpr int BadLocalStaticConstexprInt = 6;
> 
> (except of course changing "Bad" to "Good" in any cases where 6 is acceptable as an initializer).
> 
> Also
> 
>     std::vector<int> BadLocalVector(6);
>     const std::vector<int> BadLocalConstVector(6);
> 
> etc. etc.
Again... all the "const .* (\d)+" patterns should be acceptable. We're initializing a constant. Would you prefer an explicit option?


================
Comment at: test/clang-tidy/readability-magic-numbers.cpp:151
+
+const geometry::Rectangle<double> mandelbrotCanvas{geometry::Point<double>{-2.5, -1}, geometry::Dimension<double>{3.5, 2}};
----------------
Quuxplusone wrote:
> Surely you should expect some diagnostics on this line!
I am using those values to initialize a constant, as described earlier.

We can disable this - but this will be a terribly noisy checker.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114





More information about the cfe-commits mailing list