V738. Temporary anonymous object is used.


The analyzer detected that a temporary anonymous object is used which is created as a result of executing the postfix ++ or -- operator. It does make sense sometimes, but it is certainly an error when such temporary object is changed or its address is retrieved.

Consider the following example:

vector<float>::iterator it = foo();
it++ = x;

In this code, a temporary copy of an iterator is created. Then the iterator is incremented. After that, the assignment operator is applied to the temporary object. This code doesn't make sense; the author obviously wanted it to do something else. For example, they may have intended to execute the assignment operation first and only then the increment operation.

In that case, the fixed version of the code should look like this:

it = x;
it++;

However, postfix operations are not efficient with iterators, and a better version would be the following:

it = x;
++it;

An alternative version:

it = x + 1;

Here's another example:

const vector<int>::iterator *itp = &it++;

The 'itp' pointer can't be used as it points to a temporary unnamed object already destroyed. The correct version:

++it;
const vector<int>::iterator *itp = ⁢

According to Common Weakness Enumeration, potential errors found by using this diagnostic are classified as CWE-672.


Bugs Found

Checked Projects
343
Collected Errors
12 850