文章目录
C/C++常见warning
case 1:
warning: unused parameter [-Wunused-parameter]
warning: unused variable [-Wunused-variable]
参数(或变量)没有被使用
解决方法:
- 去掉编译flag,当然这个不推荐,掩耳盗铃
-Wall
和-Wextra
会打开所有编译选项,-Wno-unused-parameter
会全局禁用这个warning
- 可以在参数前加上
__attribute__((unused))
解决
int func( __attribute__((unused)) int parm )
- 在参数后加上
[[gnu::unused]]
属性说明符
int func( int parm [[gnu::unused]] )
- c++17可以使用
[[maybe unused]]
int func( [[maybe unused]] int parm )
- 如果是Qt的话,可以使用
Q_UNUSED
宏
#define Q_UNUSED(x) (void)x
(void)parameter
表达式
#define UNUSED(parm) (void)(parm)
int func( int parm )
{
UNUSED(parm); //或者直接 (void) parm;
...
}
- 使用未命名参数
int func( int )
std::ignore = parameter
int func( int parm )
{
std::ignore = parm;
...
}
一个漂亮的小宏
#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#else
# define UNUSED(x) x
#endif
case 2:
warning: multi-line comment [-Wcomment]
warning: “/*” within comment [-Wcomment]
当\
出现在以// ...
注释结尾处和/*
出现在/* ... */
注释中时,不当的注释符号有可能会吞掉有用的代码
解决方法:正确注释即可
/** this is some comments
/* and warning report 2
*/
// this is comment warning report \
and continue
int mian()
{
printf("this is main function");
return 0;
}
case 3:
warning: cast increases required alignment of target type [-Wcast-align]
强制转换增加了目标类型的对齐要求
uint8_t *a;
uint16_t *b;
a = b; //warning
case 4:
warning: large integer implicitly truncated to unsigned type [-Woverflow]
整数溢出,大的整数会被截断成unsigned类型,当整数达到可表示的最大值后会从起点重新开始
unsigned char a;
int b;
a = b; //warning
case 5:
warning: is used uninitialized in this function [-Wuninitialized]
warning: may be used uninitialized in this function [-Wmaybe-uninitialized]
没有初始化就使用
int var1;
process(var1); //warning
case 6:
warning: implicit declaration of function [-Wimplicit-function-declaration]
函数使用了隐式声明,提供对应声明即可
/** main.c */
main_func()
{
int var1 = 0;
process(var1); //warning "process"" implicit declare
}
case 7:
warning: comparison of promoted ~unsigned with unsigned [-Wsign-compare]
提升~unsigned
和unsigned
的比较,将~unsigned
强制转换为unsigned
,根据整数促销规则,~unsigned
会被提升为~int
解决方法:将~unsigned
强制转换为unsigned
unsigned var;
if ( var > ~var ) //warning
{
}
case 8:
warning: comparison is always true due to limited range of data type [-Wtype-limits]
warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]
由于数据类型的范围有限,比较总是正确的
解决方法:改为正确逻辑
uint8_t var;
std::cin >> var
if( var >= 0 ) //warning
{
process();
}
case 9:
warning: variable set but not used [-Wunused-but-set-variable]
变量定义了却没有使用
解决方法:删除无用变量或者参考case 1 加上unused限定符
int func()
{
int novar; //warning
process();
return 0;
}
case 10:
warning: control reaches end of non-void function [-Wreturn-type]
控制达到了非void函数的结尾,函数本该有返回值却没有返回
解决方法:给函数尾部加上return返回值
int func(int var)
{
process(var);
} //warning
case 11:
warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
建议在||
中的&&
周围加括号,优先级问题,最好加上括号
case 12:
warning: function defined but not used [-Wunused-function]
函数定义了但是没有使用
case 13:
warning: passing argument 2 of ‘function 2’ from incomaptible pointer type
warning: passing argument 1 of ‘function 1’ discards ‘const volatile’ qualifier form pointer target type
传参类型和声明类型不一致,会导致功能异常,传递给某个function的参数丢弃了const volatile
的限定符
解决方法:
1、使函数声明、定义和调用处一致即可;
2、可在不会导致其他问题的情况下移除const限定符。
case 14:
warning: “something” redefined
某个函数或变量重定义了
case 15:
warning: missing initializer for field 'field 1 ’ of ’ struct 1 ’ [-Wmissing-field-initializers]
缺少字段初始化,将会进行隐式默认初始化
case 16:
warning:braces around scalar initializer
在标量初始化器周围加上大括号
case 17:
warning: (near initialization for ‘array 1’) [-Wmissing-braces]
warning: missing braces around initializer [-Wmissing-braces]
初始化元素的数量超过了数组大小,在初始化器周围缺少大括号
case 18:
warning: excess elements in scalar initializer
在标量初始化器中有多余元素,即没有定义就初始化
解决方法:正确定义标量
char *foo = {"aaa", "bbb", "ccc"}; //warning
case 19:
warning: ignoring #pragma unused [-Wunknown-pragmas]
未知的#pragmas
语句
解决方法:编写正确的#pragmas语句
#pragmas unused(size) //warning
case 20:
warning: this decimal constant is unsigned only in ISO C90
此十进制常数仅在ISO C90中是无符号的
解决方法:
1、加上对应的U标识或L标识;2、使用c99编译器;3、使用十六进制数字;
unsigned int a = 4286545791; //warning
case 21:
warning: conflicting types for “function-1”
函数的定义冲突了,在C99之前的C版本中,如果函数在使用前没有定义,编译器会假定函数返回int
解决方法:在函数使用前给出函数申明或者定义,此隐式int规则已从C99开始删除
use()
{
func(); //warning
}
void func()
{
printf("this is void function");
};
更多的warning可以参考stackoverflow官网:https://stackoverflow.com