abseil-duration-addition.cpp
4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// RUN: %check_clang_tidy %s abseil-duration-addition %t -- -- -I%S/Inputs
#include "absl/time/time.h"
void f() {
absl::Time t;
int i;
i = absl::ToUnixHours(t) + 5;
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixHours(t + absl::Hours(5))
i = absl::ToUnixMinutes(t) + 5;
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixMinutes(t + absl::Minutes(5))
i = absl::ToUnixSeconds(t) + 5;
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixSeconds(t + absl::Seconds(5))
i = absl::ToUnixMillis(t) + 5;
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixMillis(t + absl::Milliseconds(5))
i = absl::ToUnixMicros(t) + 5;
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixMicros(t + absl::Microseconds(5))
i = absl::ToUnixNanos(t) + 5;
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixNanos(t + absl::Nanoseconds(5))
i = 3 + absl::ToUnixHours(t);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixHours(absl::Hours(3) + t)
i = 3 + absl::ToUnixMinutes(t);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixMinutes(absl::Minutes(3) + t)
i = 3 + absl::ToUnixSeconds(t);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixSeconds(absl::Seconds(3) + t)
i = 3 + absl::ToUnixMillis(t);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixMillis(absl::Milliseconds(3) + t)
i = 3 + absl::ToUnixMicros(t);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixMicros(absl::Microseconds(3) + t)
i = 3 + absl::ToUnixNanos(t);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixNanos(absl::Nanoseconds(3) + t)
// Undoing inverse conversions
i = absl::ToUnixMicros(t) + absl::ToInt64Microseconds(absl::Seconds(1));
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixMicros(t + absl::Seconds(1))
// Parens
i = 3 + (absl::ToUnixHours(t));
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixHours(absl::Hours(3) + t)
// Float folding
i = absl::ToUnixSeconds(t) + 5.0;
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixSeconds(t + absl::Seconds(5))
// We can rewrite the argument of the duration conversion
#define THIRTY absl::FromUnixSeconds(30)
i = absl::ToUnixSeconds(THIRTY) + 1;
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixSeconds(THIRTY + absl::Seconds(1))
#undef THIRTY
// Some other contexts
if (absl::ToUnixSeconds(t) + 1.0 > 10) {}
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixSeconds(t + absl::Seconds(1))
// These should not match
i = 5 + 6;
i = absl::ToUnixSeconds(t) - 1.0;
i = absl::ToUnixSeconds(t) * 1.0;
i = absl::ToUnixSeconds(t) / 1.0;
i += absl::ToInt64Microseconds(absl::Seconds(1));
#define PLUS_FIVE(z) absl::ToUnixSeconds(z) + 5
i = PLUS_FIVE(t);
#undef PLUS_FIVE
}
// Within a templated function
template<typename T>
void foo(absl::Time t) {
int i = absl::ToUnixNanos(t) + T{};
// CHECK-MESSAGES: [[@LINE-1]]:11: warning: perform addition in the duration domain [abseil-duration-addition]
// CHECK-FIXES: absl::ToUnixNanos(t + absl::Nanoseconds(T{}))
}
void g() {
absl::Time t;
foo<int>(t);
foo<double>(t);
}