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

False positive warning PVS Studio: V808 object of type was created but was not utilized

本文关键字:对象 创建 未使用 类型 V808 警告 PVS Studio      更新时间:2023-10-16

pvs Studio 6.17(Windows 7,64bit,vs2015)似乎在下面的代码上发出了错误的警告。创建了" unordered_map"类型的" V808'状态"对象的警告。具有unordered_map初始化的原始代码,并使用几个QStringLiteral键值进行初始化。仅使用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,它带有2个或更多的agruments。

参考:

  • https://www.viva64.com/en/w/v808/

是知道的问题吗?

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

  1. 实际上没有什么可回答的。这只是对工作不足的描述,而不是一个问题。错误报告和功能请求在堆栈溢出上不在主题上。我想请您在类似情况下写我们的支持。
  2. 请检查该分析仪会产生警告的书面合成示例。我无法通过检查已发布的代码来复制误报。我猜该代码包含一些使分析仪感到困惑的东西。或分析仪可能是正确的,例如,如果使用情况在不活动的构造#if ...#endif。