Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %check_clang_tidy --match-partial-fixes %s abseil-duration-addition %t -- -- -I%S/Inputs
// RUN: %check_clang_tidy %s abseil-duration-addition %t -- -- -I%S/Inputs

#include "absl/time/time.h"

Expand All @@ -8,68 +8,68 @@ void f() {

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))
// CHECK-FIXES: i = 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))
// CHECK-FIXES: i = 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))
// CHECK-FIXES: i = 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))
// CHECK-FIXES: i = 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))
// CHECK-FIXES: i = 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))
// CHECK-FIXES: i = 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)
// CHECK-FIXES: i = 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)
// CHECK-FIXES: i = 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)
// CHECK-FIXES: i = 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)
// CHECK-FIXES: i = 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)
// CHECK-FIXES: i = 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)
// CHECK-FIXES: i = 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))
// CHECK-FIXES: i = 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)
// CHECK-FIXES: i = 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))
// CHECK-FIXES: i = 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))
// CHECK-FIXES: i = 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))
// CHECK-FIXES: if (absl::ToUnixSeconds(t + absl::Seconds(1)) > 10) {}

// These should not match
i = 5 + 6;
Expand All @@ -88,7 +88,7 @@ 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{}))
// CHECK-FIXES: int i = absl::ToUnixNanos(t + absl::Nanoseconds(T{}));
}

void g() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %check_clang_tidy --match-partial-fixes %s abseil-duration-comparison %t -- -- -I%S/Inputs
// RUN: %check_clang_tidy %s abseil-duration-comparison %t -- -- -I%S/Inputs

#include "absl/time/time.h"

