bugprone-incorrect-roundings.cpp
3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// RUN: %check_clang_tidy %s bugprone-incorrect-roundings %t
void b(int x) {}
void f1() {
float f;
double d;
long double ld;
int x;
x = (d + 0.5);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5) to integer leads to incorrect rounding; consider using lround (#include <cmath>) instead [bugprone-incorrect-roundings]
x = (d + 0.5f);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
x = (f + 0.5);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
x = (f + 0.5f);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
x = (0.5 + d);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
x = (0.5f + d);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
x = (0.5 + ld);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
x = (0.5f + ld);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
x = (0.5 + f);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
x = (0.5f + f);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
x = (int)(d + 0.5);
// CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
x = (int)(d + 0.5f);
// CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
x = (int)(ld + 0.5);
// CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
x = (int)(ld + 0.5f);
// CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
x = (int)(f + 0.5);
// CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
x = (int)(f + 0.5f);
// CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
x = (int)(0.5 + d);
// CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
x = (int)(0.5f + d);
// CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
x = (int)(0.5 + ld);
// CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
x = (int)(0.5f + ld);
// CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
x = (int)(0.5 + f);
// CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
x = (int)(0.5f + f);
// CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
x = static_cast<int>(d + 0.5);
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
x = static_cast<int>(d + 0.5f);
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
x = static_cast<int>(ld + 0.5);
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
x = static_cast<int>(ld + 0.5f);
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
x = static_cast<int>(f + 0.5);
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
x = static_cast<int>(f + 0.5f);
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
x = static_cast<int>(0.5 + d);
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
x = static_cast<int>(0.5f + d);
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
x = static_cast<int>(0.5 + ld);
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
x = static_cast<int>(0.5f + ld);
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
x = static_cast<int>(0.5 + f);
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
x = static_cast<int>(0.5f + f);
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
// Don't warn if constant is not 0.5.
x = (int)(d + 0.6);
x = (int)(0.6 + d);
// Don't warn if binary operator is not directly beneath cast.
x = (int)(1 + (0.5 + f));
}