57 lines · plain
1.. title:: clang-tidy - cppcoreguidelines-avoid-non-const-global-variables2 3cppcoreguidelines-avoid-non-const-global-variables4==================================================5 6Finds non-const global variables as described in `I.27<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#i2-avoid-non-const-global-variables>`_8of C++ Core Guidelines.9As `R.6 <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#rr-global>`_10of C++ Core Guidelines is a duplicate of rule `I.211<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#i2-avoid-non-const-global-variables>`_12it also covers that rule.13 14.. code-block:: c++15 16 char a; // Warns!17 const char b = 0;18 19 namespace some_namespace20 {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 covered30 {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 40The variables ``a``, ``c``, ``c_ptr1``, ``c_const_ptr`` and ``c_reference``41will all generate warnings since they are either a non-const globally accessible42variable, a pointer or a reference providing global access to non-const data43or both.44 45Options46-------47 48.. option:: AllowInternalLinkage49 50 When set to `true`, static non-const variables and variables in anonymous51 namespaces will not generate a warning. The default value is `false`.52 53.. option:: AllowThreadLocal54 55 When set to `true`, non-const global variables with thread-local storage56 duration will not generate a warning. The default value is `false`.57