| 1 |
.. title:: clang-tidy - abseil-duration-conversion-cast |
| 2 |
|
| 3 |
abseil-duration-conversion-cast |
| 4 |
=============================== |
| 5 |
|
| 6 |
Checks for casts of ``absl::Duration`` conversion functions, and recommends |
| 7 |
the right conversion function instead. |
| 8 |
|
| 9 |
Examples: |
| 10 |
|
| 11 |
.. code-block:: c++ |
| 12 |
|
| 13 |
// Original - Cast from a double to an integer |
| 14 |
absl::Duration d; |
| 15 |
int i = static_cast<int>(absl::ToDoubleSeconds(d)); |
| 16 |
|
| 17 |
// Suggested - Use the integer conversion function directly. |
| 18 |
int i = absl::ToInt64Seconds(d); |
| 19 |
|
| 20 |
|
| 21 |
// Original - Cast from a double to an integer |
| 22 |
absl::Duration d; |
| 23 |
double x = static_cast<double>(absl::ToInt64Seconds(d)); |
| 24 |
|
| 25 |
// Suggested - Use the integer conversion function directly. |
| 26 |
double x = absl::ToDoubleSeconds(d); |
| 27 |
|
| 28 |
|
| 29 |
Note: In the second example, the suggested fix could yield a different result, |
| 30 |
as the conversion to integer could truncate. In practice, this is very rare, |
| 31 |
and you should use ``absl::Trunc`` to perform this operation explicitly instead. |