https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80528
Martin Sebor <msebor at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|UNCONFIRMED |NEW Last reconfirmed| |2017-08-18 CC| |msebor at gcc dot gnu.org Ever confirmed|0 |1 --- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> --- I think a warning like this might be useful, although I probably wouldn't want to enable under -Wall or -Wextra. (GCC could also use branch probabilities to decide whether or not to warn.) That said, I would expect GCC to get rid of the tests with -fdelete-null-pointer-checks. As it is, it eliminates the call to free when the pointer is known to be null but not the test itself (see below). That seems quite suboptimal to me since in the absence of any other information a pointer being passed to free is far more likely to be non-null than not. Clang and ICC both optimize the function below into an unconditional jump to free. So while I think implementing the optimization is the better solution here, the warning could also help improve the efficiency of the emitted code in its absence. $ cat b.c && gcc -O2 -S -Wall -Wextra -fdump-tree-optimized=/dev/stdout b.c void free (void*); void g (void *p) { if (p) // test retained free (p); else free (p); // eliminated } ;; Function g (g, funcdef_no=0, decl_uid=1817, cgraph_uid=0, symbol_order=0) Removing basic block 5 g (void * p) { <bb 2> [100.00%] [count: INV]: if (p_2(D) != 0B) goto <bb 3>; [53.47%] [count: INV] else goto <bb 4>; [46.53%] [count: INV] <bb 3> [53.47%] [count: INV]: free (p_2(D)); [tail call] <bb 4> [100.00%] [count: INV]: return; }