https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96460
Bug ID: 96460
Summary: Warn about signed module that is converted to unsigned
value
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Keywords: diagnostic
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: fw at gcc dot gnu.org
Target Milestone: ---
I do not know how many warnings it would generate to warn for code like this
(if i is not known to be non-negative):
unsigned long
f2 (int i)
{
return i % 16;
}
The problem is that if i is negative, i % 16 is also negative, and the result
is a large unsigned value. This is rarely what is intended. Even if the
negative input never occurs, GCC still needs to generate additional code that
is valid for this case.
A fix-hit hint should suggests & for powers of two and a cast to the
appropriate unsigned type for other operands.
I'm not sure what one would write to suppress this warning in cases where it is
inappropriate, maybe (int) i % 16.