Search code examples
cppcheck

CppCheck ignores the usage of variables in macro definitions, how to change that?


The output of running CppCheck on my code displays the following error: Variable 'strFullPath' is assigned a value that is never used. [unreadVariable]

The method below is the one in discussion.

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 states that strFullPath's value is never used. However, it is used inside a macro.

How to setup CppCheck so it becomes capable of finding the usage of the variable?


Solution

  • Move it inside the macro.

    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
    }