131 lines · cpp
1// RUN: %check_clang_tidy %s bugprone-integer-division %t2 3// Functions expecting a floating-point parameter.4void floatArg(float x) {}5void doubleArg(double x) {}6void longDoubleArg(long double x) {}7 8// Functions expected to return a floating-point value.9float 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 in14}15 16double wrongOrder(int x, int y) {17 return x/y/0.1;18// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: result of integer division used in19}20 21long double rightOrder(int x, int y) {22 return 0.1/x/y; // OK23}24 25// Typical mathematical functions.26float sin(float);27double acos(double);28long double tanh(long double);29 30namespace std {31 using ::sin;32}33 34template <typename T>35void intDivSin(T x) {36 sin(x);37}38 39int intFunc(int);40 41struct X {42 int n;43 void m() {44 sin(n / 3);45// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: result of integer division used in46 }47};48 49void 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 in61 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 in66 acos(2 / intFunc(7));67// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: result of integer division used in68 69 floatArg(1 + 2 / 3);70// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: result of integer division used in71 sin(1 + 2 / 3);72// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of integer division used in73 intFunc(sin(1 + 2 / 3));74// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: result of integer division used in75 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_SIN84// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: result of integer division used in85 86 intDivSin<float>(a / b);87// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: result of integer division used in88 intDivSin<double>(c / d);89// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: result of integer division used in90 intDivSin<long double>(f / g);91// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: result of integer division used in92 93 floatArg(1 / 3);94// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of integer division used in95 doubleArg(a / b);96// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: result of integer division used in97 longDoubleArg(3 / d);98// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: result of integer division used in99 floatArg(a / b / 0.1);100// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of integer division used in101 doubleArg(1 / 3 / 0.1);102// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: result of integer division used in103 longDoubleArg(2 / 3 / 5);104// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: result of integer division used in105 106 std::sin(2 / 3);107// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of integer division used in108 ::acos(7 / d);109// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: result of integer division used in110 tanh(f / g);111// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: result of integer division used in112 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 in122 acos(f / d);123// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: result of integer division used in124 tanh(c / g);125// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: result of integer division used in126 127 sin(3.0 / a);128 acos(b / 3.14);129 tanh(3.14 / f / g);130}131