Bug 97287

Summary: Warn for expanding range of an arithmetic type
Product: gcc Reporter: Matthew Wilcox <matthew>
Component: cAssignee: Not yet assigned to anyone <unassigned>
Status: UNCONFIRMED ---    
Severity: normal Keywords: diagnostic
Priority: P3    
Version: 11.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description Matthew Wilcox 2020-10-04 17:21:58 UTC
I've just fixed multiple instances of bugs that look a lot like function f() when they should have been function g().  This affects filesystems in Linux which have to remember to cast an unsigned long to an off_t before shifting (or there will be a bug on 32-bit kernels when dealing with files that are larger than 4GB).

When I looked for a warning option to add, I thought -Warith-conversions might do the job, but it doesn't.  Maybe this functionality should be added there, or maybe it should have its own warning.

I think what we're looking for is an operation which expands the range of the type (left shift, multiplication, addition; maybe subtraction?) and the rules of C require the operation to be done in a narrower type, but the result is assigned to a wider type.

unsigned long long f(unsigned int i) { return i * 4096; }
unsigned long long g(unsigned int i) { return i * 4096ULL; }