NRefactory icon indicating copy to clipboard operation
NRefactory copied to clipboard

Split 'if' works wrong for OR condition

Open amirhalatzi opened this issue 10 years ago • 2 comments

Split 'if' for (cond1 || cond2) condition should be if(cond1){} if(cond2{}

Currently: if(cond1){} else if(cond2){}

amirhalatzi avatar Mar 02 '15 08:03 amirhalatzi

But what if both conditions are true? Then both if blocks would be executed, which is not equivalent to original "if (cond1 || cond2)". The "if" + "else if" is correct in my opinion. Do you have any example showing that "else if" is wrong?

Rpinski avatar Mar 02 '15 23:03 Rpinski

In terms of block execution @amirhalatzi is right. But in the case of cond1 || cond2 cond2 should only be evaluated if cond1 returned false.

Consider the following example:

Result r;
if (LoadFromSource(a, out r) || LoadFromSource(b, out r))
    Process(r);

Would be turned into:

Result r;
if (LoadFromSource(a, out r))
    Process(r);
if (LoadFromSource(b, out r)) // what if r was already loaded from the first source?
    Process(r);

Which could lead to undesired side effects...

siegfriedpammer avatar Mar 03 '15 05:03 siegfriedpammer