Skip to content

Commit 6bed383

Browse files
committed
diagnostics: emit unused_doc_comments on fn nest
When a doc comment is nested within an item: fn foo() { /// doc comment fn bar() {} } When that happens, emit a warning, because they aren't rendered, even with --document-private-items warning: unused doc comment --> src/lib.rs:3:3 | 3 | /// nested fn | ^^^^^^^^^^^^^ 4 | pub fn bar() { | -------------- rustdoc does not generate documentation for items defined in functions | = help: use `//` for a plain comment = note: `#[warn(unused_doc_comments)]` (part of `#[warn(unused)]`) on by default
1 parent 8b95a26 commit 6bed383

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

compiler/rustc_lint/src/builtin.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,32 @@ impl EarlyLintPass for UnusedDocComment {
838838
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
839839
if let ast::ItemKind::ForeignMod(_) = item.kind {
840840
warn_if_doc(cx, item.span, "extern blocks", &item.attrs);
841+
} else if self.in_fn != 0 {
842+
warn_if_doc(cx, item.span, "items defined in functions", &item.attrs);
843+
fn warn_on_fields(cx: &EarlyContext<'_>, var_data: &VariantData) {
844+
for field in var_data.fields() {
845+
warn_if_doc(cx, field.span, "fields defined nested in functions", &field.attrs);
846+
}
847+
}
848+
if let ast::ItemKind::Struct(_, _, var_data) = &item.kind {
849+
warn_on_fields(cx, var_data);
850+
} else if let ast::ItemKind::Union(_, _, var_data) = &item.kind {
851+
warn_on_fields(cx, var_data);
852+
} else if let ast::ItemKind::Enum(_, _, def) = &item.kind {
853+
for var in &def.variants {
854+
warn_if_doc(cx, var.span, "variants defined nested in functions", &var.attrs);
855+
warn_on_fields(cx, &var.data);
856+
}
857+
}
858+
}
859+
if let ast::ItemKind::Fn(_) = item.kind {
860+
self.in_fn += 1;
861+
}
862+
}
863+
864+
fn check_item_post(&mut self, _: &EarlyContext<'_>, item: &ast::Item) {
865+
if let ast::ItemKind::Fn(_) = item.kind {
866+
self.in_fn -= 1;
841867
}
842868
}
843869
}

compiler/rustc_lint/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ early_lint_methods!(
184184
NonAsciiIdents: NonAsciiIdents,
185185
IncompleteInternalFeatures: IncompleteInternalFeatures,
186186
RedundantSemicolons: RedundantSemicolons,
187-
UnusedDocComment: UnusedDocComment,
187+
UnusedDocComment: UnusedDocComment::default(),
188188
Expr2024: Expr2024,
189189
Precedence: Precedence,
190190
DoubleNegations: DoubleNegations,

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! When removing a lint, make sure to also add a call to `register_removed` in
88
//! compiler/rustc_lint/src/lib.rs.
99
10-
use crate::{declare_lint, declare_lint_pass, fcw};
10+
use crate::{declare_lint, fcw, impl_lint_pass};
1111

1212
pub mod hardwired {
1313
use super::*;
@@ -3341,7 +3341,11 @@ declare_lint! {
33413341
};
33423342
}
33433343

3344-
declare_lint_pass!(UnusedDocComment => [UNUSED_DOC_COMMENTS]);
3344+
#[derive(Clone, Copy, Default)]
3345+
pub struct UnusedDocComment {
3346+
pub in_fn: usize,
3347+
}
3348+
impl_lint_pass!(UnusedDocComment => [UNUSED_DOC_COMMENTS]);
33453349

33463350
declare_lint! {
33473351
/// The `missing_abi` lint detects cases where the ABI is omitted from

0 commit comments

Comments
 (0)