On Tue, Jan 27, 2009 at 05:42:04AM -0800, Daniel Jacobowitz wrote: > On Tue, Jan 27, 2009 at 06:52:18PM +1100, zol...@bendor.com.au wrote: > > // Debug( tst->value ); > > > > if ( ! tst ) { > > ptr->next = (void *) 0; > > break; > > } > > This optimization comes up on the list frequently. Do folks think a > corresponding warning would be a win? "warning: ignored NULL check > because pointer has already been dereferenced"?
The warning's a good idea, but there's an important caveat that might result in extra noise if it isn't handled correctly. Static checkers (Coverity, for example), warn about this kind of code, because it often indicates a bug (either the test should have come first, or it's redundant). However, there's a problem: sometimes the pointer test might be inside a macro, meaning that the programmer isn't deliberately writing redundant tests. So at least in the case of Coverity, the warning does not appear if the redundant NULL check is inside a macro (they had to do that to get rid of a lot of noise).