Skip to content

Commit 7762131

Browse files
committed
Auto merge of #66938 - GuillaumeGomez:lint-for-no-crate-level-doc, r=Dylan-DPC
Add lint when no doc is present at the crate-level Follow-up of #66267. r? @kinnison
2 parents c52cee1 + be97eb4 commit 7762131

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

src/librustc_session/lint/builtin.rs

+7
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,12 @@ declare_lint! {
386386
"failures in resolving intra-doc link targets"
387387
}
388388

389+
declare_lint! {
390+
pub MISSING_CRATE_LEVEL_DOCS,
391+
Allow,
392+
"detects crates with no crate-level documentation"
393+
}
394+
389395
declare_lint! {
390396
pub MISSING_DOC_CODE_EXAMPLES,
391397
Allow,
@@ -547,6 +553,7 @@ declare_lint_pass! {
547553
UNSTABLE_NAME_COLLISIONS,
548554
IRREFUTABLE_LET_PATTERNS,
549555
INTRA_DOC_LINK_RESOLUTION_FAILURE,
556+
MISSING_CRATE_LEVEL_DOCS,
550557
MISSING_DOC_CODE_EXAMPLES,
551558
PRIVATE_DOC_TESTS,
552559
WHERE_CLAUSES_OBJECT_SAFETY,

src/librustdoc/core.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
249249
let missing_docs = rustc_lint::builtin::MISSING_DOCS.name;
250250
let missing_doc_example = rustc_lint::builtin::MISSING_DOC_CODE_EXAMPLES.name;
251251
let private_doc_tests = rustc_lint::builtin::PRIVATE_DOC_TESTS.name;
252+
let no_crate_level_docs = rustc_lint::builtin::MISSING_CRATE_LEVEL_DOCS.name;
252253

253254
// In addition to those specific lints, we also need to whitelist those given through
254255
// command line, otherwise they'll get ignored and we don't want that.
@@ -258,6 +259,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
258259
missing_docs.to_owned(),
259260
missing_doc_example.to_owned(),
260261
private_doc_tests.to_owned(),
262+
no_crate_level_docs.to_owned(),
261263
];
262264

263265
whitelisted_lints.extend(lint_opts.iter().map(|(lint, _)| lint).cloned());
@@ -411,10 +413,28 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
411413

412414
let mut krate = clean::krate(&mut ctxt);
413415

416+
if let Some(ref m) = krate.module {
417+
if let None | Some("") = m.doc_value() {
418+
let help = "The following guide may be of use:\n\
419+
https://doc.rust-lang.org/nightly/rustdoc/how-to-write-documentation\
420+
.html";
421+
tcx.struct_lint_node(
422+
rustc_lint::builtin::MISSING_CRATE_LEVEL_DOCS,
423+
ctxt.as_local_hir_id(m.def_id).unwrap(),
424+
|lint| {
425+
let mut diag = lint.build(
426+
"no documentation found for this crate's top-level module",
427+
);
428+
diag.help(help);
429+
diag.emit();
430+
},
431+
);
432+
}
433+
}
434+
414435
fn report_deprecated_attr(name: &str, diag: &rustc_errors::Handler) {
415436
let mut msg = diag.struct_warn(&format!(
416-
"the `#![doc({})]` attribute is \
417-
considered deprecated",
437+
"the `#![doc({})]` attribute is considered deprecated",
418438
name
419439
));
420440
msg.warn(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![deny(missing_crate_level_docs)]
2+
3+
pub fn foo() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: no documentation found for this crate's top-level module
2+
|
3+
note: the lint level is defined here
4+
--> $DIR/no-crate-level-doc-lint.rs:1:9
5+
|
6+
LL | #![deny(missing_crate_level_docs)]
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^
8+
= help: The following guide may be of use:
9+
https://doc.rust-lang.org/nightly/rustdoc/how-to-write-documentation.html
10+
11+
error: aborting due to previous error
12+

0 commit comments

Comments
 (0)