| 1 | // RUN: %check_clang_tidy %s bugprone-integer-division %t |
| 2 | |
| 3 | // Functions expecting a floating-point parameter. |
| 4 | void floatArg(float x) {} |
| 5 | void doubleArg(double x) {} |
| 6 | void longDoubleArg(long double x) {} |
| 7 | |
| 8 | // Functions expected to return a floating-point value. |
| 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 | |
| 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 | |
| 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> |
| 35 | void intDivSin(T x) { |
| 36 | sin(x); |
| 37 | } |
| 38 | |
| 39 | int intFunc(int); |
| 40 | |
| 41 | struct X { |
| 42 | int n; |
| 43 | void m() { |
| 44 | sin(n / 3); |
| 45 | // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: result of integer division used in |
| 46 | } |
| 47 | }; |
| 48 | |
| 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(x: 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(x: 1 + intFunc(1 + 2 / 3)); |
| 77 | floatArg(x: 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>(x: a / b); |
| 87 | // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: result of integer division used in |
| 88 | intDivSin<double>(x: c / d); |
| 89 | // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: result of integer division used in |
| 90 | intDivSin<long double>(x: f / g); |
| 91 | // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: result of integer division used in |
| 92 | |
| 93 | floatArg(x: 1 / 3); |
| 94 | // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of integer division used in |
| 95 | doubleArg(x: a / b); |
| 96 | // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: result of integer division used in |
| 97 | longDoubleArg(x: 3 / d); |
| 98 | // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: result of integer division used in |
| 99 | floatArg(x: a / b / 0.1); |
| 100 | // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of integer division used in |
| 101 | doubleArg(x: 1 / 3 / 0.1); |
| 102 | // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: result of integer division used in |
| 103 | longDoubleArg(x: 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(x: 0.1 / a / b); |
| 114 | doubleArg(x: 0.1 / 3 / 1); |
| 115 | |
| 116 | singleDiv(); |
| 117 | wrongOrder(x: a,y: b); |
| 118 | rightOrder(x: a,y: 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 | |