Warn about do nothing expressions?

monarch_dodra monarchdodra at gmail.com
Fri Mar 28 10:03:03 PDT 2014


On Friday, 28 March 2014 at 16:54:49 UTC, Benjamin Thaut wrote:
> I had a bug which came down to the following line today:
>
> m_takeIndex = m_takeIndex++;
>
> Actually this line does nothing. m_takeIndex remains at exactly 
> the same value as before (tested with dmd 2.065).
>
> Can someone please explain why?

IT does nothing because that's post-increment: EG "save the 
current value, increment, return the old value".

Then you assign back the old value, effectively putting you back 
where you started:

int i = 5;

i = i++; //i is 5
then
i = 5; //i was incremented to 6
then
i is 5 again;

> And would be suitable to warn about this, if it is correct 
> behaviour?

The problem here though is that we are talking about a "logical" 
no side effect: It *does* something, you just can't observe it.

Imagine this:

//----
void fun(ref i, ref j)
{
     i = j++;
}
void main()
{
     int a = 5;
     fun(a, a); //do nothing?
}
//----

This is to contrast with "i + j;" which is an *actual* no op.


More information about the Digitalmars-d mailing list