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 性能下降。'rhs' 变量可以在较低级别的范围内构造。
由于s
是std::stack
-type,而对应的算法要求rhs
-element从栈中弹出,看来PVS-Studio是错误的。我错过了什么?
顺便一提:
PVS Studio 消息中有错字:
perfomance->performance
参考