误报警告PVS Studio:已创建V808类型对象但未使用

时间:2017-09-21 14:00:41

标签: c++ qt pvs-studio

PVS Studio 6.17(Windows 7,64Bit,VS2015)似乎对下面的代码发出错误警告。 警告“'unordered_map'类型的V808'状态'对象已创建但未被使用”。 使用几个QStringLiteral键值初始化unordered_map的原始代码。仅使用STL的简化版本如下所示:

#include <string>
#include <unordered_map>
#include <iostream>

// Simplified analogue of QStringLiteral
#define StringLiteral(str) ([]() { return std::string{str}; }())

int main()
{
    const std::unordered_map<std::string, int> statuses{
        { StringLiteral("aaa"), 1 },
        { StringLiteral("bbb"), 2 },
        { StringLiteral("ccc"), 3 }
    };

    auto iter = statuses.find("aaa");
    if (iter != statuses.cend())
        std::cout << iter->first << " has status: " << iter->second << std::endl;
    return 0;
}

奇怪的是,当使用lambda中返回值的通用初始化时产生的V808。如果使用构造函数语法,则不会显示警告。

产生V808的另一个错误案例是:

const std::unordered_map<int, std::function<void(int, int)>> functions{
    { 0, [](int a, int b) {} },
    { 1, [](int a, int b) {} },
};

const auto it = functions.find(0);
if (it != functions.cend() && it->second)
    it->second(1, 2);

这里如果创建一个带有一个参数lambdas的地图 - 没有V808,它有两个或更多的agruments。

参考:

这个问题是否已知?

1 个答案:

答案 0 :(得分:0)

请不要创建此类问题。 Stackoverflow用户反复评论过类似的问题。

  1. 实际上没有什么可回答的。它只是对欠工作的描述,而不是一个问题。 Stack Overflow上的错误报告和功能请求为not on-topic。我想请你在类似的情况下写我们的support
  2. 请检查,对于书面合成示例,分析仪会生成警告。我无法通过检查已发布的代码来重现误报。我猜这段代码包含的东西让分析器感到困惑。或者分析器可能是正确的,例如,如果用法在非活动构造中#if ...#endif。