我有一个驱动 cppcheck 疯狂的代码片段,因为它没有看到日志调用中使用的变量。所以我得到未使用的变量和范围缩小警告:
double start = GetTimeOfDayDoubleSec(), afterDb = 0;
if (LoadFromDatabase(serials)) {
afterDb = GetTimeOfDayDoubleSec();
Cache.ResetDebugFlags(serials);
}
double end = GetTimeOfDayDoubleSec();
ZLOG_INFO("DB time %f, total %f", afterDb ? afterDb - start : 0, end - start);
Cppcheck 说:
The scope of the variable 'afterDb' can be reduced.
Variable 'afterDb' is assigned a value that is never used.
我无法找出抑制这两种方法的语法,而且手册也无济于事。单独的行、空格、逗号、冒号和分号都失败。单独的行给了我“不匹配的抑制”,其余的都是无效的:
//cppcheck-suppress variableScope
//cppcheck-suppress unreadVariable
//cppcheck-suppress variableScope unreadVariable
//cppcheck-suppress variableScope,unreadVariable
//cppcheck-suppress variableScope;unreadVariable
double afterDb = 0;
Failed to add suppression. Invalid id "variableScope:unreadVariable"
cppcheck 是否让我内联执行此操作,还是必须在命令行上使用 XML 执行此操作?
抱歉,事实证明存在一个令人困惑的问题:在 cppcheck-gui 中点击“刷新”并不能刷新所有内容,我必须重新加载 cppcheck 文件以获取要更新的抑制更改。即,工具栏上的“打开文件”图标而不是右侧的“刷新”图标。
事实证明,空间分隔有效:
//cppcheck-suppress variableScope unreadVariable