PVS Studio 6.17 (Windows 7, 64Bit, VS2017, C++-03) seems to give a wrong warning on following reduced code

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

The warning from PVS studio is

V821 Decreased perfomance. The 'rhs' variable can be constructed in a lower level scope.

Since s is a std::stack-type, and the corresponding algorithm requires that the rhs-element is popped from the stack, it looks like PVS-Studio is wrong. Did i miss something?

By the way:

There is a typo in PVS Studio message:

   perfomance->performance

Reference

1

There are 1 answers

0
AndreyKarpov On BEST ANSWER

In comments ways of optimization of code are discussed. Yes, it can be optimized, though, I think there is practically no point in it. If you have to use C++-03, then, because of optimization, code will become complicated for understanding, which is bad. Well, sure, it would be appropriate to use std::move.

Now, speaking about PVS-Studio.The analyzer is not right, issuing a warning here. It is not possible to just take and relocate variable rhs creating inside the if-scope. The analyzer didn't tale it into account that the data source would change and s.top() would return the other value. Well, V821 diagnostic is new and there are shortcomings. We'll try to remove such a kind of false positives. Thank you for the given example, and also for the information about the typo the word "performance".