| 1 |
.. title:: clang-tidy - readability-non-const-parameter |
| 2 |
|
| 3 |
readability-non-const-parameter |
| 4 |
=============================== |
| 5 |
|
| 6 |
The check finds function parameters of a pointer type that could be changed to |
| 7 |
point to a constant type instead. |
| 8 |
|
| 9 |
When ``const`` is used properly, many mistakes can be avoided. Advantages when |
| 10 |
using ``const`` properly: |
| 11 |
|
| 12 |
- prevent unintentional modification of data; |
| 13 |
|
| 14 |
- get additional warnings such as using uninitialized data; |
| 15 |
|
| 16 |
- make it easier for developers to see possible side effects. |
| 17 |
|
| 18 |
This check is not strict about constness, it only warns when the constness will |
| 19 |
make the function interface safer. |
| 20 |
|
| 21 |
.. code-block:: c++ |
| 22 |
|
| 23 |
// warning here; the declaration "const char *p" would make the function |
| 24 |
// interface safer. |
| 25 |
char f1(char *p) { |
| 26 |
return *p; |
| 27 |
} |
| 28 |
|
| 29 |
// no warning; the declaration could be more const "const int * const p" but |
| 30 |
// that does not make the function interface safer. |
| 31 |
int f2(const int *p) { |
| 32 |
return *p; |
| 33 |
} |
| 34 |
|
| 35 |
// no warning; making x const does not make the function interface safer |
| 36 |
int f3(int x) { |
| 37 |
return x; |
| 38 |
} |
| 39 |
|
| 40 |
// no warning; Technically, *p can be const ("const struct S *p"). But making |
| 41 |
// *p const could be misleading. People might think that it's safe to pass |
| 42 |
// const data to this function. |
| 43 |
struct S { int *a; int *b; }; |
| 44 |
int f3(struct S *p) { |
| 45 |
*(p->a) = 0; |
| 46 |
} |
| 47 |
|
| 48 |
// no warning; p is referenced by an lvalue. |
| 49 |
void f4(int *p) { |
| 50 |
int &x = *p; |
| 51 |
} |