Skip to content

Do not emit missing_doc_code_examples rustdoc lint on module and a few other items #133595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/doc/rustdoc/src/unstable-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ It can be enabled using:
#![deny(rustdoc::missing_doc_code_examples)]
```

It is not emitted for items that cannot be instantiated/called such as fields, variants, modules,
associated trait/impl items, impl blocks, statics and constants.
It is also not emitted for foreign items, aliases, extern crates and imports.

## Extensions to the `#[doc]` attribute

These features operate by extending the `#[doc]` attribute, and thus can be caught by the compiler
Expand Down
11 changes: 9 additions & 2 deletions src/librustdoc/passes/check_doc_test_visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,22 @@ pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -
item.kind,
clean::StructFieldItem(_)
| clean::VariantItem(_)
| clean::AssocConstItem(..)
| clean::AssocTypeItem(..)
| clean::TypeAliasItem(_)
| clean::StaticItem(_)
| clean::ConstantItem(..)
| clean::ExternCrateItem { .. }
| clean::ImportItem(_)
| clean::PrimitiveItem(_)
| clean::KeywordItem
| clean::ModuleItem(_)
| clean::TraitAliasItem(_)
| clean::ForeignFunctionItem(..)
| clean::ForeignStaticItem(..)
| clean::ForeignTypeItem
| clean::AssocConstItem(..)
| clean::AssocTypeItem(..)
| clean::TyAssocConstItem(..)
| clean::TyAssocTypeItem(..)
// check for trait impl
| clean::ImplItem(box clean::Impl { trait_: Some(_), .. })
)
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/coverage/doc-examples-json.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"$DIR/doc-examples-json.rs":{"total":3,"with_docs":2,"total_examples":2,"with_examples":1}}
{"$DIR/doc-examples-json.rs":{"total":3,"with_docs":2,"total_examples":1,"with_examples":1}}
4 changes: 2 additions & 2 deletions tests/rustdoc-ui/coverage/doc-examples.stdout
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
+-------------------------------------+------------+------------+------------+------------+
| File | Documented | Percentage | Examples | Percentage |
+-------------------------------------+------------+------------+------------+------------+
| ...tdoc-ui/coverage/doc-examples.rs | 4 | 100.0% | 1 | 25.0% |
| ...tdoc-ui/coverage/doc-examples.rs | 4 | 100.0% | 1 | 33.3% |
+-------------------------------------+------------+------------+------------+------------+
| Total | 4 | 100.0% | 1 | 25.0% |
| Total | 4 | 100.0% | 1 | 33.3% |
+-------------------------------------+------------+------------+------------+------------+
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/coverage/json.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"$DIR/json.rs":{"total":17,"with_docs":12,"total_examples":15,"with_examples":6}}
{"$DIR/json.rs":{"total":17,"with_docs":12,"total_examples":13,"with_examples":6}}
1 change: 0 additions & 1 deletion tests/rustdoc-ui/lints/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#![feature(rustdoc_missing_doc_code_examples)]
//~^ WARN
//~^^ WARN

#![warn(missing_docs)]
#![warn(rustdoc::missing_doc_code_examples)]
Expand Down
28 changes: 9 additions & 19 deletions tests/rustdoc-ui/lints/check.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ warning: missing documentation for the crate
LL | / #![feature(rustdoc_missing_doc_code_examples)]
LL | |
LL | |
LL | | #![warn(missing_docs)]
... |
LL | |
LL | | pub fn foo() {}
| |_______________^
|
note: the lint level is defined here
--> $DIR/check.rs:9:9
--> $DIR/check.rs:8:9
|
LL | #![warn(missing_docs)]
| ^^^^^^^^^^^^

warning: missing documentation for a function
--> $DIR/check.rs:13:1
--> $DIR/check.rs:12:1
|
LL | pub fn foo() {}
| ^^^^^^^^^^^^
Expand All @@ -26,34 +27,23 @@ warning: no documentation found for this crate's top-level module
= help: The following guide may be of use:
https://doc.rust-lang.org/$CHANNEL/rustdoc/how-to-write-documentation.html
note: the lint level is defined here
--> $DIR/check.rs:11:9
--> $DIR/check.rs:10:9
|
LL | #![warn(rustdoc::all)]
| ^^^^^^^^^^^^
= note: `#[warn(rustdoc::missing_crate_level_docs)]` implied by `#[warn(rustdoc::all)]`

