2016-08-23 18:09:08 +08:00
|
|
|
.. title:: clang-tidy - readability-non-const-parameter
|
|
|
|
|
|
|
|
readability-non-const-parameter
|
|
|
|
===============================
|
|
|
|
|
|
|
|
The check finds function parameters of a pointer type that could be changed to
|
|
|
|
point to a constant type instead.
|
|
|
|
|
2016-08-24 01:57:24 +08:00
|
|
|
When ``const`` is used properly, many mistakes can be avoided. Advantages when
|
|
|
|
using ``const`` properly:
|
|
|
|
|
|
|
|
- prevent unintentional modification of data;
|
|
|
|
|
|
|
|
- get additional warnings such as using uninitialized data;
|
|
|
|
|
|
|
|
- make it easier for developers to see possible side effects.
|
2016-08-23 18:09:08 +08:00
|
|
|
|
|
|
|
This check is not strict about constness, it only warns when the constness will
|
|
|
|
make the function interface safer.
|
|
|
|
|
2016-08-24 01:57:24 +08:00
|
|
|
.. code-block:: c++
|
2016-08-23 18:09:08 +08:00
|
|
|
|
|
|
|
// warning here; the declaration "const char *p" would make the function
|
|
|
|
// interface safer.
|
|
|
|
char f1(char *p) {
|
|
|
|
return *p;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no warning; the declaration could be more const "const int * const p" but
|
|
|
|
// that does not make the function interface safer.
|
|
|
|
int f2(const int *p) {
|
|
|
|
return *p;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no warning; making x const does not make the function interface safer
|
|
|
|
int f3(int x) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no warning; Technically, *p can be const ("const struct S *p"). But making
|
|
|
|
// *p const could be misleading. People might think that it's safe to pass
|
|
|
|
// const data to this function.
|
|
|
|
struct S { int *a; int *b; };
|
|
|
|
int f3(struct S *p) {
|
|
|
|
*(p->a) = 0;
|
|
|
|
}
|