So would you require a comment for each of the following?
float f = 1.0; /* conversion from double to float */
This is obviously safe, and it doesn't really particularly matter
whether the programmer realized there is conversion or not. I would
hope no rational compiler would issue a warning for this code except
in some crazy pedantic mode no sane person would use.
short s = 1; /* conversion from int to short */
Same here. This is "obviously safe", obvious even to automated tools.
int *p = NULL; /* conversion from ? to int* */
In the latter case, the type of NULL is likely to be int, but it could
be void* or even some integral type other than int.
And what about this?
Again, same point. Obviously safe, no rational reviewer (human or
automated) would be concerned by these cases.
printf("Hello, world\n");
Here the identifier printf is converted from a function type to a
pointer-to-function type (assuming printf isn't defined as a macro),
and the string literal "Hello, world\n" is converted from char[14] to
char*, and then from char* to const char*.
C is so full of implicit conversions that trying to comment all of
them is likely to result in more comments than code. On the other
hand, if you want to require comments for some subset of conversions,
that might be reasonable -- assuming you can define the subset clearly
enough.
The subset is not perfectly precisely defined, IMO, but the following
considerations apply:
1) If it quiets a rational compiler warning, that weighs in favor of a
cast.
2) If the conversion is obviously safe, that weighs against a cast.
3) If the cast indicates the programmer was aware of an unobvious
conversion, that weighs in favor of the cast.
4) If the cast points to a reviewer that a conversion takes place,
that weighs in favor of the cast.
In most cases, these factors all turn into the same thing. If it's
obviously safe, there's nothing to know the programmer is aware of or
warn the reviewer about. Most compilers, even at high warning levels,
only issue warnings about conversion that are not obviously safe.
So gray areas are the exception rather than the rule.
GCC does issue some warnings in cases where, IMO, such a warning is
ridiculous. This makes a policy of "silence all compiler warnings by
vouching for the safety of the conversion" untenable.
An example would be (sorry, only have a C++ example handy, but the
concept is the same):
unsigned char uc_xor(unsigned char bar, unsigned char qux)
{
unsigned char ret=bar;
ret^=qux; // G++ 4.3.3 alerts here
return ret;
}
g++ 4.3.3 -Wconversion ->
"warning: conversion to 'unsigned char' from 'int' may alter its
value"
I don't know offhand of any cases where GCC, on C code, issues
obviously bogus warnings, but it wouldn't surprise me if there were
some. And I wouldn't suggest butchering your code to "fix" that.
(I'm assuming you really meant "conversions", not "casts".)
Definitely. A hard rule that all conversions must be commented or,
worse, identified by casts, is truly terrible. Anyone who actually had
such a rule would, we hope, at least have some implicit understanding
that it doesn't apply to trivial, obviously safe cases.
DS