[libcxx-commits] [libcxx] [libc++][chrono] Applied `[[nodiscard]]` to The Civil Calendar (PR #174968)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jan 10 22:10:20 PST 2026


================
@@ -107,223 +107,832 @@ void test(std::chrono::time_zone tz, std::chrono::time_zone_link link, std::chro
 }
 #endif // TEST_STD_VER >= 20
 
-void test() {
-  { // [time.duration]
-    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::microseconds(2));
+void test_duration() { // [time.duration]
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::microseconds(2));
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::duration_values<int>::zero();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::duration_values<int>::max();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::duration_values<int>::min();
+
+  std::chrono::duration<int, std::ratio<1, 30> > dr;
+
+#if TEST_STD_VER >= 17
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::floor<std::chrono::seconds>(dr);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::ceil<std::chrono::seconds>(dr);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::round<std::chrono::seconds>(dr);
+#endif
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  dr.count();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  +dr;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  -dr;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::duration<int, std::ratio<1, 30> >::zero();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::duration<int, std::ratio<1, 30> >::max();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::duration<int, std::ratio<1, 30> >::min();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  dr + dr;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  dr - dr;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  dr * 94;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  94 * dr;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  dr / 82;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  dr / dr;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  dr % 47;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  dr % dr;
+
+#if TEST_STD_VER >= 17
+  using namespace std::chrono_literals;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  94h;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  82.5h;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  94min;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  82.5min;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  94s;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  82.5s;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  94ms;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  82.5ms;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  94us;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  82.5us;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  94ns;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  82.5ns;
+#endif // TEST_STD_VER >= 14
+
+#if TEST_STD_VER >= 26
+  std::hash<std::chrono::duration<int, std::ratio<1, 30>>> hash;
 
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  hash(dr);
+#endif
+}
+
+#if TEST_STD_VER >= 20
+void test_hh_mm_ss() { // [time.hms]
+  const std::chrono::hh_mm_ss<std::chrono::seconds> hms;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  hms.is_negative();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  hms.hours();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  hms.minutes();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  hms.seconds();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  hms.subseconds();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  hms.to_duration();
+
+  using namespace std::chrono_literals;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::is_am(1h);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::is_pm(1h);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::make12(1h);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::make24(1h, false);
+}
+#endif
+
+void test_time_point() { // [time.point]
+  std::chrono::time_point<std::chrono::system_clock> tp;
+  std::chrono::duration<double, std::ratio<1, 30> > dr;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  tp.time_since_epoch();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  tp.min();
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  tp.max();
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::time_point_cast<std::chrono::seconds>(tp);
+
+#if TEST_STD_VER >= 17
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::floor<std::chrono::seconds>(tp);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::ceil<std::chrono::seconds>(tp);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::round<std::chrono::seconds>(tp);
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::chrono::abs(dr);
+#endif
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  tp + dr;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  dr + tp;
+
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  tp - dr;
+  // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  tp - tp;
+}
+
+void test_clocks() {
+#if TEST_STD_VER >= 20
+  { // [time.clock.file]
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    std::chrono::duration_values<int>::zero();
+    std::chrono::file_clock::now();
+
+    using Duration = std::chrono::duration<double, std::ratio<1, 30>>;
+
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    std::chrono::duration_values<int>::max();
+    std::chrono::file_clock::to_sys(std::chrono::file_time<Duration>{});
+
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    std::chrono::duration_values<int>::min();
+    std::chrono::file_clock::from_sys(std::chrono::sys_time<Duration>{});
+  }
+#endif
 
-    std::chrono::duration<int, std::ratio<1, 30> > dr;
+#if TEST_STD_VER >= 20
+  { // [time.clock.gps]
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::chrono::gps_clock::now();
 
-#if TEST_STD_VER >= 17
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    std::chrono::floor<std::chrono::seconds>(dr);
+    std::chrono::gps_clock::to_utc(std::chrono::gps_seconds{});
+
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    std::chrono::ceil<std::chrono::seconds>(dr);
+    std::chrono::gps_clock::from_utc(std::chrono::utc_seconds{});
+  }
+#endif // TEST_STD_VER >= 20
+
+#if _LIBCPP_HAS_MONOTONIC_CLOCK
+  { // [time.clock.steady]
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    std::chrono::round<std::chrono::seconds>(dr);
+    std::chrono::steady_clock::now();
+  }
 #endif
 
+  { // [time.clock.system]
+
+    std::chrono::time_point<std::chrono::system_clock> tp;
+    std::time_t time = std::time(nullptr);
+
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    dr.count();
+    std::chrono::system_clock::now();
 
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    +dr;
+    std::chrono::system_clock::to_time_t(tp);
+
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    -dr;
+    std::chrono::system_clock::from_time_t(time);
+
+#if TEST_STD_VER >= 26
+    std::hash<std::chrono::time_point<std::chrono::system_clock>> hash;
 
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    std::chrono::duration<int, std::ratio<1, 30> >::zero();
+    hash(tp);
+#endif
+  }
+
+#if TEST_STD_VER >= 20
+  { // [time.clock.tai]
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    std::chrono::duration<int, std::ratio<1, 30> >::max();
+    std::chrono::tai_clock::now();
+
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    std::chrono::duration<int, std::ratio<1, 30> >::min();
+    std::chrono::tai_clock::to_utc(std::chrono::tai_seconds{});
 
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    dr + dr;
+    std::chrono::tai_clock::from_utc(std::chrono::utc_seconds{});
+  }
+#endif
+
+#if TEST_STD_VER >= 20
+  { // [time.clock.utc]
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    dr - dr;
+    std::chrono::utc_clock::now();
+
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    dr * 94;
+    std::chrono::utc_clock::to_sys(std::chrono::utc_seconds{});
+
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    94 * dr;
+    std::chrono::utc_clock::from_sys(std::chrono::sys_seconds{});
+
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    dr / 82;
+    std::chrono::get_leap_second_info(std::chrono::utc_seconds{});
+  }
+#endif // TEST_STD_VER >= 20
+}
+
+#if TEST_STD_VER >= 20
+void test_calendar() { // [time.cal]
+
+  { // [time.cal.day]
+    const std::chrono::day day{};
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    day.ok();
+
+    std::chrono::days days;
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    day + days;
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    dr / dr;
+    days + day;
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    dr % 47;
+    day - days;
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    dr % dr;
+    day - day;
 
-#if TEST_STD_VER >= 17
+#  if TEST_STD_VER >= 26
+    std::hash<std::chrono::day> hash;
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    hash(day);
+#  endif
+  }
+
+  {
     using namespace std::chrono_literals;
 
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    94h;
+    94d;
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    82y;
+  }
+
+  { // [time.cal.month]
+    const std::chrono::month month{};
+
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    82.5h;
+    month.ok();
 
+    std::chrono::months months;
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    month + months;
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    months + month;
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    94min;
+    month - months;
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    82.5min;
+    month - month;
+
+#  if TEST_STD_VER >= 26
+    std::hash<std::chrono::month> hash;
 
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    94s;
+    hash(month);
+#  endif
+  }
+
+  { // [time.cal.mwd]
+    std::chrono::month month;
+    std::chrono::weekday_indexed wdidx;
+
+    const std::chrono::month_weekday mwd{month, wdidx};
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    mwd.month();
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    mwd.weekday_indexed();
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    82.5s;
+    mwd.ok();
 
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    94ms;
+    month / wdidx;
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    82.5ms;
+    94 / wdidx;
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    wdidx / month;
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    wdidx / 82;
+
+#  if TEST_STD_VER >= 26
+    std::hash<std::chrono::month_weekday> hash;
 
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    94us;
+    hash(mwd);
+#  endif
+  }
+
+  { // [time.cal.mwdlast]
+    std::chrono::month month;
+    std::chrono::weekday_last wdl{{}};
+
+    const std::chrono::month_weekday_last mwdl{month, wdl};
+
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    82.5us;
+    mwdl.month();
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    mwdl.weekday_last();
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    mwdl.ok();
 
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    94ns;
+    month / wdl;
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    82.5ns;
-#endif // TEST_STD_VER >= 14
+    94 / wdl;
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    wdl / month;
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    wdl / 82;
 
-#if TEST_STD_VER >= 26
-    std::hash<std::chrono::duration<int, std::ratio<1, 30>>> hash;
+#  if TEST_STD_VER >= 26
+    std::hash<std::chrono::month_weekday_last> hash;
 
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    hash(dr);
-#endif
+    hash(mwdl);
+#  endif
   }
 
-#if TEST_STD_VER >= 20
-  { // [time.clock.file]
+  { // [time.cal.month]
+    const std::chrono::month month{};
+
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    std::chrono::file_clock::now();
+    month.ok();
 
-    using Duration = std::chrono::duration<double, std::ratio<1, 30>>;
+    std::chrono::months months;
 
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    std::chrono::file_clock::to_sys(std::chrono::file_time<Duration>{});
+    month + months;
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    months + month;
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    month - months;
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    month - month;
+
+#  if TEST_STD_VER >= 26
+    std::hash<std::chrono::month> hash;
 
     // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
-    std::chrono::file_clock::from_sys(std::chrono::sys_time<Duration>{});
+    hash(month);
+#  endif
   }
----------------
H-G-Hristov wrote:


```suggestion
```

A duplicate

https://github.com/llvm/llvm-project/pull/174968


More information about the libcxx-commits mailing list