0

I must first admit that I have very little experience with C++, so please forgive me if the question looks somewhat stupid.

I've found a puzzling element while analyzing project with PVS-Studio. Here's the code to reproduce the question:

class Test
{
public:
    const Test &Test::operator=(const Test &test);
};

const Test &Test::operator=(const Test &test) {
    if (this == &test)
        return test;

    return *this;
}

Of course, this assignment operator is unnecessary here, but the question is in the warning PVS-Studio generated on it:

V790 It is odd that the assignment operator takes an object by a non-constant reference and returns this object.

(emphasis mine)

I'm not sure what to think. Where is this non-constant reference? Argument of the assignment operator is const Test &, i.e. it seems to be the constant reference. Furthermore, return test is executed only if the assignment is a no-op anyway, i.e. returning *this and test should make no difference.

Is this a false positive, or I simply don't understand something?

Cerberus
  • 8,879
  • 1
  • 25
  • 40
  • You didn't understand something. – πάντα ῥεῖ Apr 19 '19 at 18:33
  • Technically - `this` is non-const inside the method (it would be `const` if the method was marked as `const`). – Algirdas Preidžius Apr 19 '19 at 18:34
  • 1
    Why would you return `test`? This clearly looks suspicious, even though it is the same object as `this`. – user7860670 Apr 19 '19 at 18:35
  • @πάνταῥεῖ I'm not sure how this helps... It's not a question on how to do this right (this would be a separate question, and it seems to be somewhat more clear), I'm trying to understand the concrete error and the reference mentioned. – Cerberus Apr 20 '19 at 03:08
  • 1
    In this case, the analyzer issued inaccurate report that has led to misunderstanding. We've fixed it and in the next versions the report for this code will be the following: “It is odd that the assignment operator takes an object by a constant reference and returns this object.” – AndreyKarpov Jul 18 '19 at 14:17

0 Answers0