1// RUN: %check_clang_tidy -std=c++11-or-later %s abseil-duration-unnecessary-conversion %t -- -- -I %S/Inputs
2
3#include "absl/time/time.h"
4
5void f() {
6 absl::Duration d1, d2;
7
8 // Floating point
9 d2 = absl::Hours(absl::ToDoubleHours(d1));
10 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
11 // CHECK-FIXES: d2 = d1
12 d2 = absl::Minutes(absl::ToDoubleMinutes(d1));
13 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
14 // CHECK-FIXES: d2 = d1
15 d2 = absl::Seconds(absl::ToDoubleSeconds(d1));
16 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
17 // CHECK-FIXES: d2 = d1
18 d2 = absl::Milliseconds(absl::ToDoubleMilliseconds(d1));
19 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
20 // CHECK-FIXES: d2 = d1
21 d2 = absl::Microseconds(absl::ToDoubleMicroseconds(d1));
22 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
23 // CHECK-FIXES: d2 = d1
24 d2 = absl::Nanoseconds(absl::ToDoubleNanoseconds(d1));
25 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
26 // CHECK-FIXES: d2 = d1
27
28 // Integer point
29 d2 = absl::Hours(absl::ToInt64Hours(d1));
30 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
31 // CHECK-FIXES: d2 = d1
32 d2 = absl::Minutes(absl::ToInt64Minutes(d1));
33 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
34 // CHECK-FIXES: d2 = d1
35 d2 = absl::Seconds(absl::ToInt64Seconds(d1));
36 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
37 // CHECK-FIXES: d2 = d1
38 d2 = absl::Milliseconds(absl::ToInt64Milliseconds(d1));
39 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
40 // CHECK-FIXES: d2 = d1
41 d2 = absl::Microseconds(absl::ToInt64Microseconds(d1));
42 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
43 // CHECK-FIXES: d2 = d1
44 d2 = absl::Nanoseconds(absl::ToInt64Nanoseconds(d1));
45 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
46 // CHECK-FIXES: d2 = d1
47
48 d2 = absl::Hours(d1 / absl::Hours(1));
49 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
50 // CHECK-FIXES: d2 = d1
51 d2 = absl::Minutes(d1 / absl::Minutes(1));
52 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
53 // CHECK-FIXES: d2 = d1
54 d2 = absl::Seconds(d1 / absl::Seconds(1));
55 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
56 // CHECK-FIXES: d2 = d1
57 d2 = absl::Milliseconds(d1 / absl::Milliseconds(1));
58 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
59 // CHECK-FIXES: d2 = d1
60 d2 = absl::Microseconds(d1 / absl::Microseconds(1));
61 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
62 // CHECK-FIXES: d2 = d1
63 d2 = absl::Nanoseconds(d1 / absl::Nanoseconds(1));
64 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
65 // CHECK-FIXES: d2 = d1
66
67 d2 = absl::Hours(absl::FDivDuration(d1, absl::Hours(1)));
68 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
69 // CHECK-FIXES: d2 = d1
70 d2 = absl::Minutes(absl::FDivDuration(d1, absl::Minutes(1)));
71 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
72 // CHECK-FIXES: d2 = d1
73 d2 = absl::Seconds(absl::FDivDuration(d1, absl::Seconds(1)));
74 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
75 // CHECK-FIXES: d2 = d1
76 d2 = absl::Milliseconds(absl::FDivDuration(d1, absl::Milliseconds(1)));
77 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
78 // CHECK-FIXES: d2 = d1
79 d2 = absl::Microseconds(absl::FDivDuration(d1, absl::Microseconds(1)));
80 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
81 // CHECK-FIXES: d2 = d1
82 d2 = absl::Nanoseconds(absl::FDivDuration(d1, absl::Nanoseconds(1)));
83 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
84 // CHECK-FIXES: d2 = d1
85
86 // As macro argument
87#define PLUS_FIVE_S(x) x + absl::Seconds(5)
88 d2 = PLUS_FIVE_S(absl::Seconds(absl::ToInt64Seconds(d1)));
89 // CHECK-MESSAGES: [[@LINE-1]]:20: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
90 // CHECK-FIXES: PLUS_FIVE_S(d1)
91#undef PLUS_FIVE_S
92
93 // Split by macro: should not change
94#define TOSECONDS(x) absl::Seconds(x)
95 d2 = TOSECONDS(absl::ToInt64Seconds(d1));
96#undef TOSECONDS
97
98 // Don't change something inside a macro definition
99#define VALUE(x) absl::Hours(absl::ToInt64Hours(x));
100 d2 = VALUE(d1);
101#undef VALUE
102
103 // Multiplication
104 d2 = absl::Nanoseconds(absl::ToDoubleNanoseconds(d1) * 2);
105 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
106 // CHECK-FIXES: d2 = d1 * 2
107 d2 = absl::Microseconds(absl::ToInt64Microseconds(d1) * 2);
108 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
109 // CHECK-FIXES: d2 = d1 * 2
110 d2 = absl::Milliseconds(absl::ToDoubleMilliseconds(d1) * 2);
111 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
112 // CHECK-FIXES: d2 = d1 * 2
113 d2 = absl::Seconds(absl::ToInt64Seconds(d1) * 2);
114 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
115 // CHECK-FIXES: d2 = d1 * 2
116 d2 = absl::Minutes(absl::ToDoubleMinutes(d1) * 2);
117 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
118 // CHECK-FIXES: d2 = d1 * 2
119 d2 = absl::Hours(absl::ToInt64Hours(d1) * 2);
120 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
121 // CHECK-FIXES: d2 = d1 * 2
122 d2 = absl::Nanoseconds(2 * absl::ToDoubleNanoseconds(d1));
123 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
124 // CHECK-FIXES: d2 = 2 * d1
125 d2 = absl::Microseconds(2 * absl::ToInt64Microseconds(d1));
126 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
127 // CHECK-FIXES: d2 = 2 * d1
128 d2 = absl::Milliseconds(2 * absl::ToDoubleMilliseconds(d1));
129 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
130 // CHECK-FIXES: d2 = 2 * d1
131 d2 = absl::Seconds(2 * absl::ToInt64Seconds(d1));
132 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
133 // CHECK-FIXES: d2 = 2 * d1
134 d2 = absl::Minutes(2 * absl::ToDoubleMinutes(d1));
135 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
136 // CHECK-FIXES: d2 = 2 * d1
137 d2 = absl::Hours(2 * absl::ToInt64Hours(d1));
138 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion]
139 // CHECK-FIXES: d2 = 2 * d1
140
141 // These should not match
142 d2 = absl::Seconds(absl::ToDoubleMilliseconds(d1));
143 d2 = absl::Seconds(4);
144 int i = absl::ToInt64Milliseconds(d1);
145 d2 = absl::Hours(d1 / absl::Minutes(1));
146 d2 = absl::Seconds(d1 / absl::Seconds(30));
147 d2 = absl::Hours(absl::FDivDuration(d1, absl::Minutes(1)));
148 d2 = absl::Milliseconds(absl::FDivDuration(d1, absl::Milliseconds(20)));
149 d2 = absl::Seconds(absl::ToInt64Milliseconds(d1) * 2);
150 d2 = absl::Milliseconds(absl::ToDoubleSeconds(d1) * 2);
151}
152

source code of clang-tools-extra/test/clang-tidy/checkers/abseil/duration-unnecessary-conversion.cpp