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

Arthur O'Dwyer via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 11 15:27:01 PDT 2018


Quuxplusone added inline comments.


================
Comment at: docs/clang-tidy/checks/readability-magic-numbers.rst:11
+
+   double circleArea = 3.1415926535 * radius * radius;
+
----------------
0x8000-0000 wrote:
> Quuxplusone wrote:
> > 0x8000-0000 wrote:
> > > Eugene.Zelenko wrote:
> > > > JonasToth wrote:
> > > > > This example is good, but right now the code only checks for integer literals. Maybe an integer example would be better?
> > > > Please use .. code-block:: c++. Same for good example.
> > > @JonasToth I raked my brain but I just can't come up with a short and effective example. I haven't given up yet.
> > A common "magic number" in real code would be something like
> > ```
> > int get_answer() {
> >     if (cond) return x;
> >     return -1;  // FILENOTFOUND
> > }
> > ```
> Thank you. May I use this as an example?
> 
> The other cases I could find involved either "constants" coming from customer requirements that became variable with age, or examples where the same underlying value was buried inside other pre-calculated values that needed to be kept in sync when the base value changed.... but that does not make for a pithy example.
> Thank you. May I use this as an example?

Of course! Just make sure to add it as a unit test also ;)


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49114





More information about the cfe-commits mailing list