diff options
author | Hyrum Wright <hwright@google.com> | 2018-12-13 19:23:52 +0000 |
---|---|---|
committer | Hyrum Wright <hwright@google.com> | 2018-12-13 19:23:52 +0000 |
commit | d7486196796bf8ce16c55f95b8c47bd5112ddd04 (patch) | |
tree | db9b1164bf8584af905502f9632e82e0a249296e /clang-tools-extra/test | |
parent | a8339fa59b225b07fd310bcc4f7a2e81278d04ad (diff) |
[clang-tidy] Add the abseil-duration-subtraction check
Summary:
This check uses the context of a subtraction expression as well as knowledge
about the Abseil Time types, to infer the type of the second operand of some
subtraction expressions in Duration conversions. For example:
absl::ToDoubleSeconds(duration) - foo
can become
absl::ToDoubleSeconds(duration - absl::Seconds(foo))
This ensures that time calculations are done in the proper domain, and also
makes it easier to further deduce the types of the second operands to these
expressions.
Reviewed By: JonasToth
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D55245
Diffstat (limited to 'clang-tools-extra/test')
-rw-r--r-- | clang-tools-extra/test/clang-tidy/abseil-duration-subtraction.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/abseil-duration-subtraction.cpp b/clang-tools-extra/test/clang-tidy/abseil-duration-subtraction.cpp new file mode 100644 index 00000000000..154b7d4ba76 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/abseil-duration-subtraction.cpp @@ -0,0 +1,64 @@ +// RUN: %check_clang_tidy %s abseil-duration-subtraction %t -- -- -I %S/Inputs + +#include "absl/time/time.h" + +void f() { + double x; + absl::Duration d, d1, d2; + + x = absl::ToDoubleSeconds(d) - 1.0; + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] + // CHECK-FIXES: absl::ToDoubleSeconds(d - absl::Seconds(1)) + x = absl::ToDoubleSeconds(d) - absl::ToDoubleSeconds(d1); + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] + // CHECK-FIXES: absl::ToDoubleSeconds(d - d1); + x = absl::ToDoubleSeconds(d) - 6.5 - 8.0; + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] + // CHECK-FIXES: absl::ToDoubleSeconds(d - absl::Seconds(6.5)) - 8.0; + x = absl::ToDoubleHours(d) - 1.0; + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] + // CHECK-FIXES: absl::ToDoubleHours(d - absl::Hours(1)) + x = absl::ToDoubleMinutes(d) - 1; + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] + // CHECK-FIXES: absl::ToDoubleMinutes(d - absl::Minutes(1)) + x = absl::ToDoubleMilliseconds(d) - 9; + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] + // CHECK-FIXES: absl::ToDoubleMilliseconds(d - absl::Milliseconds(9)) + x = absl::ToDoubleMicroseconds(d) - 9; + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] + // CHECK-FIXES: absl::ToDoubleMicroseconds(d - absl::Microseconds(9)) + x = absl::ToDoubleNanoseconds(d) - 42; + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] + // CHECK-FIXES: absl::ToDoubleNanoseconds(d - absl::Nanoseconds(42)) + + // We can rewrite the argument of the duration conversion +#define THIRTY absl::Seconds(30) + x = absl::ToDoubleSeconds(THIRTY) - 1.0; + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] + // CHECK-FIXES: absl::ToDoubleSeconds(THIRTY - absl::Seconds(1)) +#undef THIRTY + + // Some other contexts + if (absl::ToDoubleSeconds(d) - 1.0 > 10) {} + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] + // CHECK-FIXES: if (absl::ToDoubleSeconds(d - absl::Seconds(1)) > 10) {} + + // A nested occurance + x = absl::ToDoubleSeconds(d) - absl::ToDoubleSeconds(absl::Seconds(5)); + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] + // CHECK-FIXES: absl::ToDoubleSeconds(d - absl::Seconds(5)) + x = absl::ToDoubleSeconds(d) - absl::ToDoubleSeconds(absl::Seconds(absl::ToDoubleSeconds(d1))); + // CHECK-MESSAGES: [[@LINE-1]]:7: warning: perform subtraction in the duration domain [abseil-duration-subtraction] + // CHECK-FIXES: absl::ToDoubleSeconds(d - absl::Seconds(absl::ToDoubleSeconds(d1))) + + // These should not match + x = 5 - 6; + x = 4 - absl::ToDoubleSeconds(d) - 6.5 - 8.0; + x = absl::ToDoubleSeconds(d) + 1.0; + x = absl::ToDoubleSeconds(d) * 1.0; + x = absl::ToDoubleSeconds(d) / 1.0; + +#define MINUS_FIVE(z) absl::ToDoubleSeconds(z) - 5 + x = MINUS_FIVE(d); +#undef MINUS_FIVE +} |