man/checkers/truncLongCast.md
Message: int result is assigned to long variable. If the variable is long to avoid loss of information, then you have loss of information.
Category: Type Safety
Severity: Style
Language: C/C++
This checker warns when a calculation has type 'int' and it could potentially overflow that and the result is implicitly or explicitly converted to a larger integer type after the loss of information has already occured.
The motivation of this checker is to catch bugs. Unintentional loss of information.
You can fix these warnings by:
Before:
void foo(int32_t y) {
int64_t x = y * y; // <- warning
}
After (explicit cast):
void foo(int32_t y) {
int64_t x = (int64_t)y * y; // <- 64-bit multiplication
}
After (explicitly truncate the result):
void foo(int32_t y) {
int64_t x = (int32_t)(y * y); // redundant cast makes it explicit that your intention is to truncate the result to 32-bit
}
After (change type of assigned variable):
void foo(int32_t y) {
int32_t x = y * y;
}