1 // RUN: %check_clang_tidy %s abseil-duration-division %t
2
3 namespace absl {
4
5 class Duration {};
6
7 int operator/(Duration lhs, Duration rhs);
8
9 double FDivDuration(Duration num, Duration den);
10
11 } // namespace absl
12
13 void TakesDouble(double);
14
15 #define MACRO_EQ(x, y) (x == y)
16 #define MACRO_DIVEQ(x,y,z) (x/y == z)
17 #define CHECK(x) (x)
18
Positives()19 void Positives() {
20 absl::Duration d;
21
22 const double num_double = d/d;
23 // CHECK-MESSAGES: [[@LINE-1]]:30: warning: operator/ on absl::Duration objects performs integer division; did you mean to use FDivDuration()? [abseil-duration-division]
24 // CHECK-FIXES: const double num_double = absl::FDivDuration(d, d);
25 const float num_float = d/d;
26 // CHECK-MESSAGES: [[@LINE-1]]:28: warning: operator/ on absl::Duration objects
27 // CHECK-FIXES: const float num_float = absl::FDivDuration(d, d);
28 const auto SomeVal = 1.0 + d/d;
29 // CHECK-MESSAGES: [[@LINE-1]]:31: warning: operator/ on absl::Duration objects
30 // CHECK-FIXES: const auto SomeVal = 1.0 + absl::FDivDuration(d, d);
31 if (MACRO_EQ(d/d, 0.0)) {}
32 // CHECK-MESSAGES: [[@LINE-1]]:17: warning: operator/ on absl::Duration objects
33 // CHECK-FIXES: if (MACRO_EQ(absl::FDivDuration(d, d), 0.0)) {}
34 if (CHECK(MACRO_EQ(d/d, 0.0))) {}
35 // CHECK-MESSAGES: [[@LINE-1]]:23: warning: operator/ on absl::Duration objects
36 // CHECK-FIXES: if (CHECK(MACRO_EQ(absl::FDivDuration(d, d), 0.0))) {}
37
38 // This one generates a message, but no fix.
39 if (MACRO_DIVEQ(d, d, 0.0)) {}
40 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: operator/ on absl::Duration objects
41 // CHECK-FIXES: if (MACRO_DIVEQ(d, d, 0.0)) {}
42
43 TakesDouble(d/d);
44 // CHECK-MESSAGES: [[@LINE-1]]:16: warning: operator/ on absl::Duration objects
45 // CHECK-FIXES: TakesDouble(absl::FDivDuration(d, d));
46 }
47
48 void TakesInt(int);
49 template <class T>
50 void TakesGeneric(T);
51
Negatives()52 void Negatives() {
53 absl::Duration d;
54 const int num_int = d/d;
55 const long num_long = d/d;
56 const short num_short = d/d;
57 const char num_char = d/d;
58 const auto num_auto = d/d;
59 const auto SomeVal = 1 + d/d;
60
61 TakesInt(d/d);
62 TakesGeneric(d/d);
63 // Explicit cast should disable the warning.
64 const double num_cast1 = static_cast<double>(d/d);
65 const double num_cast2 = (double)(d/d);
66 }
67
68 template <class T>
DoubleDivision(T t1,T t2)69 double DoubleDivision(T t1, T t2) {return t1/t2;}
70
71 //This also won't trigger a warning
TemplateDivision()72 void TemplateDivision() {
73 absl::Duration d;
74 DoubleDivision(d, d);
75 }
76