[libcxx-commits] [libcxx] ef25db4 - [libc++][chrono] Avoid tautological comparisions.

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 12 10:15:35 PDT 2022


Author: Mark de Wever
Date: 2022-07-12T19:15:24+02:00
New Revision: ef25db495bdbceb0f4881d3ee2c5a7a81d70fe2a

URL: https://github.com/llvm/llvm-project/commit/ef25db495bdbceb0f4881d3ee2c5a7a81d70fe2a
DIFF: https://github.com/llvm/llvm-project/commit/ef25db495bdbceb0f4881d3ee2c5a7a81d70fe2a.diff

LOG: [libc++][chrono] Avoid tautological comparisions.

In our implementation the year is always less than or equal to the
class' `max()`. It's unlikely this ever changes since changing the
year's range will be an ABI break. A static_assert is added as a
guard.

This was reported by @philnik.

Reviewed By: ldionne, #libc

Differential Revision: https://reviews.llvm.org/D129442

Added: 
    

Modified: 
    libcxx/include/__chrono/year.h

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__chrono/year.h b/libcxx/include/__chrono/year.h
index a641fe1c93b0a..c7f0027eba7b4 100644
--- a/libcxx/include/__chrono/year.h
+++ b/libcxx/include/__chrono/year.h
@@ -12,6 +12,7 @@
 
 #include <__chrono/duration.h>
 #include <__config>
+#include <limits>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -100,9 +101,11 @@ _LIBCPP_HIDE_FROM_ABI inline constexpr
 year& year::operator-=(const years& __dy) noexcept
 { *this = *this - __dy; return *this; }
 
-_LIBCPP_HIDE_FROM_ABI inline constexpr
-bool year::ok() const noexcept
-{ return static_cast<int>(min()) <= __y && __y <= static_cast<int>(max()); }
+_LIBCPP_HIDE_FROM_ABI constexpr bool year::ok() const noexcept {
+  static_assert(static_cast<int>(std::numeric_limits<decltype(__y)>::max()) == static_cast<int>(max()));
+  return static_cast<int>(min()) <= __y;
+}
+
 } // namespace chrono
 
 _LIBCPP_END_NAMESPACE_STD


        


More information about the libcxx-commits mailing list