| 1 |
.. title:: clang-tidy - cppcoreguidelines-init-variables |
| 2 |
|
| 3 |
cppcoreguidelines-init-variables |
| 4 |
================================ |
| 5 |
|
| 6 |
Checks whether there are local variables that are declared without an initial |
| 7 |
value. These may lead to unexpected behavior if there is a code path that reads |
| 8 |
the variable before assigning to it. |
| 9 |
|
| 10 |
Only integers, booleans, floats, doubles and pointers are checked. The fix |
| 11 |
option initializes all detected values with the value of zero. An exception is |
| 12 |
float and double types, which are initialized to NaN. |
| 13 |
|
| 14 |
As an example a function that looks like this: |
| 15 |
|
| 16 |
.. code-block:: c++ |
| 17 |
|
| 18 |
void function() { |
| 19 |
int x; |
| 20 |
char *txt; |
| 21 |
double d; |
| 22 |
|
| 23 |
// Rest of the function. |
| 24 |
} |
| 25 |
|
| 26 |
Would be rewritten to look like this: |
| 27 |
|
| 28 |
.. code-block:: c++ |
| 29 |
|
| 30 |
#include <math.h> |
| 31 |
|
| 32 |
void function() { |
| 33 |
int x = 0; |
| 34 |
char *txt = nullptr; |
| 35 |
double d = NAN; |
| 36 |
|
| 37 |
// Rest of the function. |
| 38 |
} |
| 39 |
|
| 40 |
It warns for the uninitialized enum case, but without a FixIt: |
| 41 |
|
| 42 |
.. code-block:: c++ |
| 43 |
|
| 44 |
enum A {A1, A2, A3}; |
| 45 |
enum A_c : char { A_c1, A_c2, A_c3 }; |
| 46 |
enum class B { B1, B2, B3 }; |
| 47 |
enum class B_i : int { B_i1, B_i2, B_i3 }; |
| 48 |
void function() { |
| 49 |
A a; // Warning: variable 'a' is not initialized |
| 50 |
A_c a_c; // Warning: variable 'a_c' is not initialized |
| 51 |
B b; // Warning: variable 'b' is not initialized |
| 52 |
B_i b_i; // Warning: variable 'b_i' is not initialized |
| 53 |
} |
| 54 |
|
| 55 |
Options |
| 56 |
------- |
| 57 |
|
| 58 |
.. option:: IncludeStyle |
| 59 |
|
| 60 |
A string specifying which include-style is used, `llvm` or `google`. Default |
| 61 |
is `llvm`. |
| 62 |
|
| 63 |
.. option:: MathHeader |
| 64 |
|
| 65 |
A string specifying the header to include to get the definition of `NAN`. |
| 66 |
Default is `<math.h>`. |