The forbiding for "return NULL;" in function declared returned int

Sometimes in C extern module, in some function that returned an int (0 means successful and -1 means failed), people may mistaken write return NULL; in error branch (for example: in tp_setattro slot). Usually the IDE won’t warn it in c++ because it will be change to return 0; and 0 is a valid value to return, and then compiler will pass, but then problem will occurred in runtime because python find the return value is 0 means all right but error occurred.

However, in musllinux python version 3.10, the compiling will failed. The message is usually:

In file included from /usr/include/c++/14.2.0/clocale:42,
                   from /usr/include/c++/14.2.0/x86_64-alpine-linux-musl/bits/c++locale.h:41,
                   from /usr/include/c++/14.2.0/bits/localefwd.h:40,
                   from /usr/include/c++/14.2.0/string:45,
                   from xxx.cpp:8:
 xxx.cpp: In function 'int XXX_delattr(PyObject*, PyObject*)':
  xxx.cpp:a:b: error: cannot convert 'std::nullptr_t' to 'int' in return
    a   |                 return NULL;
        |                        ^~~~
  xxx.cpp:c:d: error: cannot convert 'std::nullptr_t' to 'int' in return
    c   |                 return NULL;
        |                        ^~~~
  error: command '/usr/bin/g++' failed with exit code 1

This can note programmer to change the wrong return value. So can we use that way in all of the systems to ensure programmer can find the problem in advance?

This is how C works. Python has nothing to do with this. You can use other programming languages to write extensions. For example, nullptr_t is a thing in C++.

Interesting, gcc emits a warning or an error, depending on the choosen C standard version.

$ cat test.c
#include <stddef.h>

int f(void)
{
    return NULL;
}
$ gcc -std=c89 -c test.c
test.c: In function ‘f’:
test.c:5:12: warning: returning ‘void *’ from a function with return type ‘int’ makes integer from pointer without a cast [-Wint-conversion]
    5 |     return NULL;
      |            ^~~~
$ gcc -std=c99 -c test.c
test.c: In function ‘f’:
test.c:5:12: error: returning ‘void *’ from a function with return type ‘int’ makes integer from pointer without a cast [-Wint-conversion]
    5 |     return NULL;
      |            ^~~~

This is why such error never survived compilation.