Skip to content

Commit d3e48d9

Browse files
JonasTothtru
authored andcommitted
[docs] improve documentation for misc-const-correctness
Improve the documentation for 'misc-const-correctness' to: - include better examples - improve the english - fix links to other checks that were broken due to the directory-layout changes - mention the limitation that the check does not run on `C` code. Addresses #56749, #56958 Reviewed By: njames93 Differential Revision: https://reviews.llvm.org/D132244 (cherry picked from commit b5b7503)
1 parent 0b41e63 commit d3e48d9

File tree

1 file changed

+80
-32
lines changed

1 file changed

+80
-32
lines changed

clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst

Lines changed: 80 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ misc-const-correctness
44
======================
55

66
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
88
coding guidelines, such as:
99
`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>`_
1010
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>`_.
1111

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
1313
but used to create a non-const handle that might escape the scope are not diagnosed
1414
as potential ``const``.
1515

@@ -18,44 +18,50 @@ as potential ``const``.
1818
// Declare a variable, which is not ``const`` ...
1919
int i = 42;
2020
// 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
2223

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:
2425

2526
.. code-block:: c++
2627

2728
// 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
2931
int copy_of_value = potential_const_int;
3032

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
3235
could_be_const.const_qualified_method();
3336

3437
// 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
3640
int another_copy = reference_value;
3741

3842
// 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.
4044
int last_copy = *pointer_variable;
4145
4246
The automatic code transformation is only applied to variables that are declared in single
4347
declarations. You may want to prepare your code base with
4448
`readability-isolate-declaration <readability-isolate-declaration.html>`_ first.
4549

4650
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>`_
4852
to enforce ``const`` correctness on all globals.
4953

5054
Known Limitations
5155
-----------------
5256

57+
The check does not run on `C` code.
58+
5359
The check will not analyze templated variables or variables that are instantiation dependent.
5460
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.
5763

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.
5965

6066
.. code-block:: c++
6167

@@ -74,44 +80,72 @@ This limitation affects the capability to add ``const`` to methods which is not
7480
Options
7581
-------
7682

77-
.. option:: AnalyzeValues (default = 1)
83+
.. option:: AnalyzeValues (default = true)
7884

7985
Enable or disable the analysis of ordinary value variables, like ``int i = 42;``
8086

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)
82100

83101
Enable or disable the analysis of reference variables, like ``int &ref = i;``
84102

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)
86112

87113
This option enables the suggestion for ``const`` of the pointer itself.
88114
Pointer values have two possibilities to be ``const``, the pointer
89115
and the value pointing to.
90116

91117
.. code-block:: c++
92118

93-
const int value = 42;
94-
const int * const pointer_variable = &value;
119+
int value = 42;
95120

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;
99125
100-
.. option:: TransformValues (default = 1)
126+
.. option:: TransformValues (default = true)
101127

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.
103129

104130
.. code-block:: c++
105131

106-
// Emits a hint for 'value' to become 'const int value = 42;'.
132+
// Before
107133
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+
108142
// Result is modified later in its life-time. No diagnostic and fixit hint will be emitted.
109143
int result = value * 3;
110144
result -= 10;
111145

112-
.. option:: TransformReferences (default = 1)
146+
.. option:: TransformReferences (default = true)
113147

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
115149
declaration.
116150

117151
.. code-block:: c++
@@ -120,31 +154,45 @@ Options
120154
// it, it can not be transformed (yet).
121155
int value = 42;
122156
// The reference 'ref_value' is not modified and can be made 'const int &ref_value = value;'
157+
// Before
123158
int &ref_value = value;
159+
// After
160+
int const &ref_value = value;
124161

125162
// Result is modified later in its life-time. No diagnostic and fixit hint will be emitted.
126163
int result = ref_value * 3;
127164
result -= 10;
128165

129-
.. option:: TransformPointersAsValues (default = 0)
166+
.. option:: TransformPointersAsValues (default = false)
130167

131168
Provides fixit-hints for pointers if their pointee is not changed. This does not analyze if the
132169
value-pointed-to is unchanged!
133170

134-
Requires 'WarnPointersAsValues' to be 1.
171+
Requires 'WarnPointersAsValues' to be 'true'.
135172

136173
.. code-block:: c++
137174

138175
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
141188
int *ptr_value = &value;
189+
// After
190+
int *const ptr_value = &value;
142191
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.
145194
*ptr_value = 0;
146195
147196
// The following pointer may not become a 'int *const'.
148197
int *changing_pointee = &value;
149198
changing_pointee = &result;
150-

0 commit comments

Comments
 (0)