| 1 |
// RUN: %check_clang_tidy %s abseil-duration-subtraction %t -- -- -I %S/Inputs |
| 2 |
|
| 3 |
#include "absl/time/time.h" |
| 4 |
|
| 5 |
void f() { |
| 6 |
double x; |
| 7 |
absl::Duration d, d1, d2; |
| 8 |
|
| 9 |
x = absl::ToDoubleSeconds(d) - 1.0; |
| 10 |
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] |
| 11 |
// CHECK-FIXES: absl::ToDoubleSeconds(d - absl::Seconds(1)) |
| 12 |
x = absl::ToDoubleSeconds(d) - absl::ToDoubleSeconds(d1); |
| 13 |
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] |
| 14 |
// CHECK-FIXES: absl::ToDoubleSeconds(d - d1); |
| 15 |
x = absl::ToDoubleSeconds(d) - 6.5 - 8.0; |
| 16 |
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] |
| 17 |
// CHECK-FIXES: absl::ToDoubleSeconds(d - absl::Seconds(6.5)) - 8.0; |
| 18 |
x = absl::ToDoubleHours(d) - 1.0; |
| 19 |
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] |
| 20 |
// CHECK-FIXES: absl::ToDoubleHours(d - absl::Hours(1)) |
| 21 |
x = absl::ToDoubleMinutes(d) - 1; |
| 22 |
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] |
| 23 |
// CHECK-FIXES: absl::ToDoubleMinutes(d - absl::Minutes(1)) |
| 24 |
x = absl::ToDoubleMilliseconds(d) - 9; |
| 25 |
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] |
| 26 |
// CHECK-FIXES: absl::ToDoubleMilliseconds(d - absl::Milliseconds(9)) |
| 27 |
x = absl::ToDoubleMicroseconds(d) - 9; |
| 28 |
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] |
| 29 |
// CHECK-FIXES: absl::ToDoubleMicroseconds(d - absl::Microseconds(9)) |
| 30 |
x = absl::ToDoubleNanoseconds(d) - 42; |
| 31 |
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] |
| 32 |
// CHECK-FIXES: absl::ToDoubleNanoseconds(d - absl::Nanoseconds(42)) |
| 33 |
|
| 34 |
// We can rewrite the argument of the duration conversion |
| 35 |
#define THIRTY absl::Seconds(30) |
| 36 |
x = absl::ToDoubleSeconds(THIRTY) - 1.0; |
| 37 |
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] |
| 38 |
// CHECK-FIXES: absl::ToDoubleSeconds(THIRTY - absl::Seconds(1)) |
| 39 |
#undef THIRTY |
| 40 |
|
| 41 |
// Some other contexts |
| 42 |
if (absl::ToDoubleSeconds(d) - 1.0 > 10) {} |
| 43 |
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] |
| 44 |
// CHECK-FIXES: if (absl::ToDoubleSeconds(d - absl::Seconds(1)) > 10) {} |
| 45 |
|
| 46 |
// A nested occurrence |
| 47 |
x = absl::ToDoubleSeconds(d) - absl::ToDoubleSeconds(absl::Seconds(5)); |
| 48 |
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] |
| 49 |
// CHECK-FIXES: absl::ToDoubleSeconds(d - absl::Seconds(5)) |
| 50 |
x = absl::ToDoubleSeconds(d) - absl::ToDoubleSeconds(absl::Seconds(absl::ToDoubleSeconds(d1))); |
| 51 |
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] |
| 52 |
// CHECK-FIXES: absl::ToDoubleSeconds(d - absl::Seconds(absl::ToDoubleSeconds(d1))) |
| 53 |
|
| 54 |
// These should not match |
| 55 |
x = 5 - 6; |
| 56 |
x = 4 - absl::ToDoubleSeconds(d) - 6.5 - 8.0; |
| 57 |
x = absl::ToDoubleSeconds(d) + 1.0; |
| 58 |
x = absl::ToDoubleSeconds(d) * 1.0; |
| 59 |
x = absl::ToDoubleSeconds(d) / 1.0; |
| 60 |
|
| 61 |
#define MINUS_FIVE(z) absl::ToDoubleSeconds(z) - 5 |
| 62 |
x = MINUS_FIVE(d); |
| 63 |
#undef MINUS_FIVE |
| 64 |
} |