CppCheck 忽略宏定义中变量的使用,如何更改?

问题描述

在我的代码上运行 CppCheck 的输出显示以下错误: Variable 'strFullPath' is assigned a value that is never used. [unreadVariable]

下面的方法是讨论中的方法。

void DebugLogging::GetDebugLogSettings(const std::string& sAppProfile)
{
    std::string strFullPath = ROOT_KEY_NAME + sAppProfile;
    
#ifdef _WIN32
    std::string strFullPathLocal = strFullPath + "Local";
#else
    std::string sAppProfileLocal = sAppProfile + "Local";
    std::ifstream settingsLocalfile(sAppProfileLocal.c_str());
#endif
}

CppCheck 声明从未使用 strFullPath 的值。但是,它是在宏内部使用的。

如何设置 CppCheck 使其能够找到变量的用法?

解决方法

在宏中移动它。

void DebugLogging::GetDebugLogSettings(const std::string& sAppProfile)
{
#ifdef _WIN32
    std::string strFullPath = ROOT_KEY_NAME + sAppProfile;
    std::string strFullPathLocal = strFullPath + "Local";
#else
    std::string sAppProfileLocal = sAppProfile + "Local";
    std::ifstream settingsLocalfile(sAppProfileLocal.c_str());
#endif
}
,

我是一名 Cppcheck 开发人员。 Damian-Teodor Beles 建议的修复方法很好。

我只是想添加一些哲学。在我看来,这个特别的警告是不幸的。我认为这是误报,作为人类,我可以看到该值已被使用。

这个误报可以在 Cppcheck 中修复。我们已经修复了一些相关的误报,但显然不是这个。将来可能会在 Cppcheck 中修复它,但我不能保证什么时候会发生。

总的来说,我认为移动变量是一个不错的解决方案。但也许有时您不想移动变量。然后你现在可以内联抑制警告。当我们在 Cppcheck 中修复此问题时,您将收到一条警告,指出从未使用内联抑制...然后您可以删除注释。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...