brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.3 KiB · 51d012f Raw
76 lines · cpp
1// RUN: %check_clang_tidy %s abseil-duration-division %t2 3namespace absl {4 5class Duration {};6 7int operator/(Duration lhs, Duration rhs);8 9double FDivDuration(Duration num, Duration den);10 11}  // namespace absl12 13void TakesDouble(double);14 15#define MACRO_EQ(x, y) (x == y)16#define MACRO_DIVEQ(x,y,z) (x/y == z)17#define CHECK(x) (x)18 19void Positives() {20  absl::Duration d;21 22  const double num_double = d/d;23  // CHECK-MESSAGES: [[@LINE-1]]:30: warning: operator/ on absl::Duration objects performs integer division; did you mean to use FDivDuration()? [abseil-duration-division]24  // CHECK-FIXES: const double num_double = absl::FDivDuration(d, d);25  const float num_float = d/d;26  // CHECK-MESSAGES: [[@LINE-1]]:28: warning: operator/ on absl::Duration objects27  // CHECK-FIXES: const float num_float = absl::FDivDuration(d, d);28  const auto SomeVal = 1.0 + d/d;29  // CHECK-MESSAGES: [[@LINE-1]]:31: warning: operator/ on absl::Duration objects30  // CHECK-FIXES: const auto SomeVal = 1.0 + absl::FDivDuration(d, d);31  if (MACRO_EQ(d/d, 0.0)) {}32  // CHECK-MESSAGES: [[@LINE-1]]:17: warning: operator/ on absl::Duration objects33  // CHECK-FIXES: if (MACRO_EQ(absl::FDivDuration(d, d), 0.0)) {}34  if (CHECK(MACRO_EQ(d/d, 0.0))) {}35  // CHECK-MESSAGES: [[@LINE-1]]:23: warning: operator/ on absl::Duration objects36  // CHECK-FIXES: if (CHECK(MACRO_EQ(absl::FDivDuration(d, d), 0.0))) {}37 38  // This one generates a message, but no fix.39  if (MACRO_DIVEQ(d, d, 0.0)) {}40  // CHECK-MESSAGES: [[@LINE-1]]:7: warning: operator/ on absl::Duration objects41  // CHECK-FIXES: if (MACRO_DIVEQ(d, d, 0.0)) {}42 43  TakesDouble(d/d);44  // CHECK-MESSAGES: [[@LINE-1]]:16: warning: operator/ on absl::Duration objects45  // CHECK-FIXES: TakesDouble(absl::FDivDuration(d, d));46}47 48void TakesInt(int);49template <class T>50void TakesGeneric(T);51 52void Negatives() {53  absl::Duration d;54  const int num_int = d/d;55  const long num_long = d/d;56  const short num_short = d/d;57  const char num_char = d/d;58  const auto num_auto = d/d;59  const auto SomeVal = 1 + d/d;60 61  TakesInt(d/d);62  TakesGeneric(d/d);63  // Explicit cast should disable the warning.64  const double num_cast1 = static_cast<double>(d/d);65  const double num_cast2 = (double)(d/d);66}67 68template <class T>69double DoubleDivision(T t1, T t2) {return t1/t2;}70 71//This also won't trigger a warning72void TemplateDivision() {73  absl::Duration d;74  DoubleDivision(d, d);75}76