[RFC] Throwing an exception with null pointers
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Wed Apr 16 23:13:27 UTC 2025
On 17/04/2025 10:54 AM, Derek Fawcus wrote:
> On Wednesday, 16 April 2025 at 22:43:24 UTC, Derek Fawcus wrote:
>> ```
>> $ clang-14 -Wall -Wnullable-to-nonnull-conversion -c ttt.c
>> ttt.c:37:8: warning: implicit conversion from nullable pointer 'int *
>> _Nullable' to non-nullable pointer type 'int * _Nonnull' [-Wnullable-
>> to-nonnull-conversion]
>> ptr = arg3;
>> ^
>> ttt.c:41:20: warning: implicit conversion from nullable pointer 'int *
>> _Nullable' to non-nullable pointer type 'int * _Nonnull' [-Wnullable-
>> to-nonnull-conversion]
>> a += *someNonNull(arg3);
>> ^
>> ttt.c:29:16: warning: variable 'ptr' set but not used [-Wunused-but-
>> set-variable]
>> int * NONNULL ptr;
>> ^
>> 3 warnings generated.
>> ```
>
> Having now got this to complain in the desired fashion, I'll now be
> applying it to some code at work. More help in quashing sources of bugs.
After a quick play, I would suggest also passing ``--analyze`` if you
are not already doing so.
It covers the case where you are not assuming non-null.
I.e. it can see the difference between:
```c++
void test(int *p) {
if (!p)
*p = 0; // warn
}
```
and
```c++
void test2(int *p) {
if (p)
*p = 0; // ok
}
```
This is definitely DFA.
More information about the Digitalmars-d
mailing list