Skip to content

Commit cdadb6c

Browse files
Rollup merge of rust-lang#45767 - QuietMisdreavus:who-docs-the-doc-tool, r=frewsxcv
rustdoc book: talk about #![doc(test(no_crate_inject))] and #![doc(test(attr(...)))] While investigating rust-lang#45750 i noticed that `#![doc(test(attr(...)))]` wasn't documented at all. Since this is useful for making your examples follow the same coding guidelines as your code, i wanted to add it to the Rustdoc Book. I also added `#![doc(test(no_crate_inject))]` since it's used in the same place and might be useful for macro-heavy crates. I added mentions for these to "The `doc` attribute" as well as "Documentation tests" since it's useful information in both places. Technically the step reordering in the second commit is gated on rust-lang#45764, since before that lands attributes from the doctest come before the ones from `#![doc(test(attr(...)))]`.
2 parents 859c716 + ce7a3a4 commit cdadb6c

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,19 @@ function! Forcing you to write `main` for every example, no matter how small,
3838
adds friction. So `rustdoc` processes your examples slightly before
3939
running them. Here's the full algorithm rustdoc uses to preprocess examples:
4040

41-
1. Any leading `#![foo]` attributes are left intact as crate attributes.
42-
2. Some common `allow` attributes are inserted, including
41+
1. Some common `allow` attributes are inserted, including
4342
`unused_variables`, `unused_assignments`, `unused_mut`,
4443
`unused_attributes`, and `dead_code`. Small examples often trigger
4544
these lints.
46-
3. If the example does not contain `extern crate`, then `extern crate
45+
2. Any attributes specified with `#![doc(test(attr(...)))]` are added.
46+
3. Any leading `#![foo]` attributes are left intact as crate attributes.
47+
4. If the example does not contain `extern crate`, and
48+
`#![doc(test(no_crate_inject))]` was not specified, then `extern crate
4749
<mycrate>;` is inserted (note the lack of `#[macro_use]`).
48-
4. Finally, if the example does not contain `fn main`, the remainder of the
50+
5. Finally, if the example does not contain `fn main`, the remainder of the
4951
text is wrapped in `fn main() { your_code }`.
5052

51-
For more about that caveat in rule 3, see "Documeting Macros" below.
53+
For more about that caveat in rule 4, see "Documeting Macros" below.
5254

5355
## Hiding portions of the example
5456

@@ -261,4 +263,4 @@ are added.
261263
The `no_run` attribute will compile your code, but not run it. This is
262264
important for examples such as "Here's how to retrieve a web page,"
263265
which you would want to ensure compiles, but might be run in a test
264-
environment that has no network access.
266+
environment that has no network access.

src/doc/rustdoc/src/the-doc-attribute.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ to it in the docs. But if you include this:
103103

104104
it will not.
105105

106+
### `test(no_crate_inject)`
107+
108+
By default, `rustdoc` will automatically add a line with `extern crate my_crate;` into each doctest.
109+
But if you include this:
110+
111+
```rust,ignore
112+
#![doc(test(no_crate_inject))]
113+
```
114+
115+
it will not.
116+
117+
### `test(attr(...))`
118+
119+
This form of the `doc` attribute allows you to add arbitrary attributes to all your doctests. For
120+
example, if you want your doctests to fail if they produce any warnings, you could add this:
121+
122+
```rust,ignore
123+
#![doc(test(attr(deny(warnings))))]
124+
```
125+
106126
## At the item level
107127

108128
These forms of the `#[doc]` attribute are used on individual items, to control how

0 commit comments

Comments
 (0)