52 lines · plain
1.. title:: clang-tidy - readability-non-const-parameter2 3readability-non-const-parameter4===============================5 6The check finds function parameters of a pointer type that could be changed to7point to a constant type instead.8 9When ``const`` is used properly, many mistakes can be avoided. Advantages when10using ``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 18This check is not strict about constness, it only warns when the constness will19make the function interface safer.20 21.. code-block:: c++22 23 // warning here; the declaration "const char *p" would make the function24 // 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" but30 // 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 safer36 int f3(int x) {37 return x;38 }39 40 // no warning; Technically, *p can be const ("const struct S *p"). But making41 // *p const could be misleading. People might think that it's safe to pass42 // 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 }52