66 lines · plain
1.. title:: clang-tidy - bugprone-misplaced-widening-cast2 3bugprone-misplaced-widening-cast4================================5 6This check will warn when there is a cast of a calculation result to a bigger7type. If the intention of the cast is to avoid loss of precision then the cast8is misplaced, and there can be loss of precision. Otherwise the cast is9ineffective.10 11Example code:12 13.. code-block:: c++14 15 long f(int x) {16 return (long)(x * 1000);17 }18 19The result ``x * 1000`` is first calculated using ``int`` precision. If the20result exceeds ``int`` precision there is loss of precision. Then the result is21casted to ``long``.22 23If there is no loss of precision then the cast can be removed or you can24explicitly cast to ``int`` instead.25 26If you want to avoid loss of precision then put the cast in a proper location,27for instance:28 29.. code-block:: c++30 31 long f(int x) {32 return (long)x * 1000;33 }34 35Implicit casts36--------------37 38Forgetting to place the cast at all is at least as dangerous and at least as39common as misplacing it. If :option:`CheckImplicitCasts` is enabled the check40also detects these cases, for instance:41 42.. code-block:: c++43 44 long f(int x) {45 return x * 1000;46 }47 48Floating point49--------------50 51Currently warnings are only written for integer conversion. No warning is52written for this code:53 54.. code-block:: c++55 56 double f(float x) {57 return (double)(x * 10.0f);58 }59 60Options61-------62 63.. option:: CheckImplicitCasts64 65 If `true`, enables detection of implicit casts. Default is `false`.66