readability-non-const-parameter¶
该检查会查找指针类型的函数参数,这些参数可以改为指向常量类型。
正确使用 const
可以避免很多错误。正确使用 const
的优势
防止意外修改数据;
获得更多警告,例如使用未初始化数据;
使开发人员更容易发现可能的副作用。
此检查对常量性并不严格,只有当常量性能使函数接口更安全时才会发出警告。
// 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;
}
// no warning; p is referenced by an lvalue.
void f4(int *p) {
int &x = *p;
}