@@ -4,12 +4,12 @@ misc-const-correctness
4
4
======================
5
5
6
6
This check implements detection of local variables which could be declared as
7
- ``const ``, but are not. Declaring variables as ``const `` is required or recommended by many
7
+ ``const `` but are not. Declaring variables as ``const `` is required or recommended by many
8
8
coding guidelines, such as:
9
9
`CppCoreGuidelines ES.25 <https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es25-declare-an-object-const-or-constexpr-unless-you-want-to-modify-its-value-later-on >`_
10
10
and `AUTOSAR C++14 Rule A7-1-1 (6.7.1 Specifiers) <https://www.autosar.org/fileadmin/user_upload/standards/adaptive/17-03/AUTOSAR_RS_CPP14Guidelines.pdf >`_.
11
11
12
- Please note that this analysis is type-based only. Variables that are not modified
12
+ Please note that this check's analysis is type-based only. Variables that are not modified
13
13
but used to create a non-const handle that might escape the scope are not diagnosed
14
14
as potential ``const ``.
15
15
@@ -18,44 +18,50 @@ as potential ``const``.
18
18
// Declare a variable, which is not ``const `` ...
19
19
int i = 42;
20
20
// but use it as read-only. This means that `i ` can be declared ``const ``.
21
- int result = i * i;
21
+ int result = i * i; // Before transformation
22
+ int const result = i * i; // After transformation
22
23
23
- The check can analyzes values, pointers and references but not (yet) pointees:
24
+ The check can analyze values, pointers and references but not (yet) pointees:
24
25
25
26
.. code-block :: c++
26
27
27
28
// Normal values like built-ins or objects.
28
- int potential_const_int = 42; // 'const int potential_const_int = 42' suggestion.
29
+ int potential_const_int = 42; // Before transformation
30
+ int const potential_const_int = 42; // After transformation
29
31
int copy_of_value = potential_const_int;
30
32
31
- MyClass could_be_const; // 'const MyClass could_be_const' suggestion;
33
+ MyClass could_be_const; // Before transformation
34
+ MyClass const could_be_const; // After transformation
32
35
could_be_const.const_qualified_method();
33
36
34
37
// References can be declared const as well.
35
- int &reference_value = potential_const_int; // 'const int &reference_value' suggestion.
38
+ int &reference_value = potential_const_int; // Before transformation
39
+ int const& reference_value = potential_const_int; // After transformation
36
40
int another_copy = reference_value;
37
41
38
42
// The similar semantics of pointers are not (yet) analyzed.
39
- int *pointer_variable = &potential_const_int; // Not 'const int *pointer_variable' suggestion.
43
+ int *pointer_variable = &potential_const_int; // _NO_ 'const int *pointer_variable' suggestion.
40
44
int last_copy = *pointer_variable;
41
45
42
46
The automatic code transformation is only applied to variables that are declared in single
43
47
declarations. You may want to prepare your code base with
44
48
`readability-isolate-declaration <readability-isolate-declaration.html >`_ first.
45
49
46
50
Note that there is the check
47
- `cppcoreguidelines-avoid-non-const-global-variables <cppcoreguidelines- avoid-non-const-global-variables.html >`_
51
+ `cppcoreguidelines-avoid-non-const-global-variables <../ cppcoreguidelines/ avoid-non-const-global-variables.html >`_
48
52
to enforce ``const `` correctness on all globals.
49
53
50
54
Known Limitations
51
55
-----------------
52
56
57
+ The check does not run on `C ` code.
58
+
53
59
The check will not analyze templated variables or variables that are instantiation dependent.
54
60
Different instantiations can result in different ``const `` correctness properties and in general it
55
- is not possible to find all instantiations of a template. It might be used differently in an
56
- independent translation unit.
61
+ is not possible to find all instantiations of a template. The template might be used differently in
62
+ an independent translation unit.
57
63
58
- Pointees can not be analyzed for constness yet. The following code is shows this limitation.
64
+ Pointees can not be analyzed for constness yet. The following code shows this limitation.
59
65
60
66
.. code-block :: c++
61
67
@@ -74,44 +80,72 @@ This limitation affects the capability to add ``const`` to methods which is not
74
80
Options
75
81
-------
76
82
77
- .. option :: AnalyzeValues (default = 1 )
83
+ .. option :: AnalyzeValues (default = true )
78
84
79
85
Enable or disable the analysis of ordinary value variables, like ``int i = 42; ``
80
86
81
- .. option :: AnalyzeReferences (default = 1)
87
+ .. code-block :: c++
88
+
89
+ // Warning
90
+ int i = 42;
91
+ // No warning
92
+ int const i = 42;
93
+
94
+ // Warning
95
+ int a[] = {42, 42, 42};
96
+ // No warning
97
+ int const a[] = {42, 42, 42};
98
+
99
+ .. option :: AnalyzeReferences (default = true)
82
100
83
101
Enable or disable the analysis of reference variables, like ``int &ref = i; ``
84
102
85
- .. option :: WarnPointersAsValues (default = 0)
103
+ .. code-block :: c++
104
+
105
+ int i = 42;
106
+ // Warning
107
+ int& ref = i;
108
+ // No warning
109
+ int const& ref = i;
110
+
111
+ .. option :: WarnPointersAsValues (default = false)
86
112
87
113
This option enables the suggestion for ``const `` of the pointer itself.
88
114
Pointer values have two possibilities to be ``const ``, the pointer
89
115
and the value pointing to.
90
116
91
117
.. code-block :: c++
92
118
93
- const int value = 42;
94
- const int * const pointer_variable = &value;
119
+ int value = 42;
95
120
96
- // The following operations are forbidden for `pointer_variable `.
97
- // *pointer_variable = 44;
98
- // pointer_variable = nullptr;
121
+ // Warning
122
+ const int * pointer_variable = &value;
123
+ // No warning
124
+ const int *const pointer_variable = &value;
99
125
100
- .. option :: TransformValues (default = 1 )
126
+ .. option :: TransformValues (default = true )
101
127
102
- Provides fixit-hints for value types that automatically adds ``const `` if its a single declaration.
128
+ Provides fixit-hints for value types that automatically add ``const `` if its a single declaration.
103
129
104
130
.. code-block :: c++
105
131
106
- // Emits a hint for 'value' to become 'const int value = 42;'.
132
+ // Before
107
133
int value = 42;
134
+ // After
135
+ int const value = 42;
136
+
137
+ // Before
138
+ int a[] = {42, 42, 42};
139
+ // After
140
+ int const a[] = {42, 42, 42};
141
+
108
142
// Result is modified later in its life-time. No diagnostic and fixit hint will be emitted.
109
143
int result = value * 3;
110
144
result -= 10;
111
145
112
- .. option :: TransformReferences (default = 1 )
146
+ .. option :: TransformReferences (default = true )
113
147
114
- Provides fixit-hints for reference types that automatically adds ``const `` if its a single
148
+ Provides fixit-hints for reference types that automatically add ``const `` if its a single
115
149
declaration.
116
150
117
151
.. code-block :: c++
@@ -120,31 +154,45 @@ Options
120
154
// it, it can not be transformed (yet).
121
155
int value = 42;
122
156
// The reference 'ref_value' is not modified and can be made 'const int &ref_value = value;'
157
+ // Before
123
158
int &ref_value = value;
159
+ // After
160
+ int const &ref_value = value;
124
161
125
162
// Result is modified later in its life-time. No diagnostic and fixit hint will be emitted.
126
163
int result = ref_value * 3;
127
164
result -= 10;
128
165
129
- .. option :: TransformPointersAsValues (default = 0 )
166
+ .. option :: TransformPointersAsValues (default = false )
130
167
131
168
Provides fixit-hints for pointers if their pointee is not changed. This does not analyze if the
132
169
value-pointed-to is unchanged!
133
170
134
- Requires 'WarnPointersAsValues' to be 1 .
171
+ Requires 'WarnPointersAsValues' to be 'true' .
135
172
136
173
.. code-block :: c++
137
174
138
175
int value = 42;
139
- // Emits a hint that 'ptr_value' may become 'int *const ptr_value = &value' because its pointee
140
- // is not changed.
176
+
177
+ // Before
178
+ const int * pointer_variable = &value;
179
+ // After
180
+ const int *const pointer_variable = &value;
181
+
182
+ // Before
183
+ const int * a[] = {&value, &value};
184
+ // After
185
+ const int *const a[] = {&value, &value};
186
+
187
+ // Before
141
188
int *ptr_value = &value;
189
+ // After
190
+ int *const ptr_value = &value;
142
191
143
- int result = 100 * (*ptr_value);
144
- // This modification of the pointee is still allowed and not analyzed/ diagnosed.
192
+ int result = 100 * (*ptr_value); // Does not modify the pointer itself.
193
+ // This modification of the pointee is still allowed and not diagnosed.
145
194
*ptr_value = 0;
146
195
147
196
// The following pointer may not become a 'int *const'.
148
197
int *changing_pointee = &value;
149
198
changing_pointee = &result;
150
-
0 commit comments