| 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 of C++ Core Guidelines <https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Ri-global>`_ . |
| 7 |
As `R.6 of C++ Core Guidelines <https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-global>`_ is a duplicate of rule I.2 it also covers that rule. |
| 8 |
|
| 9 |
.. code-block:: c++ |
| 10 |
|
| 11 |
char a; // Warns! |
| 12 |
const char b = 0; |
| 13 |
|
| 14 |
namespace some_namespace |
| 15 |
{ |
| 16 |
char c; // Warns! |
| 17 |
const char d = 0; |
| 18 |
} |
| 19 |
|
| 20 |
char * c_ptr1 = &some_namespace::c; // Warns! |
| 21 |
char *const c_const_ptr = &some_namespace::c; // Warns! |
| 22 |
char & c_reference = some_namespace::c; // Warns! |
| 23 |
|
| 24 |
class Foo // No Warnings inside Foo, only namespace scope is covered |
| 25 |
{ |
| 26 |
public: |
| 27 |
char e = 0; |
| 28 |
const char f = 0; |
| 29 |
protected: |
| 30 |
char g = 0; |
| 31 |
private: |
| 32 |
char h = 0; |
| 33 |
}; |
| 34 |
|
| 35 |
Variables: ``a``, ``c``, ``c_ptr1``, ``c_ptr2``, ``c_const_ptr`` and |
| 36 |
``c_reference``, will all generate warnings since they are either: |
| 37 |
a globally accessible variable and non-const, a pointer or reference providing |
| 38 |
global access to non-const data or both. |