|
| 1 | +// RUN: %check_clang_tidy %s abseil-duration-unnecessary-conversion %t -- -- -I%S/Inputs |
| 2 | + |
| 3 | +#include "absl/time/time.h" |
| 4 | + |
| 5 | +void 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: d1 |
| 47 | + |
| 48 | + // As macro argument |
| 49 | +#define PLUS_FIVE_S(x) x + absl::Seconds(5) |
| 50 | + d2 = PLUS_FIVE_S(absl::Seconds(absl::ToInt64Seconds(d1))); |
| 51 | + // CHECK-MESSAGES: [[@LINE-1]]:20: warning: remove unnecessary absl::Duration conversions [abseil-duration-unnecessary-conversion] |
| 52 | + // CHECK-FIXES: PLUS_FIVE_S(d1) |
| 53 | +#undef PLUS_FIVE_S |
| 54 | + |
| 55 | + // Split by macro: should not change |
| 56 | +#define TOSECONDS(x) absl::Seconds(x) |
| 57 | + d2 = TOSECONDS(absl::ToInt64Seconds(d1)); |
| 58 | +#undef TOSECONDS |
| 59 | + |
| 60 | + // Don't change something inside a macro definition |
| 61 | +#define VALUE(x) absl::Hours(absl::ToInt64Hours(x)); |
| 62 | + d2 = VALUE(d1); |
| 63 | +#undef VALUE |
| 64 | + |
| 65 | + // These should not match |
| 66 | + d2 = absl::Seconds(absl::ToDoubleMilliseconds(d1)); |
| 67 | + d2 = absl::Seconds(4); |
| 68 | + int i = absl::ToInt64Milliseconds(d1); |
| 69 | +} |
0 commit comments