xref: /llvm-project/clang-tools-extra/docs/clang-tidy/checks/readability/non-const-parameter.rst (revision 6e566bc5523f743bc34a7e26f050f1f2b4d699a8)
1.. title:: clang-tidy - readability-non-const-parameter
2
3readability-non-const-parameter
4===============================
5
6The check finds function parameters of a pointer type that could be changed to
7point to a constant type instead.
8
9When ``const`` is used properly, many mistakes can be avoided. Advantages when
10using ``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 will
19make 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  }
52