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

时间:2017-09-05 12:50:01

标签: c++ static-code-analysis false-positive pvs-studio

PVS Studio 6.17(Windows 7,64Bit,VS2017,C ++ - 03)似乎对以下缩减代码发出错误警告

#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;
}

PVS工作室的警告是

V821性能下降。 &#39; rhs&#39;变量可以在较低级别的范围内构建。

由于sstd::stack - 类型,并且相应的算法要求从堆栈中弹出rhs - 元素,因此看起来PVS-Studio是错误的。我错过了什么吗?

顺便说一下:

PVS Studio消息中有拼写错误:

   perfomance->performance

参考

1 个答案:

答案 0 :(得分:2)

在评论中讨论了代码优化的方法。是的,它可以被优化,但我认为它几乎没有意义。如果你必须使用C ++ - 03,那么,由于优化,代码将变得难以理解,这很糟糕。嗯,当然,使用std :: move。

是合适的

现在,谈谈PVS-Studio。分析仪不对,在这里发出警告。在rhs范围内创建变量if是不可能的。分析器没有考虑到数据源会发生变化,而s.top()会返回另一个值。那么,V821诊断是新的,并且存在缺点。我们会尝试消除这种误报。感谢您提供的示例,以及有关拼写错误的信息&#34; performance&#34;。