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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
// RUN: %check_clang_tidy %s bugprone-integer-division %t
// Functions expecting a floating-point parameter.
void floatArg(float x) {}
void doubleArg(double x) {}
void longDoubleArg(long double x) {}
// Functions expected to return a floating-point value.
float singleDiv() {
int x = -5;
int y = 2;
return x/y;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: result of integer division used in
}
double wrongOrder(int x, int y) {
return x/y/0.1;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: result of integer division used in
}
long double rightOrder(int x, int y) {
return 0.1/x/y; // OK
}
// Typical mathematical functions.
float sin(float);
double acos(double);
long double tanh(long double);
namespace std {
using ::sin;
}
template <typename T>
void intDivSin(T x) {
sin(x);
}
int intFunc(int);
struct X {
int n;
void m() {
sin(n / 3);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: result of integer division used in
}
};
void integerDivision() {
char a = 2;
short b = -5;
int c = 9784;
enum third { x, y, z=2 };
third d = z;
char e[] = {'a', 'b', 'c'};
char f = *(e + 1 / a);
bool g = 1;
sin(1 + c / (2 + 2));
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of integer division used in
sin(c / (1 + .5));
sin((c + .5) / 3);
sin(intFunc(3) / 5);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: result of integer division used in
acos(2 / intFunc(7));
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: result of integer division used in
floatArg(1 + 2 / 3);
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: result of integer division used in
sin(1 + 2 / 3);
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of integer division used in
intFunc(sin(1 + 2 / 3));
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: result of integer division used in
floatArg(1 + intFunc(1 + 2 / 3));
floatArg(1 + 3 * intFunc(a / b));
1 << (2 / 3);
1 << intFunc(2 / 3);
#define M_SIN sin(a / b);
M_SIN
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: result of integer division used in
intDivSin<float>(a / b);
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: result of integer division used in
intDivSin<double>(c / d);
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: result of integer division used in
intDivSin<long double>(f / g);
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: result of integer division used in
floatArg(1 / 3);
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of integer division used in
doubleArg(a / b);
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: result of integer division used in
longDoubleArg(3 / d);
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: result of integer division used in
floatArg(a / b / 0.1);
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of integer division used in
doubleArg(1 / 3 / 0.1);
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: result of integer division used in
longDoubleArg(2 / 3 / 5);
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: result of integer division used in
std::sin(2 / 3);
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of integer division used in
::acos(7 / d);
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: result of integer division used in
tanh(f / g);
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: result of integer division used in
floatArg(0.1 / a / b);
doubleArg(0.1 / 3 / 1);
singleDiv();
wrongOrder(a,b);
rightOrder(a,b);
sin(a / b);
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: result of integer division used in
acos(f / d);
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: result of integer division used in
tanh(c / g);
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: result of integer division used in
sin(3.0 / a);
acos(b / 3.14);
tanh(3.14 / f / g);
}
|