| 1 | // RUN: %check_clang_tidy --match-partial-fixes %s abseil-duration-comparison %t -- -- -I%S/Inputs |
| 2 | |
| 3 | #include "absl/time/time.h" |
| 4 | |
| 5 | void f() { |
| 6 | double x; |
| 7 | absl::Duration d1, d2; |
| 8 | bool b; |
| 9 | absl::Time t1, t2; |
| 10 | |
| 11 | // Check against the RHS |
| 12 | b = x > absl::ToDoubleSeconds(d1); |
| 13 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 14 | // CHECK-FIXES: absl::Seconds(x) > d1; |
| 15 | b = x >= absl::ToDoubleSeconds(d1); |
| 16 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 17 | // CHECK-FIXES: absl::Seconds(x) >= d1; |
| 18 | b = x == absl::ToDoubleSeconds(d1); |
| 19 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 20 | // CHECK-FIXES: absl::Seconds(x) == d1; |
| 21 | b = x <= absl::ToDoubleSeconds(d1); |
| 22 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 23 | // CHECK-FIXES: absl::Seconds(x) <= d1; |
| 24 | b = x < absl::ToDoubleSeconds(d1); |
| 25 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 26 | // CHECK-FIXES: absl::Seconds(x) < d1; |
| 27 | b = x == absl::ToDoubleSeconds(t1 - t2); |
| 28 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 29 | // CHECK-FIXES: absl::Seconds(x) == t1 - t2; |
| 30 | b = absl::ToDoubleSeconds(d1) > absl::ToDoubleSeconds(d2); |
| 31 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 32 | // CHECK-FIXES: d1 > d2; |
| 33 | |
| 34 | // Check against the LHS |
| 35 | b = absl::ToDoubleSeconds(d1) < x; |
| 36 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 37 | // CHECK-FIXES: d1 < absl::Seconds(x); |
| 38 | b = absl::ToDoubleSeconds(d1) <= x; |
| 39 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 40 | // CHECK-FIXES: d1 <= absl::Seconds(x); |
| 41 | b = absl::ToDoubleSeconds(d1) == x; |
| 42 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 43 | // CHECK-FIXES: d1 == absl::Seconds(x); |
| 44 | b = absl::ToDoubleSeconds(d1) >= x; |
| 45 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 46 | // CHECK-FIXES: d1 >= absl::Seconds(x); |
| 47 | b = absl::ToDoubleSeconds(d1) > x; |
| 48 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 49 | // CHECK-FIXES: d1 > absl::Seconds(x); |
| 50 | |
| 51 | // Comparison against zero |
| 52 | b = absl::ToDoubleSeconds(d1) < 0.0; |
| 53 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 54 | // CHECK-FIXES: d1 < absl::ZeroDuration(); |
| 55 | b = absl::ToDoubleSeconds(d1) < 0; |
| 56 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 57 | // CHECK-FIXES: d1 < absl::ZeroDuration(); |
| 58 | |
| 59 | // Scales other than Seconds |
| 60 | b = x > absl::ToDoubleMicroseconds(d1); |
| 61 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 62 | // CHECK-FIXES: absl::Microseconds(x) > d1; |
| 63 | b = x >= absl::ToDoubleMilliseconds(d1); |
| 64 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 65 | // CHECK-FIXES: absl::Milliseconds(x) >= d1; |
| 66 | b = x == absl::ToDoubleNanoseconds(d1); |
| 67 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 68 | // CHECK-FIXES: absl::Nanoseconds(x) == d1; |
| 69 | b = x <= absl::ToDoubleMinutes(d1); |
| 70 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 71 | // CHECK-FIXES: absl::Minutes(x) <= d1; |
| 72 | b = x < absl::ToDoubleHours(d1); |
| 73 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 74 | // CHECK-FIXES: absl::Hours(x) < d1; |
| 75 | |
| 76 | // Integer comparisons |
| 77 | b = x > absl::ToInt64Microseconds(d1); |
| 78 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 79 | // CHECK-FIXES: absl::Microseconds(x) > d1; |
| 80 | b = x >= absl::ToInt64Milliseconds(d1); |
| 81 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 82 | // CHECK-FIXES: absl::Milliseconds(x) >= d1; |
| 83 | b = x == absl::ToInt64Nanoseconds(d1); |
| 84 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 85 | // CHECK-FIXES: absl::Nanoseconds(x) == d1; |
| 86 | b = x == absl::ToInt64Seconds(d1); |
| 87 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 88 | // CHECK-FIXES: absl::Seconds(x) == d1; |
| 89 | b = x <= absl::ToInt64Minutes(d1); |
| 90 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 91 | // CHECK-FIXES: absl::Minutes(x) <= d1; |
| 92 | b = x < absl::ToInt64Hours(d1); |
| 93 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 94 | // CHECK-FIXES: absl::Hours(x) < d1; |
| 95 | |
| 96 | // Other abseil-duration checks folded into this one |
| 97 | b = static_cast<double>(5) > absl::ToDoubleSeconds(d1); |
| 98 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 99 | // CHECK-FIXES: absl::Seconds(5) > d1; |
| 100 | b = double(5) > absl::ToDoubleSeconds(d1); |
| 101 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 102 | // CHECK-FIXES: absl::Seconds(5) > d1; |
| 103 | b = float(5) > absl::ToDoubleSeconds(d1); |
| 104 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 105 | // CHECK-FIXES: absl::Seconds(5) > d1; |
| 106 | b = ((double)5) > absl::ToDoubleSeconds(d1); |
| 107 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 108 | // CHECK-FIXES: absl::Seconds(5) > d1; |
| 109 | b = 5.0 > absl::ToDoubleSeconds(d1); |
| 110 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 111 | // CHECK-FIXES: absl::Seconds(5) > d1; |
| 112 | |
| 113 | // A long expression |
| 114 | bool some_condition; |
| 115 | int very_very_very_very_long_variable_name; |
| 116 | absl::Duration SomeDuration; |
| 117 | if (some_condition && very_very_very_very_long_variable_name |
| 118 | < absl::ToDoubleSeconds(SomeDuration)) { |
| 119 | // CHECK-MESSAGES: [[@LINE-2]]:25: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 120 | // CHECK-FIXES: if (some_condition && absl::Seconds(very_very_very_very_long_variable_name) < SomeDuration) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | // A complex expression |
| 125 | int y; |
| 126 | b = (y + 5) * 10 > absl::ToDoubleMilliseconds(d1); |
| 127 | // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 128 | // CHECK-FIXES: absl::Milliseconds((y + 5) * 10) > d1; |
| 129 | |
| 130 | // We should still transform the expression inside this macro invocation |
| 131 | #define VALUE_IF(v, e) v ? (e) : 0 |
| 132 | int a = VALUE_IF(1, 5 > absl::ToDoubleSeconds(d1)); |
| 133 | // CHECK-MESSAGES: [[@LINE-1]]:23: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 134 | // CHECK-FIXES: VALUE_IF(1, absl::Seconds(5) > d1); |
| 135 | #undef VALUE_IF |
| 136 | |
| 137 | #define VALUE_IF_2(e) (e) |
| 138 | #define VALUE_IF(v, e) v ? VALUE_IF_2(e) : VALUE_IF_2(0) |
| 139 | int a2 = VALUE_IF(1, 5 > absl::ToDoubleSeconds(d1)); |
| 140 | // CHECK-MESSAGES: [[@LINE-1]]:24: warning: perform comparison in the duration domain [abseil-duration-comparison] |
| 141 | // CHECK-FIXES: VALUE_IF(1, absl::Seconds(5) > d1); |
| 142 | #undef VALUE_IF |
| 143 | #undef VALUE_IF_2 |
| 144 | |
| 145 | #define VALUE_IF_2(e) (e) |
| 146 | #define VALUE_IF(v, e, type) (v ? VALUE_IF_2(absl::To##type##Seconds(e)) : 0) |
| 147 | int a3 = VALUE_IF(1, d1, Double); |
| 148 | #undef VALUE_IF |
| 149 | #undef VALUE_IF_2 |
| 150 | |
| 151 | #define VALUE_IF_2(e) (e) |
| 152 | #define VALUE_IF(v, e, type) (v ? (5 > VALUE_IF_2(absl::To##type##Seconds(e))) : 0) |
| 153 | int a4 = VALUE_IF(1, d1, Double); |
| 154 | #undef VALUE_IF |
| 155 | #undef VALUE_IF_2 |
| 156 | |
| 157 | // These should not match |
| 158 | b = 6 < 4; |
| 159 | |
| 160 | #define TODOUBLE(x) absl::ToDoubleSeconds(x) |
| 161 | b = 5.0 > TODOUBLE(d1); |
| 162 | #undef TODOUBLE |
| 163 | #define THIRTY 30.0 |
| 164 | b = THIRTY > absl::ToDoubleSeconds(d1); |
| 165 | #undef THIRTY |
| 166 | } |
| 167 | |