if(arr) now a warning

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Wed Apr 29 12:15:45 PDT 2015


On Wednesday, 29 April 2015 at 17:35:58 UTC, Andrei Alexandrescu 
wrote:
> I have no doubt the change can find certain errors. Problem is 
> false positives. FWIW these are the changes I had to operate on 
> std.allocator to make it work with the new compiler. One per 
> 194 lines on average, all false positives:

Yeah, but I think that it's safe to say that std.allocator is not 
the normal case, since it's operating so heavily on arrays (and 
non-GC allocated no less), and it's caring about their pointers 
rather than what they point to, whereas most D array code doesn't 
care about the ptr value of the array at all. In general, at 
most, it'll care about whether the array is empty or not. So, I 
would expect that for most programs, they won't see this warning 
much - be it a false positive or not - and if they do see it, the 
odds are pretty high that it's not a false positive. Yes, there 
will be code out there which uses if(arr) correctly, and not 
being able to do if(auto ptr = arr) will be annoying for some 
folks, but given how null and empty are so frequently conflated 
with arrays in D, I very much doubt that much code is going to be 
using if(arr) and want it to be equivalent to if(arr !is null).

Granted, we can't know for sure how common if(arr) is used in the 
wild - be it correctly or incorrectly - but std.allocator is 
definitely not your average code, and the consensus seems to be 
that if(arr) is error-prone even if it's occasionally useful.

- Jonathan M Davis


More information about the Digitalmars-d mailing list