This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/67764] -Wconversion generates false warnings for bitmask+cast expressions
- From: "egall at gwmail dot gwu.edu" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 29 Sep 2015 19:57:02 +0000
- Subject: [Bug c/67764] -Wconversion generates false warnings for bitmask+cast expressions
- Auto-submitted: auto-generated
- References: <bug-67764-4 at http dot gcc dot gnu dot org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67764
Eric Gallager <egall at gwmail dot gwu.edu> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |egall at gwmail dot gwu.edu
--- Comment #1 from Eric Gallager <egall at gwmail dot gwu.edu> ---
Remember, x |= y is the same as x = (x | y). So if we rewrite the problematic
conversions like that, it becomes clearer where the warning is really pointing:
$ /usr/local/bin/gcc -c conversion_bug.c -Wconversion
conversion_bug.c: In function â_setbitâ:
conversion_bug.c:4:13: warning: conversion to âunsigned charâ from âintâ may
alter its value [-Wconversion]
b[i / 8] = (b[i / 8] | (unsigned char)(1 << (i % 8)));
^
conversion_bug.c: In function â_mask_stupidâ:
conversion_bug.c:15:9: warning: conversion to âunsigned charâ from âintâ may
alter its value [-Wconversion]
b[i] = (b[i] | (unsigned char)i);
^
If you cast the entire expression after it has been re-written like that, the
warning goes away:
b[i / 8] = (unsigned char)(b[i / 8] | (unsigned char)(1 << (i % 8)));
b[i] = (unsigned char)(b[i] | (unsigned char)i);
(no warnings generated for either of those)