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