1 // RUN: %check_clang_tidy %s bugprone-integer-division %t
2
3 // Functions expecting a floating-point parameter.
floatArg(float x)4 void floatArg(float x) {}
doubleArg(double x)5 void doubleArg(double x) {}
longDoubleArg(long double x)6 void longDoubleArg(long double x) {}
7
8 // Functions expected to return a floating-point value.
singleDiv()9 float singleDiv() {
10 int x = -5;
11 int y = 2;
12 return x/y;
13 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: result of integer division used in
14 }
15
wrongOrder(int x,int y)16 double wrongOrder(int x, int y) {
17 return x/y/0.1;
18 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: result of integer division used in
19 }
20
rightOrder(int x,int y)21 long double rightOrder(int x, int y) {
22 return 0.1/x/y; // OK
23 }
24
25 // Typical mathematical functions.
26 float sin(float);
27 double acos(double);
28 long double tanh(long double);
29
30 namespace std {
31 using ::sin;
32 }
33
34 template <typename T>
intDivSin(T x)35 void intDivSin(T x) {
36 sin(x);
37 }
38
39 int intFunc(int);
40
41 struct X {
42 int n;
mX43 void m() {
44 sin(n / 3);
45 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: result of integer division used in
46 }
47 };
48
integerDivision()49 void integerDivision() {
50 char a = 2;
51 short b = -5;
52 int c = 9784;
53 enum third { x, y, z=2 };
54 third d = z;
55 char e[] = {'a', 'b', 'c'};
56 char f = *(e + 1 / a);
57 bool g = 1;
58
59 sin(1 + c / (2 + 2));
60 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of integer division used in
61 sin(c / (1 + .5));
62 sin((c + .5) / 3);
63
64 sin(intFunc(3) / 5);
65 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: result of integer division used in
66 acos(2 / intFunc(7));
67 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: result of integer division used in
68
69 floatArg(1 + 2 / 3);
70 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: result of integer division used in
71 sin(1 + 2 / 3);
72 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of integer division used in
73 intFunc(sin(1 + 2 / 3));
74 // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: result of integer division used in
75
76 floatArg(1 + intFunc(1 + 2 / 3));
77 floatArg(1 + 3 * intFunc(a / b));
78
79 1 << (2 / 3);
80 1 << intFunc(2 / 3);
81
82 #define M_SIN sin(a / b);
83 M_SIN
84 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: result of integer division used in
85
86 intDivSin<float>(a / b);
87 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: result of integer division used in
88 intDivSin<double>(c / d);
89 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: result of integer division used in
90 intDivSin<long double>(f / g);
91 // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: result of integer division used in
92
93 floatArg(1 / 3);
94 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of integer division used in
95 doubleArg(a / b);
96 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: result of integer division used in
97 longDoubleArg(3 / d);
98 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: result of integer division used in
99 floatArg(a / b / 0.1);
100 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of integer division used in
101 doubleArg(1 / 3 / 0.1);
102 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: result of integer division used in
103 longDoubleArg(2 / 3 / 5);
104 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: result of integer division used in
105
106 std::sin(2 / 3);
107 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of integer division used in
108 ::acos(7 / d);
109 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: result of integer division used in
110 tanh(f / g);
111 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: result of integer division used in
112
113 floatArg(0.1 / a / b);
114 doubleArg(0.1 / 3 / 1);
115
116 singleDiv();
117 wrongOrder(a,b);
118 rightOrder(a,b);
119
120 sin(a / b);
121 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: result of integer division used in
122 acos(f / d);
123 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: result of integer division used in
124 tanh(c / g);
125 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: result of integer division used in
126
127 sin(3.0 / a);
128 acos(b / 3.14);
129 tanh(3.14 / f / g);
130 }
131