393 S Single parameter constructor not ‘explicit’.
原因:只有一个参数的构造函数存在隐式类型转换的可能。
解决方法:
在单参数构造函数之前添加“explicit”。
例子:
class test{
public:
explicit test(const std::string&name =""):
m_name(name),height(0),weight(0){}
int CompareTo(const test& other);
std::string m_name;
int height;
int weight;
};
560 S Scope of variable could be reduced. : ret
原因:参考https://blog.csdn.net/java_1111/article/details/8689040;讲的很清楚。
解决方法:
减小变量作用域,在使用的地方定义,如下图所示
113 S Non standard character in source.
原因:源文件中出现非法字符。使用如下:
#undef LOG_TAG
#define LOG_TAG "test@DeathListener"
解决方法:将非法字符@去掉。
127 S Array has no bounds specified.
原因:数组未指定边界
解决方法:不修改,
出现问题的地方:main函数传参声明,函数体中未使用,C++代码风格,不影响代码安全。
int main(int /*argc*/, char* /*argv*/[]) {}
331 S Literal value requires a U suffix.
原因:文字值需要U后缀。
解决方法:文字值添加U后缀。
const size_t threadPoolCapacity = 4;
修改后
const size_t threadPoolCapacity = 4U;
90 S Basic type declaration used.
原因:使用了int、char、float、double等基础类型,这是不允许的
解决方法:
添加类似于下面这种typedef,然后在代码中使用int32_t来表示int。
typedef signed char int8_t;
typedef short int16_t;
typedef int int32_t;
typedef long long int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
514 S Using directive or declaration in header.
原因:头文件中使用了命名空间。
如下图所示
是为了防止出现以下情况:
两个头文件都定义了namespace NS1;
同事引用这两个头文件时,不同的顺序会出现不一样的结果。
解决方法:使用类似于
#ifndef 大写的当前命名空间名称_头文件名称大写_H_
#define 大写的当前命名空间名称_头文件名称大写_H_
#endif
243 S Included file not protected with #define.
原因:头文件中没有使用#ifndef…#define…#endif保护
解决方法:
使用类似于
#ifndef 大写的当前命名空间名称_头文件名称大写_H_
#define 大写的当前命名空间名称_头文件名称大写_H_
#endif
69 S #pragma used.
原因:头文件使用的是#pragma once保护,没有使用#ifndef…#define…#endif保护
解决方法:将#pragma once保护,改为#ifndef…#define…#endif保护。
119 S Nested comment found.
原因:注释部分嵌套了注释,例如 //… /…/
/…//…/
解决方法:
将嵌套的注释去掉即可。
例如:/* This is the Outer Comment
/* This is the Inner Comment - not compliant
/
改为
/ This is the Outer Comment
This is the Inner Comment - not compliant */
139 S Construct leads to infeasible code.
原因:代码不会被执行
解决方法:维持现状,不修改
9 S Assignment operation in expression.
原因:表达式中赋值操作
创建临时对象,接收方法的返回值,表达式中不写那么复杂就行,后面补充例子。
219 S User name starts with underscore.
原因:变量名以下划线开头。
解决方法:变量名不以下划线开头。
其他
前面的都是在项目中实际修改过的,下面分享以下写的还不错的,有需要可以参考。
1 S - 20 S
41 S - 60 S
61 S - 80 S
81 S - 100 S
101 S - 120 S
121 S -140 S
201 S - 220S
241 S - 280 S
这个不知道咋命名了
LDRA Standards for C/C++