1 // RUN: %check_clang_tidy %s abseil-duration-conversion-cast %t -- -- -I%S/Inputs
2 
3 #include "absl/time/time.h"
4 
f()5 void f() {
6   absl::Duration d1;
7   double x;
8   int i;
9 
10   i = static_cast<int>(absl::ToDoubleHours(d1));
11   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]
12   // CHECK-FIXES: absl::ToInt64Hours(d1);
13   x = static_cast<float>(absl::ToInt64Hours(d1));
14   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]
15   // CHECK-FIXES: absl::ToDoubleHours(d1);
16   i = static_cast<int>(absl::ToDoubleMinutes(d1));
17   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]
18   // CHECK-FIXES: absl::ToInt64Minutes(d1);
19   x = static_cast<float>(absl::ToInt64Minutes(d1));
20   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]
21   // CHECK-FIXES: absl::ToDoubleMinutes(d1);
22   i = static_cast<int>(absl::ToDoubleSeconds(d1));
23   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]
24   // CHECK-FIXES: absl::ToInt64Seconds(d1);
25   x = static_cast<float>(absl::ToInt64Seconds(d1));
26   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]
27   // CHECK-FIXES: absl::ToDoubleSeconds(d1);
28   i = static_cast<int>(absl::ToDoubleMilliseconds(d1));
29   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]
30   // CHECK-FIXES: absl::ToInt64Milliseconds(d1);
31   x = static_cast<float>(absl::ToInt64Milliseconds(d1));
32   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]
33   // CHECK-FIXES: absl::ToDoubleMilliseconds(d1);
34   i = static_cast<int>(absl::ToDoubleMicroseconds(d1));
35   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]
36   // CHECK-FIXES: absl::ToInt64Microseconds(d1);
37   x = static_cast<float>(absl::ToInt64Microseconds(d1));
38   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]
39   // CHECK-FIXES: absl::ToDoubleMicroseconds(d1);
40   i = static_cast<int>(absl::ToDoubleNanoseconds(d1));
41   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]
42   // CHECK-FIXES: absl::ToInt64Nanoseconds(d1);
43   x = static_cast<float>(absl::ToInt64Nanoseconds(d1));
44   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]
45   // CHECK-FIXES: absl::ToDoubleNanoseconds(d1);
46 
47   // Functional-style casts
48   i = int(absl::ToDoubleHours(d1));
49   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]
50   // CHECK-FIXES: absl::ToInt64Hours(d1);
51   x = float(absl::ToInt64Microseconds(d1));
52   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]
53   // CHECK-FIXES: absl::ToDoubleMicroseconds(d1);
54 
55   // C-style casts
56   i = (int) absl::ToDoubleHours(d1);
57   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]
58   // CHECK-FIXES: absl::ToInt64Hours(d1);
59   x = (float) absl::ToInt64Microseconds(d1);
60   // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]
61   // CHECK-FIXES: absl::ToDoubleMicroseconds(d1);
62 
63   // Type aliasing
64   typedef int FancyInt;
65   typedef float FancyFloat;
66 
67   FancyInt j = static_cast<FancyInt>(absl::ToDoubleHours(d1));
68   // CHECK-MESSAGES: [[@LINE-1]]:16: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]
69   // CHECK-FIXES: absl::ToInt64Hours(d1);
70   FancyFloat k = static_cast<FancyFloat>(absl::ToInt64Microseconds(d1));
71   // CHECK-MESSAGES: [[@LINE-1]]:18: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]
72   // CHECK-FIXES: absl::ToDoubleMicroseconds(d1);
73 
74   // Macro handling
75   // We want to transform things in macro arguments
76 #define EXTERNAL(x) (x) + 5
77   i = EXTERNAL(static_cast<int>(absl::ToDoubleSeconds(d1)));
78   // CHECK-MESSAGES: [[@LINE-1]]:16: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]
79   // CHECK-FIXES: EXTERNAL(absl::ToInt64Seconds(d1));
80 #undef EXTERNAL
81 
82   // We don't want to transform this which get split across macro boundaries
83 #define SPLIT(x) static_cast<int>((x)) + 5
84   i = SPLIT(absl::ToDoubleSeconds(d1));
85 #undef SPLIT
86 
87   // We also don't want to transform things inside of a macro definition
88 #define INTERNAL(x) static_cast<int>(absl::ToDoubleSeconds((x))) + 5
89   i = INTERNAL(d1);
90 #undef INTERNAL
91 
92   // These shouldn't be converted
93   i = static_cast<int>(absl::ToInt64Seconds(d1));
94   i = static_cast<float>(absl::ToDoubleSeconds(d1));
95 }
96