cpplint使用

发布时间 2023-07-10 21:05:55作者: weixicai

cpplint可用于检查代码是否遵守google c++代码规范。我的理解是检查你使用的是不是正确的C++。

安装:

python3 -m pip install cpplint

 

与之对应,代码需要格式化为满足google c++规范的格式,比如使用

clang-format --style=google -i $file

 会添加合适的空格和换行。

 

满足的过程是:1, 编写/修改代码;2, 代码简单格式化;3,  cpplint检查代码;4, 编译。不停重复1-3, 直到不报错,然后进行编译。

 

我是从c开始学习的,所以有很多c的痕迹。。下面记录一些报错信息,

1, Do not use namespace using-directives.  Use using-declarations instead. 使用using std::cout替代using namespace std; 用哪个引入哪个。

2, No copyright message found.  You should have a line: "Copyright [year] <Copyright Owner>", 加入版权信息

3, Use int16/int64/etc, rather than the C type short  [runtime/int] ,使用int16_t, int64_t等

4, Using C-style cast.  Use static_cast<double>(...) instead , 类型转换,替换(double)(...)

5, Single-parameter constructors should be marked explicit. 只有一个参数的构造函数存在隐式类型转换的可能。通过在构造函数前加explicit只能直接初始化, 而抑制隐式调用。

6, cpplint.py:57: DeprecationWarning: module 'sre_compile' is deprecated, 会不会影响检测正确性?暂时不会影响,这里 。

随时更新。。