1 // RUN: %check_clang_tidy %s abseil-duration-factory-float %t -- -- -I%S/Inputs
2 
3 #include "absl/time/time.h"
4 
ConvertFloatTest()5 void ConvertFloatTest() {
6   absl::Duration d;
7 
8   d = absl::Seconds(60.0);
9   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Seconds [abseil-duration-factory-float]
10   // CHECK-FIXES: absl::Seconds(60);
11   d = absl::Minutes(300.0);
12   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Minutes [abseil-duration-factory-float]
13   // CHECK-FIXES: absl::Minutes(300);
14 
15   d = absl::Milliseconds(1e2);
16   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Milliseconds [abseil-duration-factory-float]
17   // CHECK-FIXES: absl::Milliseconds(100);
18   d = absl::Seconds(3.0f);
19   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Seconds [abseil-duration-factory-float]
20   // CHECK-FIXES: absl::Seconds(3);
21   d = absl::Seconds(3.);
22   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Seconds [abseil-duration-factory-float]
23   // CHECK-FIXES: absl::Seconds(3);
24   d = absl::Seconds(0x3.p0);
25   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Seconds [abseil-duration-factory-float]
26   // CHECK-FIXES: absl::Seconds(3);
27   d = absl::Seconds(0x3.p1);
28   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Seconds [abseil-duration-factory-float]
29   // CHECK-FIXES: absl::Seconds(6);
30 
31 
32   // Ignored expressions
33   d = absl::Seconds(.001);
34   d = absl::Seconds(.100);
35   d = ::absl::Seconds(1);
36   d = ::absl::Minutes(1);
37   d = ::absl::Hours(1);
38   d = absl::Seconds(0x3.4p1);
39 
40   // Negative literals (we don't yet handle this case)
41   d = absl::Seconds(-3.0);
42 
43   // This is bigger than we can safely fit in a positive int32, so we ignore it.
44   d = absl::Seconds(1e12);
45 
46   int x;
47   d = absl::Seconds(x);
48   float y;
49   d = absl::Minutes(y);
50 
51 #define SECONDS(x) absl::Seconds(x)
52   SECONDS(60);
53 #undef SECONDS
54 #define THIRTY 30.0
55   d = absl::Seconds(THIRTY);
56 #undef THIRTY
57 }
58 
59 template <int N>
InTemplate()60 void InTemplate() {
61   absl::Duration d;
62 
63   d = absl::Seconds(N);  // 1
64   // ^ No replacement here.
65 
66   d = absl::Minutes(1.0);  // 2
67   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Minutes [abseil-duration-factory-float]
68   // CHECK-FIXES: absl::Minutes(1);  // 2
69 }
70 
Instantiate()71 void Instantiate() {
72   InTemplate<60>();
73   InTemplate<1>();
74 }
75 
ConvertCastTest()76 void ConvertCastTest() {
77   absl::Duration d;
78 
79   d = absl::Seconds(static_cast<double>(5));
80   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Seconds [abseil-duration-factory-float]
81   // CHECK-FIXES: absl::Seconds(5);
82 
83   d = absl::Minutes(static_cast<float>(5));
84   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Minutes [abseil-duration-factory-float]
85   // CHECK-FIXES: absl::Minutes(5);
86 
87   d = absl::Seconds((double) 5);
88   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Seconds [abseil-duration-factory-float]
89   // CHECK-FIXES: absl::Seconds(5);
90 
91   d = absl::Minutes((float) 5);
92   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Minutes [abseil-duration-factory-float]
93   // CHECK-FIXES: absl::Minutes(5);
94 
95   d = absl::Seconds(double(5));
96   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Seconds [abseil-duration-factory-float]
97   // CHECK-FIXES: absl::Seconds(5);
98 
99   d = absl::Minutes(float(5));
100   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: use the integer version of absl::Minutes [abseil-duration-factory-float]
101   // CHECK-FIXES: absl::Minutes(5);
102 
103   // This should not be flagged
104   d = absl::Seconds(static_cast<int>(5.0));
105   d = absl::Seconds((int) 5.0);
106   d = absl::Seconds(int(5.0));
107 }
108