Expand All @@ -11,104 +11,104 @@ void f() {
// Check against the RHS
b = x > absl::ToDoubleSeconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Seconds(x) > d1;
// CHECK-FIXES: b = absl::Seconds(x) > d1;
b = x >= absl::ToDoubleSeconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Seconds(x) >= d1;
// CHECK-FIXES: b = absl::Seconds(x) >= d1;
b = x == absl::ToDoubleSeconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Seconds(x) == d1;
// CHECK-FIXES: b = absl::Seconds(x) == d1;
b = x <= absl::ToDoubleSeconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Seconds(x) <= d1;
// CHECK-FIXES: b = absl::Seconds(x) <= d1;
b = x < absl::ToDoubleSeconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Seconds(x) < d1;
// CHECK-FIXES: b = absl::Seconds(x) < d1;
b = x == absl::ToDoubleSeconds(t1 - t2);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Seconds(x) == t1 - t2;
// CHECK-FIXES: b = absl::Seconds(x) == t1 - t2;
b = absl::ToDoubleSeconds(d1) > absl::ToDoubleSeconds(d2);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: d1 > d2;
// CHECK-FIXES: b = d1 > d2;

// Check against the LHS
b = absl::ToDoubleSeconds(d1) < x;
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: d1 < absl::Seconds(x);
// CHECK-FIXES: b = d1 < absl::Seconds(x);
b = absl::ToDoubleSeconds(d1) <= x;
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: d1 <= absl::Seconds(x);
// CHECK-FIXES: b = d1 <= absl::Seconds(x);
b = absl::ToDoubleSeconds(d1) == x;
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: d1 == absl::Seconds(x);
// CHECK-FIXES: b = d1 == absl::Seconds(x);
b = absl::ToDoubleSeconds(d1) >= x;
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: d1 >= absl::Seconds(x);
// CHECK-FIXES: b = d1 >= absl::Seconds(x);
b = absl::ToDoubleSeconds(d1) > x;
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: d1 > absl::Seconds(x);
// CHECK-FIXES: b = d1 > absl::Seconds(x);

// Comparison against zero
b = absl::ToDoubleSeconds(d1) < 0.0;
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: d1 < absl::ZeroDuration();
// CHECK-FIXES: b = d1 < absl::ZeroDuration();
b = absl::ToDoubleSeconds(d1) < 0;
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: d1 < absl::ZeroDuration();
// CHECK-FIXES: b = d1 < absl::ZeroDuration();

// Scales other than Seconds
b = x > absl::ToDoubleMicroseconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Microseconds(x) > d1;
// CHECK-FIXES: b = absl::Microseconds(x) > d1;
b = x >= absl::ToDoubleMilliseconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Milliseconds(x) >= d1;
// CHECK-FIXES: b = absl::Milliseconds(x) >= d1;
b = x == absl::ToDoubleNanoseconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Nanoseconds(x) == d1;
// CHECK-FIXES: b = absl::Nanoseconds(x) == d1;
b = x <= absl::ToDoubleMinutes(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Minutes(x) <= d1;
// CHECK-FIXES: b = absl::Minutes(x) <= d1;
b = x < absl::ToDoubleHours(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Hours(x) < d1;
// CHECK-FIXES: b = absl::Hours(x) < d1;

// Integer comparisons
b = x > absl::ToInt64Microseconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Microseconds(x) > d1;
// CHECK-FIXES: b = absl::Microseconds(x) > d1;
b = x >= absl::ToInt64Milliseconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Milliseconds(x) >= d1;
// CHECK-FIXES: b = absl::Milliseconds(x) >= d1;
b = x == absl::ToInt64Nanoseconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Nanoseconds(x) == d1;
// CHECK-FIXES: b = absl::Nanoseconds(x) == d1;
b = x == absl::ToInt64Seconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Seconds(x) == d1;
// CHECK-FIXES: b = absl::Seconds(x) == d1;
b = x <= absl::ToInt64Minutes(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Minutes(x) <= d1;
// CHECK-FIXES: b = absl::Minutes(x) <= d1;
b = x < absl::ToInt64Hours(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Hours(x) < d1;
// CHECK-FIXES: b = absl::Hours(x) < d1;

// Other abseil-duration checks folded into this one
b = static_cast<double>(5) > absl::ToDoubleSeconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Seconds(5) > d1;
// CHECK-FIXES: b = absl::Seconds(5) > d1;
b = double(5) > absl::ToDoubleSeconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Seconds(5) > d1;
// CHECK-FIXES: b = absl::Seconds(5) > d1;
b = float(5) > absl::ToDoubleSeconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Seconds(5) > d1;
// CHECK-FIXES: b = absl::Seconds(5) > d1;
b = ((double)5) > absl::ToDoubleSeconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Seconds(5) > d1;
// CHECK-FIXES: b = absl::Seconds(5) > d1;
b = 5.0 > absl::ToDoubleSeconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Seconds(5) > d1;
// CHECK-FIXES: b = absl::Seconds(5) > d1;

// A long expression
bool some_condition;
Expand All @@ -125,20 +125,20 @@ void f() {
int y;
b = (y + 5) * 10 > absl::ToDoubleMilliseconds(d1);
// CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: absl::Milliseconds((y + 5) * 10) > d1;
// CHECK-FIXES: b = absl::Milliseconds((y + 5) * 10) > d1;

// We should still transform the expression inside this macro invocation
#define VALUE_IF(v, e) v ? (e) : 0
int a = VALUE_IF(1, 5 > absl::ToDoubleSeconds(d1));
// CHECK-MESSAGES: [[@LINE-1]]:23: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: VALUE_IF(1, absl::Seconds(5) > d1);
// CHECK-FIXES: int a = VALUE_IF(1, absl::Seconds(5) > d1);
#undef VALUE_IF

#define VALUE_IF_2(e) (e)
#define VALUE_IF(v, e) v ? VALUE_IF_2(e) : VALUE_IF_2(0)
int a2 = VALUE_IF(1, 5 > absl::ToDoubleSeconds(d1));
// CHECK-MESSAGES: [[@LINE-1]]:24: warning: perform comparison in the duration domain [abseil-duration-comparison]
// CHECK-FIXES: VALUE_IF(1, absl::Seconds(5) > d1);
// CHECK-FIXES: int a2 = VALUE_IF(1, absl::Seconds(5) > d1);
#undef VALUE_IF
#undef VALUE_IF_2

Expand Down
Loading