1// RUN: %check_clang_tidy %s bugprone-incorrect-roundings %t
2
3void b(int x) {}
4
5void f1() {
6 float f;
7 double d;
8 long double ld;
9 int x;
10
11 x = (d + 0.5);
12 // 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]
13 x = (d + 0.5f);
14 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
15 x = (d + 0.5L);
16 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
17 x = (f + 0.5);
18 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
19 x = (f + 0.5f);
20 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
21 x = (f + 0.5L);
22 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
23 x = (0.5 + d);
24 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
25 x = (0.5f + d);
26 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
27 x = (0.5L + d);
28 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
29 x = (0.5 + ld);
30 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
31 x = (0.5f + ld);
32 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
33 x = (0.5L + ld);
34 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
35 x = (0.5 + f);
36 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
37 x = (0.5f + f);
38 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
39 x = (0.5L + f);
40 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: casting (double + 0.5)
41 x = (int)(d + 0.5);
42 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
43 x = (int)(d + 0.5f);
44 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
45 x = (int)(d + 0.5L);
46 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
47 x = (int)(ld + 0.5);
48 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
49 x = (int)(ld + 0.5f);
50 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
51 x = (int)(ld + 0.5L);
52 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
53 x = (int)(f + 0.5);
54 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
55 x = (int)(f + 0.5f);
56 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
57 x = (int)(f + 0.5L);
58 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
59 x = (int)(0.5 + d);
60 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
61 x = (int)(0.5f + d);
62 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
63 x = (int)(0.5L + d);
64 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
65 x = (int)(0.5 + ld);
66 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
67 x = (int)(0.5f + ld);
68 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
69 x = (int)(0.5L + ld);
70 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
71 x = (int)(0.5 + f);
72 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
73 x = (int)(0.5f + f);
74 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
75 x = (int)(0.5L + f);
76 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: casting (double + 0.5)
77 x = static_cast<int>(d + 0.5);
78 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
79 x = static_cast<int>(d + 0.5f);
80 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
81 x = static_cast<int>(d + 0.5L);
82 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
83 x = static_cast<int>(ld + 0.5);
84 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
85 x = static_cast<int>(ld + 0.5f);
86 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
87 x = static_cast<int>(ld + 0.5L);
88 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
89 x = static_cast<int>(f + 0.5);
90 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
91 x = static_cast<int>(f + 0.5f);
92 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
93 x = static_cast<int>(f + 0.5L);
94 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
95 x = static_cast<int>(0.5 + d);
96 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
97 x = static_cast<int>(0.5f + d);
98 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
99 x = static_cast<int>(0.5L + d);
100 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
101 x = static_cast<int>(0.5 + ld);
102 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
103 x = static_cast<int>(0.5f + ld);
104 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
105 x = static_cast<int>(0.5L + ld);
106 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
107 x = static_cast<int>(0.5 + f);
108 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
109 x = static_cast<int>(0.5f + f);
110 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
111 x = static_cast<int>(0.5L + f);
112 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: casting (double + 0.5)
113
114 // Don't warn if constant is not 0.5.
115 x = (int)(d + 0.4999);
116 x = (int)(d + 0.4999f);
117 x = (int)(d + 0.4999L);
118 x = (int)(0.4999 + d);
119 x = (int)(0.4999f + d);
120 x = (int)(0.4999L + d);
121
122 x = (int)(d + -0.5);
123 x = (int)(-0.5 + d);
124 x = (int)(d + -0.5f);
125 x = (int)(-0.5f + d);
126 x = (int)(d + -0.5L);
127 x = (int)(-0.5L + d);
128
129 x = (int)(d + 0.5001);
130 x = (int)(d + 0.5001f);
131 x = (int)(d + 0.5001L);
132 x = (int)(0.5001 + d);
133 x = (int)(0.5001f + d);
134 x = (int)(0.5001L + d);
135
136 // Don't warn if binary operator is not directly beneath cast.
137 x = (int)(1 + (0.5 + f));
138}
139

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/incorrect-roundings.cpp