You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**Message**: int result is assigned to long variable. If the variable is long to avoid loss of information, then you have loss of information.<br/>
4
+
**Category**: Type Safety<br/>
5
+
**Severity**: Style<br/>
6
+
**Language**: C/C++
7
+
8
+
## Description
9
+
10
+
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
11
+
integer type after the loss of information has already occured.
12
+
13
+
## Motivation
14
+
15
+
The motivation of this checker is to catch bugs. Unintentional loss of information.
16
+
17
+
## How to fix
18
+
19
+
You can fix these warnings by:
20
+
1. Add explicit cast to avoid loss of information
21
+
2. Explicitly truncate the result
22
+
3. Change type of assigned variable
23
+
24
+
Before:
25
+
```cpp
26
+
voidfoo(int32_t y) {
27
+
int64_t x = y * y; // <- warning
28
+
}
29
+
```
30
+
31
+
After (explicit cast):
32
+
```cpp
33
+
void foo(int32_t y) {
34
+
int64_t x = (int64_t)y * y; // <- 64-bit multiplication
35
+
}
36
+
```
37
+
38
+
After (explicitly truncate the result):
39
+
```cpp
40
+
voidfoo(int32_t y) {
41
+
int64_t x = (int32_t)(y * y); // redundant cast makes it explicit that your intention is to truncate the result to 32-bit
0 commit comments