[cfe-dev] warning with conditional operator and printf, is this a bug ?

John McCall rjmccall at apple.com
Fri Nov 4 18:00:14 PDT 2011


On Nov 4, 2011, at 4:26 PM, David Blaikie wrote:
>> 1) There is no a reason to do any arithmetic conversions to the
>> second or third operand of the conditional operator in this case.
>> So the result of both operands should be original type, which is
>> unit16_t for both operands.
> 
> Sorry, this is the bit I don't follow. "the usual arithmetic
> conversion" isn't something that requires a "reason" it's a thing of
> its own. I quoted it in my original reply, but applying "the usual
> arithmetic conversion" to "uint16_t" should result in "int", unless
> I'm missing something.

You're right that formally we have to do a promotion here.  It does
not thereby follow that we have to treat an obviously promoted
value as a completely opaque value of the promoted type.  Indeed,
we already don't:  the language specifies that an unsigned short
must be promoted to int in basically every context, and yet we
manage to not complain about the following, because doing so
would be daft:
  printf("%hu", (uint16_t) x);

The warning exists to serve the user and tell them when they've
likely done something wrong, not to impress them with our
awesome knowledge of the C type system.  There are no cases
here where the implicit promotion to int prevents the user's code
from working as obviously intended.  Therefore, this is a false
positive.

John.



More information about the cfe-dev mailing list