* [PATCH v2] rust/list: replace unwrap() with ? in doctest examples
@ 2025-05-27 20:49 Albin Babu Varghese
2025-05-28 10:21 ` Alice Ryhl
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Albin Babu Varghese @ 2025-05-27 20:49 UTC (permalink / raw)
To: ojeda
Cc: alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin, a.hindborg,
aliceryhl, tmgross, dakr, rust-for-linux, linux-kernel,
Albin Babu Varghese
Using `unwrap()` in kernel doctests can cause panics on error and may
give newcomers the mistaken impression that panicking is acceptable
in kernel code.
Replace all `.unwrap()` calls in `kernel::list`
examples with `.ok_or(EINVAL)?` so that errors are properly propagated.
Closes: https://github.com/Rust-for-Linux/linux/issues/1164
Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Reviewed-by: Benno Lossin <lossin@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/1164
Signed-off-by: Albin Babu Varghese <albinbabuvarghese20@gmail.com>
---
Changes in v2:
- Fixed patch formatting issues
- Link to v1: https://lore.kernel.org/rust-for-linux/20250525225925.14797-1-albinbabuvarghese20@gmail.com/
---
rust/kernel/list.rs | 38 +++++++++++++++++++-------------------
1 file changed, 19 insertions(+), 19 deletions(-)
diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs
index c391c30b80f8..fe58a3920e70 100644
--- a/rust/kernel/list.rs
+++ b/rust/kernel/list.rs
@@ -82,9 +82,9 @@
/// // [15, 10, 30]
/// {
/// let mut iter = list.iter();
-/// assert_eq!(iter.next().unwrap().value, 15);
-/// assert_eq!(iter.next().unwrap().value, 10);
-/// assert_eq!(iter.next().unwrap().value, 30);
+/// assert_eq!(iter.next().ok_or(EINVAL)?.value, 15);
+/// assert_eq!(iter.next().ok_or(EINVAL)?.value, 10);
+/// assert_eq!(iter.next().ok_or(EINVAL)?.value, 30);
/// assert!(iter.next().is_none());
///
/// // Verify the length of the list.
@@ -93,9 +93,9 @@
///
/// // Pop the items from the list using `pop_back()` and verify the content.
/// {
-/// assert_eq!(list.pop_back().unwrap().value, 30);
-/// assert_eq!(list.pop_back().unwrap().value, 10);
-/// assert_eq!(list.pop_back().unwrap().value, 15);
+/// assert_eq!(list.pop_back().ok_or(EINVAL)?.value, 30);
+/// assert_eq!(list.pop_back().ok_or(EINVAL)?.value, 10);
+/// assert_eq!(list.pop_back().ok_or(EINVAL)?.value, 15);
/// }
///
/// // Insert 3 elements using `push_front()`.
@@ -107,9 +107,9 @@
/// // [30, 10, 15]
/// {
/// let mut iter = list.iter();
-/// assert_eq!(iter.next().unwrap().value, 30);
-/// assert_eq!(iter.next().unwrap().value, 10);
-/// assert_eq!(iter.next().unwrap().value, 15);
+/// assert_eq!(iter.next().ok_or(EINVAL)?.value, 30);
+/// assert_eq!(iter.next().ok_or(EINVAL)?.value, 10);
+/// assert_eq!(iter.next().ok_or(EINVAL)?.value, 15);
/// assert!(iter.next().is_none());
///
/// // Verify the length of the list.
@@ -118,8 +118,8 @@
///
/// // Pop the items from the list using `pop_front()` and verify the content.
/// {
-/// assert_eq!(list.pop_front().unwrap().value, 30);
-/// assert_eq!(list.pop_front().unwrap().value, 10);
+/// assert_eq!(list.pop_front().ok_or(EINVAL)?.value, 30);
+/// assert_eq!(list.pop_front().ok_or(EINVAL)?.value, 10);
/// }
///
/// // Push `list2` to `list` through `push_all_back()`.
@@ -135,9 +135,9 @@
/// // list: [15, 25, 35]
/// // list2: []
/// let mut iter = list.iter();
-/// assert_eq!(iter.next().unwrap().value, 15);
-/// assert_eq!(iter.next().unwrap().value, 25);
-/// assert_eq!(iter.next().unwrap().value, 35);
+/// assert_eq!(iter.next().ok_or(EINVAL)?.value, 15);
+/// assert_eq!(iter.next().ok_or(EINVAL)?.value, 25);
+/// assert_eq!(iter.next().ok_or(EINVAL)?.value, 35);
/// assert!(iter.next().is_none());
/// assert!(list2.is_empty());
/// }
@@ -809,11 +809,11 @@ fn next(&mut self) -> Option<ArcBorrow<'a, T>> {
/// merge_sorted(&mut list, list2);
///
/// let mut items = list.into_iter();
-/// assert_eq!(items.next().unwrap().value, 10);
-/// assert_eq!(items.next().unwrap().value, 11);
-/// assert_eq!(items.next().unwrap().value, 12);
-/// assert_eq!(items.next().unwrap().value, 13);
-/// assert_eq!(items.next().unwrap().value, 14);
+/// assert_eq!(items.next().ok_or(EINVAL)?.value, 10);
+/// assert_eq!(items.next().ok_or(EINVAL)?.value, 11);
+/// assert_eq!(items.next().ok_or(EINVAL)?.value, 12);
+/// assert_eq!(items.next().ok_or(EINVAL)?.value, 13);
+/// assert_eq!(items.next().ok_or(EINVAL)?.value, 14);
/// assert!(items.next().is_none());
/// # Result::<(), Error>::Ok(())
/// ```
---
Thanks,
Albin
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2] rust/list: replace unwrap() with ? in doctest examples
2025-05-27 20:49 [PATCH v2] rust/list: replace unwrap() with ? in doctest examples Albin Babu Varghese
@ 2025-05-28 10:21 ` Alice Ryhl
2025-05-28 10:25 ` Danilo Krummrich
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Alice Ryhl @ 2025-05-28 10:21 UTC (permalink / raw)
To: Albin Babu Varghese
Cc: ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, tmgross, dakr, rust-for-linux, linux-kernel
On Tue, May 27, 2025 at 04:49:28PM -0400, Albin Babu Varghese wrote:
> Using `unwrap()` in kernel doctests can cause panics on error and may
> give newcomers the mistaken impression that panicking is acceptable
> in kernel code.
>
> Replace all `.unwrap()` calls in `kernel::list`
> examples with `.ok_or(EINVAL)?` so that errors are properly propagated.
>
> Closes: https://github.com/Rust-for-Linux/linux/issues/1164
> Suggested-by: Miguel Ojeda <ojeda@kernel.org>
> Reviewed-by: Benno Lossin <lossin@kernel.org>
> Link: https://github.com/Rust-for-Linux/linux/issues/1164
> Signed-off-by: Albin Babu Varghese <albinbabuvarghese20@gmail.com>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] rust/list: replace unwrap() with ? in doctest examples
2025-05-27 20:49 [PATCH v2] rust/list: replace unwrap() with ? in doctest examples Albin Babu Varghese
2025-05-28 10:21 ` Alice Ryhl
@ 2025-05-28 10:25 ` Danilo Krummrich
2025-05-28 22:25 ` Albin Babu Varghese
2025-06-23 23:12 ` Miguel Ojeda
3 siblings, 0 replies; 5+ messages in thread
From: Danilo Krummrich @ 2025-05-28 10:25 UTC (permalink / raw)
To: Albin Babu Varghese
Cc: ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross, rust-for-linux, linux-kernel
On 5/27/25 10:49 PM, Albin Babu Varghese wrote:
> Using `unwrap()` in kernel doctests can cause panics on error and may
> give newcomers the mistaken impression that panicking is acceptable
> in kernel code.
>
> Replace all `.unwrap()` calls in `kernel::list`
> examples with `.ok_or(EINVAL)?` so that errors are properly propagated.
>
> Closes: https://github.com/Rust-for-Linux/linux/issues/1164
> Suggested-by: Miguel Ojeda <ojeda@kernel.org>
> Reviewed-by: Benno Lossin <lossin@kernel.org>
> Link: https://github.com/Rust-for-Linux/linux/issues/1164
> Signed-off-by: Albin Babu Varghese <albinbabuvarghese20@gmail.com>
Reviewed-by: Danilo Krummrich <dakr@kernel.org>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] rust/list: replace unwrap() with ? in doctest examples
2025-05-27 20:49 [PATCH v2] rust/list: replace unwrap() with ? in doctest examples Albin Babu Varghese
2025-05-28 10:21 ` Alice Ryhl
2025-05-28 10:25 ` Danilo Krummrich
@ 2025-05-28 22:25 ` Albin Babu Varghese
2025-06-23 23:12 ` Miguel Ojeda
3 siblings, 0 replies; 5+ messages in thread
From: Albin Babu Varghese @ 2025-05-28 22:25 UTC (permalink / raw)
To: ojeda
Cc: alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin, a.hindborg,
aliceryhl, tmgross, dakr, rust-for-linux, linux-kernel
Thank you Alice and Danilo for reviewing the patch.
---
Cheers,
Albin
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] rust/list: replace unwrap() with ? in doctest examples
2025-05-27 20:49 [PATCH v2] rust/list: replace unwrap() with ? in doctest examples Albin Babu Varghese
` (2 preceding siblings ...)
2025-05-28 22:25 ` Albin Babu Varghese
@ 2025-06-23 23:12 ` Miguel Ojeda
3 siblings, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2025-06-23 23:12 UTC (permalink / raw)
To: Albin Babu Varghese
Cc: ojeda, alex.gaynor, boqun.feng, gary, bjorn3_gh, lossin,
a.hindborg, aliceryhl, tmgross, dakr, rust-for-linux,
linux-kernel
On Tue, May 27, 2025 at 10:52 PM Albin Babu Varghese
<albinbabuvarghese20@gmail.com> wrote:
>
> Using `unwrap()` in kernel doctests can cause panics on error and may
> give newcomers the mistaken impression that panicking is acceptable
> in kernel code.
>
> Replace all `.unwrap()` calls in `kernel::list`
> examples with `.ok_or(EINVAL)?` so that errors are properly propagated.
>
> Closes: https://github.com/Rust-for-Linux/linux/issues/1164
> Suggested-by: Miguel Ojeda <ojeda@kernel.org>
> Reviewed-by: Benno Lossin <lossin@kernel.org>
> Link: https://github.com/Rust-for-Linux/linux/issues/1164
> Signed-off-by: Albin Babu Varghese <albinbabuvarghese20@gmail.com>
Applied to `rust-next` -- thanks everyone!
[ Reworded slightly. - Miguel ]
Cheers,
Miguel
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-06-23 23:12 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-27 20:49 [PATCH v2] rust/list: replace unwrap() with ? in doctest examples Albin Babu Varghese
2025-05-28 10:21 ` Alice Ryhl
2025-05-28 10:25 ` Danilo Krummrich
2025-05-28 22:25 ` Albin Babu Varghese
2025-06-23 23:12 ` Miguel Ojeda
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).