File: avoid-non-const-global-variables.rst

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (43 lines) | stat: -rw-r--r-- 1,408 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
.. title:: clang-tidy - cppcoreguidelines-avoid-non-const-global-variables

cppcoreguidelines-avoid-non-const-global-variables
==================================================

Finds non-const global variables as described in `I.2
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#i2-avoid-non-const-global-variables>`_
of C++ Core Guidelines.
As `R.6 <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-global>`_
of C++ Core Guidelines is a duplicate of rule `I.2
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#i2-avoid-non-const-global-variables>`_
it also covers that rule.

.. code-block:: c++

    char a;  // Warns!
    const char b =  0;

    namespace some_namespace
    {
        char c;  // Warns!
        const char d = 0;
    }

    char * c_ptr1 = &some_namespace::c;  // Warns!
    char *const c_const_ptr = &some_namespace::c;  // Warns!
    char & c_reference = some_namespace::c;  // Warns!

    class Foo  // No Warnings inside Foo, only namespace scope is covered
    {
    public:
        char e = 0;
        const char f = 0;
    protected:
        char g = 0;
    private:
        char h = 0;
    };

The variables ``a``, ``c``, ``c_ptr1``, ``c_const_ptr`` and ``c_reference``
will all generate warnings since they are either a non-const globally accessible
variable, a pointer or a reference providing global access to non-const data
or both.