1 // RUN: %check_clang_tidy %s abseil-duration-factory-scale %t -- -- -I%S/Inputs
2 
3 #include "absl/time/time.h"
4 
5 namespace std { typedef long long int64_t; }
6 using int64_t = std::int64_t;
7 
ScaleTest()8 void ScaleTest() {
9   absl::Duration d;
10 
11   // Zeroes
12   d = absl::Hours(0);
13   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use ZeroDuration() for zero-length time intervals [abseil-duration-factory-scale]
14   // CHECK-FIXES: absl::ZeroDuration();
15   d = absl::Minutes(0);
16   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use ZeroDuration() for zero-length time intervals [abseil-duration-factory-scale]
17   // CHECK-FIXES: absl::ZeroDuration();
18   d = absl::Seconds(0);
19   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use ZeroDuration() for zero-length time intervals [abseil-duration-factory-scale]
20   // CHECK-FIXES: absl::ZeroDuration();
21   d = absl::Milliseconds(0);
22   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use ZeroDuration() for zero-length time intervals [abseil-duration-factory-scale]
23   // CHECK-FIXES: absl::ZeroDuration();
24   d = absl::Microseconds(0);
25   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use ZeroDuration() for zero-length time intervals [abseil-duration-factory-scale]
26   // CHECK-FIXES: absl::ZeroDuration();
27   d = absl::Nanoseconds(0);
28   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use ZeroDuration() for zero-length time intervals [abseil-duration-factory-scale]
29   // CHECK-FIXES: absl::ZeroDuration();
30   d = absl::Seconds(0.0);
31   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use ZeroDuration() for zero-length time intervals [abseil-duration-factory-scale]
32   // CHECK-FIXES: absl::ZeroDuration();
33   d = absl::Seconds(0x0.000001p-126f);
34   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use ZeroDuration() for zero-length time intervals [abseil-duration-factory-scale]
35   // CHECK-FIXES: absl::ZeroDuration();
36   d = absl::Seconds(int{0});
37   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use ZeroDuration() for zero-length time intervals [abseil-duration-factory-scale]
38   // CHECK-FIXES: absl::ZeroDuration();
39   d = absl::Seconds(int64_t{0});
40   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use ZeroDuration() for zero-length time intervals [abseil-duration-factory-scale]
41   // CHECK-FIXES: absl::ZeroDuration();
42   d = absl::Seconds(float{0});
43   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use ZeroDuration() for zero-length time intervals [abseil-duration-factory-scale]
44   // CHECK-FIXES: absl::ZeroDuration();
45 
46   // Fold seconds into minutes
47   d = absl::Seconds(30 * 60);
48   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: internal duration scaling can be removed [abseil-duration-factory-scale]
49   // CHECK-FIXES: absl::Minutes(30);
50   d = absl::Seconds(60 * 30);
51   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: internal duration scaling can be removed [abseil-duration-factory-scale]
52   // CHECK-FIXES: absl::Minutes(30);
53 
54   // Try a few more exotic multiplications
55   d = absl::Seconds(60 * 30 * 60);
56   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: internal duration scaling can be removed [abseil-duration-factory-scale]
57   // CHECK-FIXES: absl::Minutes(60 * 30);
58   d = absl::Seconds(1e-3 * 30);
59   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: internal duration scaling can be removed [abseil-duration-factory-scale]
60   // CHECK-FIXES: absl::Milliseconds(30);
61   d = absl::Milliseconds(30 * 1000);
62   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: internal duration scaling can be removed [abseil-duration-factory-scale]
63   // CHECK-FIXES: absl::Seconds(30);
64   d = absl::Milliseconds(30 * 0.001);
65   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: internal duration scaling can be removed [abseil-duration-factory-scale]
66   // CHECK-FIXES: absl::Microseconds(30);
67 
68   // Multiple steps
69   d = absl::Seconds(5 * 3600);
70   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: internal duration scaling can be removed [abseil-duration-factory-scale]
71   // CHECK-FIXES: absl::Hours(5);
72   d = absl::Microseconds(5 * 1e6);
73   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: internal duration scaling can be removed [abseil-duration-factory-scale]
74   // CHECK-FIXES: absl::Seconds(5);
75   d = absl::Seconds(5 * 1e-6);
76   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: internal duration scaling can be removed [abseil-duration-factory-scale]
77   // CHECK-FIXES: absl::Microseconds(5);
78   d = absl::Microseconds(5 * 1000000);
79   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: internal duration scaling can be removed [abseil-duration-factory-scale]
80   // CHECK-FIXES: absl::Seconds(5);
81 
82   // Division
83   d = absl::Hours(30 / 60.);
84   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: internal duration scaling can be removed [abseil-duration-factory-scale]
85   // CHECK-FIXES: absl::Minutes(30);
86   d = absl::Seconds(30 / 1000.);
87   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: internal duration scaling can be removed [abseil-duration-factory-scale]
88   // CHECK-FIXES: absl::Milliseconds(30);
89   d = absl::Milliseconds(30 / 1e3);
90   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: internal duration scaling can be removed [abseil-duration-factory-scale]
91   // CHECK-FIXES: absl::Microseconds(30);
92   d = absl::Seconds(30 / 1e6);
93   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: internal duration scaling can be removed [abseil-duration-factory-scale]
94   // CHECK-FIXES: absl::Microseconds(30);
95 
96   // None of these should trigger the check
97   d = absl::Seconds(60);
98   d = absl::Seconds(int{60});
99   d = absl::Seconds(float{60});
100   d = absl::Seconds(60 + 30);
101   d = absl::Seconds(60 - 30);
102   d = absl::Seconds(50 * 30);
103   d = absl::Hours(60 * 60);
104   d = absl::Nanoseconds(1e-3 * 30);
105   d = absl::Seconds(1000 / 30);
106   // We don't support division by integers, which could cause rounding
107   d = absl::Seconds(10 / 1000);
108   d = absl::Seconds(30 / 50);
109 
110 #define EXPRESSION 30 * 60
111   d = absl::Seconds(EXPRESSION);
112 #undef EXPRESSION
113 
114 // This should not trigger
115 #define HOURS(x) absl::Minutes(60 * x)
116   d = HOURS(40);
117 #undef HOURS
118 }
119