brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.9 KiB · e8ca823 Raw
72 lines · plain
1.. title:: clang-tidy - cppcoreguidelines-init-variables2 3cppcoreguidelines-init-variables4================================5 6Checks whether there are local variables that are declared without an initial7value. These may lead to unexpected behavior if there is a code path that reads8the variable before assigning to it.9 10This rule is part of the `Type safety (Type.5)11<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#pro-type-init>`_12profile and `ES.20 <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#res-always>`_13from the C++ Core Guidelines.14 15Only integers, booleans, floats, doubles and pointers are checked. The fix16option initializes all detected values with the value of zero. An exception is17float and double types, which are initialized to NaN.18 19As an example a function that looks like this:20 21.. code-block:: c++22 23   void function() {24     int x;25     char *txt;26     double d;27 28     // Rest of the function.29   }30 31Would be rewritten to look like this:32 33.. code-block:: c++34 35   #include <math.h>36 37   void function() {38     int x = 0;39     char *txt = nullptr;40     double d = NAN;41 42     // Rest of the function.43   }44 45It warns for the uninitialized enum case, but without a FixIt:46 47.. code-block:: c++48 49   enum A {A1, A2, A3};50   enum A_c : char { A_c1, A_c2, A_c3 };51   enum class B { B1, B2, B3 };52   enum class B_i : int { B_i1, B_i2, B_i3 };53   void function() {54     A a;     // Warning: variable 'a' is not initialized55     A_c a_c; // Warning: variable 'a_c' is not initialized56     B b;     // Warning: variable 'b' is not initialized57     B_i b_i; // Warning: variable 'b_i' is not initialized58   }59 60Options61-------62 63.. option:: IncludeStyle64 65   A string specifying which include-style is used, `llvm` or `google`. Default66   is `llvm`.67 68.. option:: MathHeader69 70   A string specifying the header to include to get the definition of `NAN`.71   Default is `<math.h>`.72