c++ - 误报警告 PVS Studio : V808 object of type was created but was not utilized

标签 c++ qt pvs-studio

PVS Studio 6.17(Windows 7、64 位、VS2015)似乎对以下代码发出错误警告。 警告“'unordered_map' 类型的 V808 'statuses' 对象已创建但未使用”。 使用几个 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,它会显示 2 个或更多参数。

引用:

这个问题是已知的吗?

最佳答案

请不要提出此类问题。 Stackoverflow 用户多次对类似问题发表评论。

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

关于c++ - 误报警告 PVS Studio : V808 object of type was created but was not utilized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46345999/

相关文章:

qt - QPushButton 或 QToolButton 的颜色

c++ - PVS-Studio 知道 Unicode 字符吗?

android - 让 c++ 项目在 android 中工作的完整过程是什么

c++ - 非常大数的除法

macos - Qt 和 OpenGL OS X : GLSL shader version only 120 on Mountain Lion

Qt3D 不同 RenderPasses 的不同 Material

c++ - 误报 V595 '_parent' 指针在根据 nullptr 进行验证之前被使用

C++:x64 不推荐使用 A2W 宏 - 替代方案?

c++ - 这是内存泄漏吗?试图重置类型类的类成员。 C++

c++ - C/C++ 的多线程内存分配器