44 lines · plain
1.. title:: clang-tidy - abseil-upgrade-duration-conversions2 3abseil-upgrade-duration-conversions4===================================5 6Finds calls to ``absl::Duration`` arithmetic operators and factories whose7argument needs an explicit cast to continue compiling after upcoming API8changes.9 10The operators ``*=``, ``/=``, ``*``, and ``/`` for ``absl::Duration`` currently11accept an argument of class type that is convertible to an arithmetic type.12Such a call currently converts the value to an ``int64_t``, even in a case such13as ``std::atomic<float>`` that would result in lossy conversion.14 15Additionally, the ``absl::Duration`` factory functions (``absl::Hours``,16``absl::Minutes``, etc) currently accept an ``int64_t`` or a floating-point17type. Similar to the arithmetic operators, calls with an argument of class type18that is convertible to an arithmetic type go through the ``int64_t`` path.19 20These operators and factories will be changed to only accept arithmetic types21to prevent unintended behavior. After these changes are released, passing an22argument of class type will no longer compile, even if the type is implicitly23convertible to an arithmetic type.24 25Here 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 33becomes34 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 41Note that this check always adds a cast to ``int64_t`` in order to preserve the42current behavior of user code. It is possible that this uncovers unintended43behavior due to types implicitly convertible to a floating-point type.44