V6028. Identical expressions to the left and to the right of compound assignment.


The analyzer detected identical subexpressions to the left and to the right of a compound assignment operator. This operation may be incorrect or meaningless, or can be simplified.

Consider the following example:

x += x + 5;

Perhaps the programmer simply wanted to add the value 5 to the 'x' variable. In that case, the fixed code would look like this:

x = x + 5;

Or perhaps they wanted to add the value 5 but wrote an extra 'x' variable by mistake. Then the code should look like this:

x += 5;

However, it is also possible that the code is written correctly, but it looks too complicated and should be simplified:

x = x * 2 + 5;

Now consider the following example:

x += x;

This operation is equivalent to multiplying the value of a variable by two. This is what a clearer version would look like:

x *= 2;

Here is one more expression:

y += top - y;

We are trying to add the difference of the variables 'top' and 'y' to the 'y' variable. Resolving this expression produces the following result:

y = y + top – y;

It can be simplified, as the 'y' variable is subtracted from itself, which does not make sense:

y = top;

Bugs Found

Checked Projects
343
Collected Errors
12 850