| 1 |
.. title:: clang-tidy - abseil-upgrade-duration-conversions |
| 2 |
|
| 3 |
abseil-upgrade-duration-conversions |
| 4 |
=================================== |
| 5 |
|
| 6 |
Finds calls to ``absl::Duration`` arithmetic operators and factories whose |
| 7 |
argument needs an explicit cast to continue compiling after upcoming API |
| 8 |
changes. |
| 9 |
|
| 10 |
The operators ``*=``, ``/=``, ``*``, and ``/`` for ``absl::Duration`` currently |
| 11 |
accept an argument of class type that is convertible to an arithmetic type. Such |
| 12 |
a call currently converts the value to an ``int64_t``, even in a case such as |
| 13 |
``std::atomic<float>`` that would result in lossy conversion. |
| 14 |
|
| 15 |
Additionally, the ``absl::Duration`` factory functions (``absl::Hours``, |
| 16 |
``absl::Minutes``, etc) currently accept an ``int64_t`` or a floating-point |
| 17 |
type. Similar to the arithmetic operators, calls with an argument of class type |
| 18 |
that is convertible to an arithmetic type go through the ``int64_t`` path. |
| 19 |
|
| 20 |
These operators and factories will be changed to only accept arithmetic types to |
| 21 |
prevent unintended behavior. After these changes are released, passing an |
| 22 |
argument of class type will no longer compile, even if the type is implicitly |
| 23 |
convertible to an arithmetic type. |
| 24 |
|
| 25 |
Here are example fixes created by this check: |
| 26 |
|
| 27 |
.. code-block:: c++ |
| 28 |
|
| 29 |
std::atomic<int> a; |
| 30 |
absl::Duration d = absl::Milliseconds(a); |
| 31 |
d *= a; |
| 32 |
|
| 33 |
becomes |
| 34 |
|
| 35 |
.. code-block:: c++ |
| 36 |
|
| 37 |
std::atomic<int> a; |
| 38 |
absl::Duration d = absl::Milliseconds(static_cast<int64_t>(a)); |
| 39 |
d *= static_cast<int64_t>(a); |
| 40 |
|
| 41 |
Note that this check always adds a cast to ``int64_t`` in order to preserve the |
| 42 |
current behavior of user code. It is possible that this uncovers unintended |
| 43 |
behavior due to types implicitly convertible to a floating-point type. |