| 1 |
// RUN: %check_clang_tidy %s cppcoreguidelines-narrowing-conversions %t \ |
| 2 |
// RUN: -- -- -target x86_64-unknown-linux -fsigned-char |
| 3 |
|
| 4 |
namespace floats { |
| 5 |
|
| 6 |
void narrow_constant_floating_point_to_int_not_ok(double d) { |
| 7 |
int i = 0; |
| 8 |
i += 0.5; |
| 9 |
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from constant 'double' to 'int' [cppcoreguidelines-narrowing-conversions] |
| 10 |
i += 0.5f; |
| 11 |
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from constant 'float' to 'int' [cppcoreguidelines-narrowing-conversions] |
| 12 |
i *= 0.5f; |
| 13 |
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from constant 'float' to 'int' [cppcoreguidelines-narrowing-conversions] |
| 14 |
i /= 0.5f; |
| 15 |
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from constant 'float' to 'int' [cppcoreguidelines-narrowing-conversions] |
| 16 |
i += (double)0.5f; |
| 17 |
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from constant 'double' to 'int' [cppcoreguidelines-narrowing-conversions] |
| 18 |
i += 2.0; |
| 19 |
i += 2.0f; |
| 20 |
} |
| 21 |
|
| 22 |
double operator"" _double(unsigned long long); |
| 23 |
|
| 24 |
float narrow_double_to_float_return() { |
| 25 |
return 0.5; |
| 26 |
} |
| 27 |
|
| 28 |
void narrow_double_to_float_not_ok(double d) { |
| 29 |
float f; |
| 30 |
f = d; |
| 31 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: narrowing conversion from 'double' to 'float' [cppcoreguidelines-narrowing-conversions] |
| 32 |
f = 15_double; |
| 33 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: narrowing conversion from 'double' to 'float' [cppcoreguidelines-narrowing-conversions] |
| 34 |
f += d; |
| 35 |
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'double' to 'float' [cppcoreguidelines-narrowing-conversions] |
| 36 |
f = narrow_double_to_float_return(); |
| 37 |
} |
| 38 |
|
| 39 |
void narrow_fp_constants() { |
| 40 |
float f; |
| 41 |
f = 0.5; // [dcl.init.list] 7.2 : in-range fp constant to narrower float is not a narrowing. |
| 42 |
|
| 43 |
f = __builtin_huge_valf(); // max float is not narrowing. |
| 44 |
f = -__builtin_huge_valf(); // -max float is not narrowing. |
| 45 |
f = __builtin_inff(); // float infinity is not narrowing. |
| 46 |
f = __builtin_nanf("0"); // float NaN is not narrowing. |
| 47 |
|
| 48 |
f = __builtin_huge_val(); // max double is not within-range of float. |
| 49 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: narrowing conversion from constant 'double' to 'float' [cppcoreguidelines-narrowing-conversions] |
| 50 |
f = -__builtin_huge_val(); // -max double is not within-range of float. |
| 51 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: narrowing conversion from constant 'double' to 'float' [cppcoreguidelines-narrowing-conversions] |
| 52 |
f = __builtin_inf(); // double infinity is not within-range of float. |
| 53 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: narrowing conversion from constant 'double' to 'float' [cppcoreguidelines-narrowing-conversions] |
| 54 |
f = __builtin_nan("0"); // double NaN is not narrowing. |
| 55 |
} |
| 56 |
|
| 57 |
double false_positive_const_qualified_cast(bool t) { |
| 58 |
double b = 1.0; |
| 59 |
constexpr double a = __builtin_huge_val(); |
| 60 |
// PR49498 The constness difference of 'a' and 'b' results in an implicit cast. |
| 61 |
return t ? b : a; |
| 62 |
} |
| 63 |
|
| 64 |
} // namespace floats |