Skip to content

Commit 6bb6c00

Browse files
committed
implicit Option-returning doctests
This distinguishes `Option` and `Result`-returning doctests with implicit `main` method, where the former tests must end with `Some(())`.
1 parent 7da1185 commit 6bb6c00

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/doc/rustdoc/src/documentation-tests.md

+13
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,19 @@ conversion, so type inference fails because the type is not unique. Please note
253253
that you must write the `(())` in one sequence without intermediate whitespace
254254
so that rustdoc understands you want an implicit `Result`-returning function.
255255

256+
As of version 1.37.0, this simplification also works with `Option`s, which can
257+
be handy to test e.g. iterators or checked arithmetic, for example:
258+
259+
```ignore
260+
/// ```
261+
/// let _ = &[].iter().next()?;
262+
///# Some(())
263+
/// ```
264+
```
265+
266+
Note that the result must be a `Some(())` and this has to be written in one go.
267+
In this case disambiguating the result isn't required.
268+
256269
## Documenting macros
257270

258271
Here’s an example of documenting a macro:

src/librustdoc/test.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,13 @@ pub fn make_test(s: &str,
499499
prog.push_str(everything_else);
500500
} else {
501501
let returns_result = everything_else.trim_end().ends_with("(())");
502+
let returns_option = everything_else.trim_end().ends_with("Some(())");
502503
let (main_pre, main_post) = if returns_result {
503-
("fn main() { fn _inner() -> Result<(), impl core::fmt::Debug> {",
504+
(if returns_option {
505+
"fn main() { fn _inner() -> Option<()> {"
506+
} else {
507+
"fn main() { fn _inner() -> Result<(), impl core::fmt::Debug> {"
508+
},
504509
"}\n_inner().unwrap() }")
505510
} else {
506511
("fn main() {\n", "\n}")

src/test/rustdoc/process-termination.rs

+12
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,16 @@
2121
/// Err("This is returned from `main`, leading to panic")?;
2222
/// Ok::<(), &'static str>(())
2323
/// ```
24+
///
25+
/// This also works with `Option<()>`s now:
26+
///
27+
/// ```rust
28+
/// Some(())
29+
/// ```
30+
///
31+
/// ```rust,should_panic
32+
/// let x: &[u32] = &[];
33+
/// let _ = x.iter().next()?;
34+
/// Some(())
35+
/// ```
2436
pub fn check_process_termination() {}

0 commit comments

Comments
 (0)