warning: missing code example in this documentation
--> $DIR/check.rs:5:1
--> $DIR/check.rs:12:1
|
LL | / #![feature(rustdoc_missing_doc_code_examples)]
LL | |
LL | |
... |
LL | |
LL | | pub fn foo() {}
| |_______________^
LL | pub fn foo() {}
| ^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/check.rs:10:9
--> $DIR/check.rs:9:9
|
LL | #![warn(rustdoc::missing_doc_code_examples)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: missing code example in this documentation
--> $DIR/check.rs:13:1
|
LL | pub fn foo() {}
| ^^^^^^^^^^^^^^^

warning: 5 warnings emitted
warning: 4 warnings emitted

3 changes: 1 addition & 2 deletions tests/rustdoc-ui/lints/doc-without-codeblock.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#![feature(rustdoc_missing_doc_code_examples)] //~ ERROR missing code example in this documentation
#![feature(rustdoc_missing_doc_code_examples)]
#![deny(rustdoc::missing_doc_code_examples)]

/// Some docs.
//~^ ERROR missing code example in this documentation
pub struct Foo;

/// And then, the princess died.
//~^ ERROR missing code example in this documentation
pub mod foo {
/// Or maybe not because she saved herself!
//~^ ERROR missing code example in this documentation
Expand Down
26 changes: 4 additions & 22 deletions tests/rustdoc-ui/lints/doc-without-codeblock.stderr
Original file line number Diff line number Diff line change
@@ -1,38 +1,20 @@
error: missing code example in this documentation
--> $DIR/doc-without-codeblock.rs:1:1
--> $DIR/doc-without-codeblock.rs:10:5
|
LL | / #![feature(rustdoc_missing_doc_code_examples)]
LL | | #![deny(rustdoc::missing_doc_code_examples)]
LL | |
LL | | /// Some docs.
... |
LL | | }
LL | | }
| |_^
LL | /// Or maybe not because she saved herself!
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/doc-without-codeblock.rs:2:9
|
LL | #![deny(rustdoc::missing_doc_code_examples)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: missing code example in this documentation
--> $DIR/doc-without-codeblock.rs:8:1
|
LL | /// And then, the princess died.
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: missing code example in this documentation
--> $DIR/doc-without-codeblock.rs:11:5
|
LL | /// Or maybe not because she saved herself!
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: missing code example in this documentation
--> $DIR/doc-without-codeblock.rs:4:1
|
LL | /// Some docs.
| ^^^^^^^^^^^^^^

error: aborting due to 4 previous errors
error: aborting due to 2 previous errors

2 changes: 1 addition & 1 deletion tests/rustdoc-ui/lints/lint-missing-doc-code-example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn test() {
}

#[allow(missing_docs)]
pub mod module1 { //~ ERROR
pub mod module1 {
}

#[allow(rustdoc::missing_doc_code_examples)]
Expand Down
14 changes: 4 additions & 10 deletions tests/rustdoc-ui/lints/lint-missing-doc-code-example.stderr
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
error: missing code example in this documentation
--> $DIR/lint-missing-doc-code-example.rs:20:1
--> $DIR/lint-missing-doc-code-example.rs:38:3
|
LL | pub mod module1 {
| ^^^^^^^^^^^^^^^
LL | /// doc
| ^^^^^^^
|
note: the lint level is defined here
--> $DIR/lint-missing-doc-code-example.rs:3:9
|
LL | #![deny(rustdoc::missing_doc_code_examples)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: missing code example in this documentation
--> $DIR/lint-missing-doc-code-example.rs:38:3
|
LL | /// doc
| ^^^^^^^

error: missing code example in this documentation
--> $DIR/lint-missing-doc-code-example.rs:50:1
|
Expand All @@ -34,5 +28,5 @@ error: missing code example in this documentation
LL | /// Doc
| ^^^^^^^

error: aborting due to 5 previous errors
error: aborting due to 4 previous errors

2 changes: 1 addition & 1 deletion tests/rustdoc-ui/show-coverage-json.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"$DIR/show-coverage-json.rs":{"total":2,"with_docs":1,"total_examples":2,"with_examples":1}}
{"$DIR/show-coverage-json.rs":{"total":2,"with_docs":1,"total_examples":1,"with_examples":1}}
4 changes: 2 additions & 2 deletions tests/rustdoc-ui/show-coverage.stdout
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
+-------------------------------------+------------+------------+------------+------------+
| File | Documented | Percentage | Examples | Percentage |
+-------------------------------------+------------+------------+------------+------------+
| ...ests/rustdoc-ui/show-coverage.rs | 1 | 50.0% | 1 | 50.0% |
| ...ests/rustdoc-ui/show-coverage.rs | 1 | 50.0% | 1 | 100.0% |
+-------------------------------------+------------+------------+------------+------------+
| Total | 1 | 50.0% | 1 | 50.0% |
| Total | 1 | 50.0% | 1 | 100.0% |
+-------------------------------------+------------+------------+------------+------------+
Loading