| 1 |
.. title:: clang-tidy - cppcoreguidelines-avoid-non-const-global-variables |
| 2 |
|
| 3 |
cppcoreguidelines-avoid-non-const-global-variables |
| 4 |
================================================== |
| 5 |
|
| 6 |
Finds non-const global variables as described in `I.2 |
| 7 |
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#i2-avoid-non-const-global-variables>`_ |
| 8 |
of C++ Core Guidelines. |
| 9 |
As `R.6 <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-global>`_ |
| 10 |
of C++ Core Guidelines is a duplicate of rule `I.2 |
| 11 |
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#i2-avoid-non-const-global-variables>`_ |
| 12 |
it also covers that rule. |
| 13 |
|
| 14 |
.. code-block:: c++ |
| 15 |
|
| 16 |
char a; // Warns! |
| 17 |
const char b = 0; |
| 18 |
|
| 19 |
namespace some_namespace |
| 20 |
{ |
| 21 |
char c; // Warns! |
| 22 |
const char d = 0; |
| 23 |
} |
| 24 |
|
| 25 |
char * c_ptr1 = &some_namespace::c; // Warns! |
| 26 |
char *const c_const_ptr = &some_namespace::c; // Warns! |
| 27 |
char & c_reference = some_namespace::c; // Warns! |
| 28 |
|
| 29 |
class Foo // No Warnings inside Foo, only namespace scope is covered |
| 30 |
{ |
| 31 |
public: |
| 32 |
char e = 0; |
| 33 |
const char f = 0; |
| 34 |
protected: |
| 35 |
char g = 0; |
| 36 |
private: |
| 37 |
char h = 0; |
| 38 |
}; |
| 39 |
|
| 40 |
The variables ``a``, ``c``, ``c_ptr1``, ``c_const_ptr`` and ``c_reference`` |
| 41 |
will all generate warnings since they are either a non-const globally accessible |
| 42 |
variable, a pointer or a reference providing global access to non-const data |
| 43 |
or both. |
| 44 |
|
| 45 |
Options |
| 46 |
------- |
| 47 |
|
| 48 |
.. option:: AllowInternalLinkage |
| 49 |
|
| 50 |
When set to `true`, static non-const variables and variables in anonymous |
| 51 |
namespaces will not generate a warning. The default value is `false`. |