https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83151

--- Comment #4 from Liu Hao <lh_mouse at 126 dot com> ---
I do care about these warnings and that is why these warnings are enabled.
However the one in the original post seems nothing but false positive to me.


I know what the standard says about integer promotion (when used as an
arithmetic operand, any bit-field with type `int`, `unsigned` or `_Bool` will
be promoted to `int` if `int` is capable of representing all of its values
possible, and to `unsigned` otherwise). The conversion from signed types to
unsigned types are clearly implementation-defined, but according to GCC manual
"GCC supports only two’s complement integer types, and all bit patterns are
ordinary values" so conversion from unsigned types to their signed counterparts
is no-op.

No doubt adding casts would mute the warning, but sometimes it is not possible
e.g. in a compound assignment expression:

```
void set_mask(uint8_t * mask, uint8_t to_rm, uint8_t to_add){
  *mask &= ~to_rm; // This leads to a warning about conversion from `int` to
`uint8_t`.
  *mask |= to_add; // There is no warning because GCC knows the upper bits are
out of interest.
}
```

These assignment expressions have to be rewritten to use the non-compound form:

```
void set_mask(uint8_t * mask, uint8_t to_rm, uint8_t to_add){
  *mask = (uint8_t)((*mask & ~to_rm) | to_add);
}
```
, being longer, more complex, thus harder to maintain.


I believe it can still be improved, since promotion from `uint8_t` to `int`
brings in bit zeroes. The higher bits can't be set to bit ones by bitwise
and'ing with anything, hence the waring is a false positive.

Reply via email to