|
| 1 | + |
| 2 | +typedef long double MYLD; |
| 3 | + |
| 4 | +bool getBool(); |
| 5 | +int getInt(); |
| 6 | +float getFloat(); |
| 7 | +double getDouble(); |
| 8 | +MYLD getMyLD(); |
| 9 | +float *getFloatPtr(); |
| 10 | +float &getFloatRef(); |
| 11 | +const float &getConstFloatRef(); |
| 12 | + |
| 13 | +void setPosInt(int x); |
| 14 | +void setPosFloat(float x); |
| 15 | + |
| 16 | +double round(double x); |
| 17 | +float roundf(float x); |
| 18 | + |
| 19 | +void test1() |
| 20 | +{ |
| 21 | + // simple |
| 22 | + |
| 23 | + if (getBool()) |
| 24 | + { |
| 25 | + setPosInt(getBool()); |
| 26 | + setPosFloat(getBool()); |
| 27 | + } |
| 28 | + if (getInt()) |
| 29 | + { |
| 30 | + setPosInt(getInt()); |
| 31 | + setPosFloat(getInt()); |
| 32 | + } |
| 33 | + if (getFloat()) // BAD |
| 34 | + { |
| 35 | + setPosInt(getFloat()); // BAD |
| 36 | + setPosFloat(getFloat()); |
| 37 | + } |
| 38 | + if (getDouble()) // BAD |
| 39 | + { |
| 40 | + setPosInt(getDouble()); // BAD |
| 41 | + setPosFloat(getDouble()); |
| 42 | + } |
| 43 | + if (getMyLD()) // BAD |
| 44 | + { |
| 45 | + setPosInt(getMyLD()); // BAD |
| 46 | + setPosFloat(getMyLD()); |
| 47 | + } |
| 48 | + if (getFloatPtr()) |
| 49 | + { |
| 50 | + // ... |
| 51 | + } |
| 52 | + if (getFloatRef()) // BAD [NOT DETECTED] |
| 53 | + { |
| 54 | + setPosInt(getFloatRef()); // BAD [NOT DETECTED] |
| 55 | + setPosFloat(getFloatRef()); |
| 56 | + } |
| 57 | + if (getConstFloatRef()) // BAD [NOT DETECTED] |
| 58 | + { |
| 59 | + setPosInt(getConstFloatRef()); // BAD [NOT DETECTED] |
| 60 | + setPosFloat(getConstFloatRef()); |
| 61 | + } |
| 62 | + |
| 63 | + // explicit cast |
| 64 | + |
| 65 | + if ((bool)getInt()) |
| 66 | + { |
| 67 | + setPosInt(getInt()); |
| 68 | + setPosFloat((float)getInt()); |
| 69 | + } |
| 70 | + if ((bool)getFloat()) |
| 71 | + { |
| 72 | + setPosInt((int)getFloat()); |
| 73 | + setPosFloat(getFloat()); |
| 74 | + } |
| 75 | + |
| 76 | + // explicit rounding |
| 77 | + |
| 78 | + if (roundf(getFloat())) |
| 79 | + { |
| 80 | + setPosInt(roundf(getFloat())); |
| 81 | + setPosFloat(roundf(getFloat())); |
| 82 | + } |
| 83 | + if (round(getDouble())) |
| 84 | + { |
| 85 | + setPosInt(round(getDouble())); |
| 86 | + setPosFloat(round(getDouble())); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +double pow(double x, double y); |
| 91 | + |
| 92 | +int test2(double v, double w, int n) |
| 93 | +{ |
| 94 | + switch (n) |
| 95 | + { |
| 96 | + case 1: |
| 97 | + return pow(2, v); // GOOD |
| 98 | + case 2: |
| 99 | + return pow(10, v); // GOOD |
| 100 | + case 3: |
| 101 | + return pow(2.5, v); // BAD |
| 102 | + case 4: |
| 103 | + return pow(v, 2); // BAD |
| 104 | + case 5: |
| 105 | + return pow(v, w); // BAD |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +double myRound1(double v) |
| 110 | +{ |
| 111 | + return round(v); |
| 112 | +} |
| 113 | + |
| 114 | +double myRound2(double v) |
| 115 | +{ |
| 116 | + double result = round(v); |
| 117 | + |
| 118 | + return result; |
| 119 | +} |
| 120 | + |
| 121 | +double myRound3(double v) |
| 122 | +{ |
| 123 | + return (v > 0) ? round(v) : 0; |
| 124 | +} |
| 125 | + |
| 126 | +void test3() |
| 127 | +{ |
| 128 | + int i = myRound1(1.5); // GOOD |
| 129 | + int j = myRound2(2.5); // GOOD |
| 130 | + int k = myRound3(3.5); // GOOD |
| 131 | +} |
0 commit comments