代码之家  ›  专栏  ›  技术社区  ›  orbitcowboy

误报警告PVS Studio:V821性能下降。“rhs”变量可以在较低级别范围内构造

  •  0
  • orbitcowboy  · 技术社区  · 7 年前

    #include <stack>
    #include <string>
    #include <vector>
    bool fred(const std::string &x)
    {
        return x == "ab";
    }
    std::vector<std::string> bar(std::stack<std::string> & s)
    {
        std::vector<std::string> v;
        const std::string rhs(s.top()); // V821 Decreased perfomance. The 'rhs' variable can be constructed in a lower level scope.
        s.pop();
        const std::string lhs(s.top());
        s.pop();
    
        if (fred(lhs))
        {
            v.push_back(rhs);
        }
        return v;
    }
    

    V821性能下降。“rhs”变量可以在较低级别范围内构造。

    自从 s 是一个 std::stack rhs -元素从堆栈中弹出,看起来PVS Studio是错误的。我错过什么了吗?

    PVS Studio消息中有一个输入错误:

       perfomance->performance
    

    参考

    1 回复  |  直到 7 年前
        1
  •  2
  •   AndreyKarpov    7 年前

    现在,谈谈PVS Studio。分析器不正确,在此发出警告。不可能只获取并重新定位变量 rhs 在内部创建 if -范围。分析仪没有考虑到数据源会发生变化 s.top()