This is about the correct diagnostics when short ints get promoted during "usual arithmetic conversions". During operation / a diagnostic could be reasonably emitted, but during /= none should be emitted.
Behaviour for gcc-trunk and clang-trunk seems OK (neither emits diagnostic for first or second case below)... until...
we add the entirely unrelated -fsanitize=undefined ... after which, completely bizarrely:
gcc-trunk emits a diagnostic for both cases. It really shouldn't for the 2nd case, at least.
Is this a bug in gcc?
Godbolt link with -O3 - same result
int main() {
short sum = 50;
short count = 10;
// sum and count get promoted to int for the "usual arithmetic conversions"
// then the assignment could result in a reasonable -Wconversion diagnostic for reduction back
// to short
// However clang-trunk and gcc-trunk choose NOT TO issue a diagnostic with -Wconversion enabled
short avg1 = sum / count;
// we should be able to prevent promotion to int by using /= assignment operator.
// Both clang-trunk and gcc-trunk, correctly, DON'T issue a diagnostic with -Wconversion enabled
auto tmp = sum;
tmp /= count;
short avg2 = tmp;
// HOWEVER if we add -fsanitize=undefined for both compilers
// then, bizarrly, gcc-trunk issues a diagnostic for both cases above and clang-trunk still for
// neither
// none of these ever issue a diagnostic (nor should they)
tmp += count; // all
tmp -= count; // are
tmp *= count; // silent
return (avg1 + avg2) & 0xff; // prevent "unused" diagnostics
}
EDIT
Here is the bug I filed for GCC: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104616