Should we warn if we detect null derefernces or void value uses ?

Andrea Fontana via Digitalmars-d digitalmars-d at puremagic.com
Wed Dec 7 02:58:28 PST 2016


On Wednesday, 7 December 2016 at 10:25:56 UTC, Walter Bright 
wrote:
> On 12/4/2016 8:41 PM, Stefan Koch wrote:
>> What is your opinion, should we warn if we unambiguously 
>> detect something that
>> is clearly unwanted ?
>>
>> int fn(int y)
>> {
>>   int x = void;
>>   ++x;
>>   return x+y;
>> }
>
> It's an old idea, and comes up regularly.
>
> This all sounds like a great idea, and I've tried it. 
> Unfortunately, it runs afoul of code like this:
>
>   int i;
>   int* p = null;
>   if (c)
>     p = &i;
>   ...code...
>   if (c)
>     *p = 3; // Warning Will Robinson! p could be null!

Yes here it *could* be null but here it is for sure:

int* p = null;
...
// p never used here inside or outside code blocks
...
*p = 3;

So I think we can safely halt compilation with an error.

(The same goes on void example above)




More information about the Digitalmars-d mailing list