[libcxx-commits] [libcxx] WIP [libc++][random] Applied `[[nodiscard]]` to `<random>` (PR #204970)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Jun 21 03:01:19 PDT 2026
https://github.com/H-G-Hristov created https://github.com/llvm/llvm-project/pull/204970
Towards: #172124
- https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
- https://wg21.link/rand
>From 8189fcc34be1aee13a2bdbf0d88ee673e500c47c Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Sun, 21 Jun 2026 13:00:15 +0300
Subject: [PATCH] [libc++][random] Applied `[[nodiscard]]` to `<random>`
Towards: #172124
- https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant
- https://wg21.link/rand
---
.../include/__random/bernoulli_distribution.h | 14 ++--
.../include/__random/binomial_distribution.h | 18 ++---
.../libcxx/numerics/rand/nodiscard.verify.cpp | 66 +++++++++++++++++++
3 files changed, 82 insertions(+), 16 deletions(-)
create mode 100644 libcxx/test/libcxx/numerics/rand/nodiscard.verify.cpp
diff --git a/libcxx/include/__random/bernoulli_distribution.h b/libcxx/include/__random/bernoulli_distribution.h
index 8d1bfad9e7d29..54ca0ee0f7504 100644
--- a/libcxx/include/__random/bernoulli_distribution.h
+++ b/libcxx/include/__random/bernoulli_distribution.h
@@ -36,7 +36,7 @@ class bernoulli_distribution {
_LIBCPP_HIDE_FROM_ABI explicit param_type(double __p = 0.5) : __p_(__p) {}
- _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
return __x.__p_ == __y.__p_;
@@ -60,20 +60,20 @@ class bernoulli_distribution {
// generating functions
template <class _URNG>
- _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
return (*this)(__g, __p_);
}
template <class _URNG>
- _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
// property functions
- _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
- _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
_LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
- _LIBCPP_HIDE_FROM_ABI result_type min() const { return false; }
- _LIBCPP_HIDE_FROM_ABI result_type max() const { return true; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type min() const { return false; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type max() const { return true; }
friend _LIBCPP_HIDE_FROM_ABI bool operator==(const bernoulli_distribution& __x, const bernoulli_distribution& __y) {
return __x.__p_ == __y.__p_;
diff --git a/libcxx/include/__random/binomial_distribution.h b/libcxx/include/__random/binomial_distribution.h
index 76996abacb3ac..e4fe92143741a 100644
--- a/libcxx/include/__random/binomial_distribution.h
+++ b/libcxx/include/__random/binomial_distribution.h
@@ -45,8 +45,8 @@ class binomial_distribution {
_LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __t = 1, double __p = 0.5);
- _LIBCPP_HIDE_FROM_ABI result_type t() const { return __t_; }
- _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type t() const { return __t_; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
return __x.__t_ == __y.__t_ && __x.__p_ == __y.__p_;
@@ -74,21 +74,21 @@ class binomial_distribution {
// generating functions
template <class _URNG>
- _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
return (*this)(__g, __p_);
}
template <class _URNG>
- _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
// property functions
- _LIBCPP_HIDE_FROM_ABI result_type t() const { return __p_.t(); }
- _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type t() const { return __p_.t(); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
- _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
_LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
- _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
- _LIBCPP_HIDE_FROM_ABI result_type max() const { return t(); }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type max() const { return t(); }
friend _LIBCPP_HIDE_FROM_ABI bool operator==(const binomial_distribution& __x, const binomial_distribution& __y) {
return __x.__p_ == __y.__p_;
diff --git a/libcxx/test/libcxx/numerics/rand/nodiscard.verify.cpp b/libcxx/test/libcxx/numerics/rand/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..fad4e303ad297
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/rand/nodiscard.verify.cpp
@@ -0,0 +1,66 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <random>
+
+// Check that functions are marked [[nodiscard]]
+
+#include <random>
+
+#include "test_macros.h"
+
+void test() {
+ {
+ std::bernoulli_distribution d;
+ std::mt19937_64 gen;
+
+ std::bernoulli_distribution::param_type p;
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ p.p();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d(gen);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d(gen, p);
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d.p();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d.param();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d.min();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d.max();
+ }
+ {
+ std::binomial_distribution<int> d;
+ std::mt19937_64 gen;
+
+ std::binomial_distribution<int>::param_type p;
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ p.t();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ p.p();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d(gen);
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d(gen, p);
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d.p();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d.param();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d.min();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ d.max();
+ }
+}
More information about the libcxx-commits
mailing list