96 lines · cpp
1// RUN: %check_clang_tidy %s abseil-duration-conversion-cast %t -- -- -I%S/Inputs2 3#include "absl/time/time.h"4 5void f() {6 absl::Duration d1;7 double x;8 int i;9 10 i = static_cast<int>(absl::ToDoubleHours(d1));11 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]12 // CHECK-FIXES: i = absl::ToInt64Hours(d1);13 x = static_cast<float>(absl::ToInt64Hours(d1));14 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]15 // CHECK-FIXES: x = absl::ToDoubleHours(d1);16 i = static_cast<int>(absl::ToDoubleMinutes(d1));17 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]18 // CHECK-FIXES: i = absl::ToInt64Minutes(d1);19 x = static_cast<float>(absl::ToInt64Minutes(d1));20 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]21 // CHECK-FIXES: x = absl::ToDoubleMinutes(d1);22 i = static_cast<int>(absl::ToDoubleSeconds(d1));23 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]24 // CHECK-FIXES: i = absl::ToInt64Seconds(d1);25 x = static_cast<float>(absl::ToInt64Seconds(d1));26 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]27 // CHECK-FIXES: x = absl::ToDoubleSeconds(d1);28 i = static_cast<int>(absl::ToDoubleMilliseconds(d1));29 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]30 // CHECK-FIXES: i = absl::ToInt64Milliseconds(d1);31 x = static_cast<float>(absl::ToInt64Milliseconds(d1));32 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]33 // CHECK-FIXES: x = absl::ToDoubleMilliseconds(d1);34 i = static_cast<int>(absl::ToDoubleMicroseconds(d1));35 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]36 // CHECK-FIXES: i = absl::ToInt64Microseconds(d1);37 x = static_cast<float>(absl::ToInt64Microseconds(d1));38 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]39 // CHECK-FIXES: x = absl::ToDoubleMicroseconds(d1);40 i = static_cast<int>(absl::ToDoubleNanoseconds(d1));41 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]42 // CHECK-FIXES: i = absl::ToInt64Nanoseconds(d1);43 x = static_cast<float>(absl::ToInt64Nanoseconds(d1));44 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]45 // CHECK-FIXES: x = absl::ToDoubleNanoseconds(d1);46 47 // Functional-style casts48 i = int(absl::ToDoubleHours(d1));49 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]50 // CHECK-FIXES: i = absl::ToInt64Hours(d1);51 x = float(absl::ToInt64Microseconds(d1));52 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]53 // CHECK-FIXES: x = absl::ToDoubleMicroseconds(d1);54 55 // C-style casts56 i = (int) absl::ToDoubleHours(d1);57 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]58 // CHECK-FIXES: i = absl::ToInt64Hours(d1);59 x = (float) absl::ToInt64Microseconds(d1);60 // CHECK-MESSAGES: [[@LINE-1]]:7: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]61 // CHECK-FIXES: x = absl::ToDoubleMicroseconds(d1);62 63 // Type aliasing64 typedef int FancyInt;65 typedef float FancyFloat;66 67 FancyInt j = static_cast<FancyInt>(absl::ToDoubleHours(d1));68 // CHECK-MESSAGES: [[@LINE-1]]:16: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]69 // CHECK-FIXES: FancyInt j = absl::ToInt64Hours(d1);70 FancyFloat k = static_cast<FancyFloat>(absl::ToInt64Microseconds(d1));71 // CHECK-MESSAGES: [[@LINE-1]]:18: warning: duration should be converted directly to a floating-point number rather than through a type cast [abseil-duration-conversion-cast]72 // CHECK-FIXES: FancyFloat k = absl::ToDoubleMicroseconds(d1);73 74 // Macro handling75 // We want to transform things in macro arguments76#define EXTERNAL(x) (x) + 577 i = EXTERNAL(static_cast<int>(absl::ToDoubleSeconds(d1)));78 // CHECK-MESSAGES: [[@LINE-1]]:16: warning: duration should be converted directly to an integer rather than through a type cast [abseil-duration-conversion-cast]79 // CHECK-FIXES: i = EXTERNAL(absl::ToInt64Seconds(d1));80#undef EXTERNAL81 82 // We don't want to transform this which get split across macro boundaries83#define SPLIT(x) static_cast<int>((x)) + 584 i = SPLIT(absl::ToDoubleSeconds(d1));85#undef SPLIT86 87 // We also don't want to transform things inside of a macro definition88#define INTERNAL(x) static_cast<int>(absl::ToDoubleSeconds((x))) + 589 i = INTERNAL(d1);90#undef INTERNAL91 92 // These shouldn't be converted93 i = static_cast<int>(absl::ToInt64Seconds(d1));94 i = static_cast<float>(absl::ToDoubleSeconds(d1));95}96