Warn about do nothing expressions?
Benjamin Thaut
code at benjamin-thaut.de
Fri Mar 28 10:43:13 PDT 2014
Am 28.03.2014 18:03, schrieb monarch_dodra:
> 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.
Well when I mean warning, I really only mean the
var = var++
case without any indirections. Because I can't think of a case where you
actually want this.
Kind Regards
Benjamin Thaut
More information about the Digitalmars-d
mailing list