误报警告 PVS Studio:V821 性能下降。 'rhs' 变量可以在较低级别的范围内构造
False positive warning PVS Studio: V821 Decreased perfomance. The 'rhs' variable can be constructed in a lower level scope
PVS Studio 6.17(Windows 7、64 位、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 性能下降。 'rhs' 变量可以在较低级别的范围内构造。
由于s
是std::stack
类型,对应的算法要求rhs
元素出栈,看来PVS-Studio是错误的。我错过了什么吗?
顺便说一句:
PVS Studio 消息中存在拼写错误:
perfomance->performance
参考
在评论中讨论了代码优化的方法。是的,它可以优化,但我认为它实际上没有意义。如果你非要用C++-03,那么,因为优化,代码会变得复杂,难以理解,这是不好的。嗯,当然,使用 std::move.
是合适的
现在说PVS-Studio.The分析器不对,在此发出警告。不可能只获取并重新定位在 if
范围内创建的变量 rhs
。分析器没有考虑到数据源会改变并且 s.top()
会 return 另一个值。嗯,V821诊断是新的,也有缺点。我们将尝试消除此类误报。感谢您提供的示例,以及有关拼写错误 "performance".
的信息
PVS Studio 6.17(Windows 7、64 位、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 性能下降。 'rhs' 变量可以在较低级别的范围内构造。
由于s
是std::stack
类型,对应的算法要求rhs
元素出栈,看来PVS-Studio是错误的。我错过了什么吗?
顺便说一句:
PVS Studio 消息中存在拼写错误:
perfomance->performance
参考
在评论中讨论了代码优化的方法。是的,它可以优化,但我认为它实际上没有意义。如果你非要用C++-03,那么,因为优化,代码会变得复杂,难以理解,这是不好的。嗯,当然,使用 std::move.
是合适的现在说PVS-Studio.The分析器不对,在此发出警告。不可能只获取并重新定位在 if
范围内创建的变量 rhs
。分析器没有考虑到数据源会改变并且 s.top()
会 return 另一个值。嗯,V821诊断是新的,也有缺点。我们将尝试消除此类误报。感谢您提供的示例,以及有关拼写错误 "performance".