Skip to content

Commit b1bb8aa

Browse files
committed
Auto merge of #75776 - GuillaumeGomez:missing-doc-examples-lint-improvements, r=jyn514
Missing doc examples lint improvements Fixes #75719. To be merged after #75718 (only the two last commits are from this PR). Since you already reviewed #75718, I'll set you as reviewer here as well. :) r? @jyn514
2 parents ebc03f7 + 7a05f13 commit b1bb8aa

File tree

4 files changed

+74
-27
lines changed

4 files changed

+74
-27
lines changed

Diff for: src/librustdoc/passes/calculate_doc_coverage.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::config::OutputFormat;
33
use crate::core::DocContext;
44
use crate::fold::{self, DocFolder};
55
use crate::html::markdown::{find_testable_code, ErrorCodes};
6-
use crate::passes::doc_test_lints::Tests;
6+
use crate::passes::doc_test_lints::{should_have_doc_example, Tests};
77
use crate::passes::Pass;
88
use rustc_span::symbol::sym;
99
use rustc_span::FileName;
@@ -231,19 +231,6 @@ impl fold::DocFolder for CoverageCalculator {
231231
let has_docs = !i.attrs.doc_strings.is_empty();
232232
let mut tests = Tests { found_tests: 0 };
233233

234-
let should_have_doc_examples = !matches!(i.inner,
235-
clean::StructFieldItem(_)
236-
| clean::VariantItem(_)
237-
| clean::AssocConstItem(_, _)
238-
| clean::AssocTypeItem(_, _)
239-
| clean::TypedefItem(_, _)
240-
| clean::StaticItem(_)
241-
| clean::ConstantItem(_)
242-
| clean::ExternCrateItem(_, _)
243-
| clean::ImportItem(_)
244-
| clean::PrimitiveItem(_)
245-
| clean::KeywordItem(_)
246-
);
247234
find_testable_code(
248235
&i.attrs.doc_strings.iter().map(|d| d.as_str()).collect::<Vec<_>>().join("\n"),
249236
&mut tests,
@@ -257,7 +244,7 @@ impl fold::DocFolder for CoverageCalculator {
257244
self.items.entry(i.source.filename.clone()).or_default().count_item(
258245
has_docs,
259246
has_doc_example,
260-
should_have_doc_examples,
247+
should_have_doc_example(&i.inner),
261248
);
262249
}
263250
}

Diff for: src/librustdoc/passes/doc_test_lints.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! - PRIVATE_DOC_TESTS: this looks for private items with doc-tests.
55
66
use super::{span_of_attrs, Pass};
7+
use crate::clean;
78
use crate::clean::*;
89
use crate::core::DocContext;
910
use crate::fold::DocFolder;
@@ -59,6 +60,22 @@ impl crate::test::Tester for Tests {
5960
}
6061
}
6162

63+
pub fn should_have_doc_example(item_kind: &clean::ItemEnum) -> bool {
64+
!matches!(item_kind,
65+
clean::StructFieldItem(_)
66+
| clean::VariantItem(_)
67+
| clean::AssocConstItem(_, _)
68+
| clean::AssocTypeItem(_, _)
69+
| clean::TypedefItem(_, _)
70+
| clean::StaticItem(_)
71+
| clean::ConstantItem(_)
72+
| clean::ExternCrateItem(_, _)
73+
| clean::ImportItem(_)
74+
| clean::PrimitiveItem(_)
75+
| clean::KeywordItem(_)
76+
)
77+
}
78+
6279
pub fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
6380
let hir_id = match cx.as_local_hir_id(item.def_id) {
6481
Some(hir_id) => hir_id,
@@ -73,13 +90,7 @@ pub fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
7390
find_testable_code(&dox, &mut tests, ErrorCodes::No, false, None);
7491

7592
if tests.found_tests == 0 {
76-
use ItemEnum::*;
77-
78-
let should_report = match item.inner {
79-
ExternCrateItem(_, _) | ImportItem(_) | PrimitiveItem(_) | KeywordItem(_) => false,
80-
_ => true,
81-
};
82-
if should_report {
93+
if should_have_doc_example(&item.inner) {
8394
debug!("reporting error for {:?} (hir_id={:?})", item, hir_id);
8495
let sp = span_of_attrs(&item.attrs).unwrap_or(item.source.span());
8596
cx.tcx.struct_span_lint_hir(

Diff for: src/test/rustdoc-ui/lint-missing-doc-code-example.rs

+31
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,34 @@ pub mod module3 {
3838
//~^ ERROR
3939
pub fn test() {}
4040
}
41+
42+
/// Doc, but no code example and it's fine!
43+
pub const Const: u32 = 0;
44+
/// Doc, but no code example and it's fine!
45+
pub static Static: u32 = 0;
46+
/// Doc, but no code example and it's fine!
47+
pub type Type = u32;
48+
49+
/// Doc
50+
//~^ ERROR
51+
pub struct Struct {
52+
/// Doc, but no code example and it's fine!
53+
pub field: u32,
54+
}
55+
56+
/// Doc
57+
//~^ ERROR
58+
pub enum Enum {
59+
/// Doc, but no code example and it's fine!
60+
X,
61+
}
62+
63+
/// Doc
64+
//~^ ERROR
65+
#[repr(C)]
66+
union Union {
67+
/// Doc, but no code example and it's fine!
68+
a: i32,
69+
/// Doc, but no code example and it's fine!
70+
b: f32,
71+
}
+23-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
error: missing code example in this documentation
2-
--> $DIR/lint-missing-doc-code-example.rs:19:1
2+
--> $DIR/lint-missing-doc-code-example.rs:49:1
33
|
4-
LL | / mod module1 {
5-
LL | | }
6-
| |_^
4+
LL | /// Doc
5+
| ^^^^^^^
76
|
87
note: the lint level is defined here
98
--> $DIR/lint-missing-doc-code-example.rs:2:9
109
|
1110
LL | #![deny(missing_doc_code_examples)]
1211
| ^^^^^^^^^^^^^^^^^^^^^^^^^
1312

13+
error: missing code example in this documentation
14+
--> $DIR/lint-missing-doc-code-example.rs:63:1
15+
|
16+
LL | /// Doc
17+
| ^^^^^^^
18+
19+
error: missing code example in this documentation
20+
--> $DIR/lint-missing-doc-code-example.rs:56:1
21+
|
22+
LL | /// Doc
23+
| ^^^^^^^
24+
25+
error: missing code example in this documentation
26+
--> $DIR/lint-missing-doc-code-example.rs:19:1
27+
|
28+
LL | / mod module1 {
29+
LL | | }
30+
| |_^
31+
1432
error: missing code example in this documentation
1533
--> $DIR/lint-missing-doc-code-example.rs:37:3
1634
|
1735
LL | /// doc
1836
| ^^^^^^^
1937

20-
error: aborting due to 2 previous errors
38+
error: aborting due to 5 previous errors
2139

0 commit comments

Comments
 (0)