Skip to content

Commit ddd1a86

Browse files
committed
Auto merge of #13573 - y21:issue13427, r=Centri3
Don't lint unnamed consts and nested items within functions in `missing_docs_in_private_items` With this change we no longer require doc comments for `const _: ()` items as well as nested items in functions or other bodies. In both of those cases, rustdoc generates no documentation even with `--document-private-items`. Fixes #13427 (first commit) Fixes #13298 (second commit) cc #5736 (comment) changelog: [`missing_docs_in_private_items`]: avoid linting in more cases where rustdoc generates no documentation
2 parents 52b8324 + b3bf128 commit ddd1a86

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

clippy_lints/src/missing_doc.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ use clippy_utils::is_from_proc_macro;
1212
use clippy_utils::source::SpanRangeExt;
1313
use rustc_ast::ast::{self, MetaItem, MetaItemKind};
1414
use rustc_hir as hir;
15+
use rustc_hir::def::DefKind;
1516
use rustc_hir::def_id::LocalDefId;
1617
use rustc_lint::{LateContext, LateLintPass, LintContext};
1718
use rustc_middle::ty::Visibility;
1819
use rustc_session::impl_lint_pass;
1920
use rustc_span::def_id::CRATE_DEF_ID;
21+
use rustc_span::symbol::kw;
2022
use rustc_span::{Span, sym};
2123

2224
declare_clippy_lint! {
@@ -110,6 +112,21 @@ impl MissingDoc {
110112
return;
111113
}
112114

115+
if let Some(parent_def_id) = cx.tcx.opt_parent(def_id.to_def_id())
116+
&& let DefKind::AnonConst
117+
| DefKind::AssocConst
118+
| DefKind::AssocFn
119+
| DefKind::Closure
120+
| DefKind::Const
121+
| DefKind::Fn
122+
| DefKind::InlineConst
123+
| DefKind::Static { .. }
124+
| DefKind::SyntheticCoroutineBody = cx.tcx.def_kind(parent_def_id)
125+
{
126+
// Nested item has no generated documentation, so it doesn't need to be documented.
127+
return;
128+
}
129+
113130
let has_doc = attrs
114131
.iter()
115132
.any(|a| a.doc_str().is_some() || Self::has_include(a.meta()))
@@ -184,8 +201,12 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
184201
}
185202
}
186203
},
187-
hir::ItemKind::Const(..)
188-
| hir::ItemKind::Enum(..)
204+
hir::ItemKind::Const(..) => {
205+
if it.ident.name == kw::Underscore {
206+
note_prev_span_then_ret!(self.prev_span, it.span);
207+
}
208+
},
209+
hir::ItemKind::Enum(..)
189210
| hir::ItemKind::Macro(..)
190211
| hir::ItemKind::Mod(..)
191212
| hir::ItemKind::Static(..)

tests/ui/missing_doc.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ with_span!(span pub fn foo_pm() {});
116116
with_span!(span pub static FOO_PM: u32 = 0;);
117117
with_span!(span pub const FOO2_PM: u32 = 0;);
118118

119+
// Don't lint unnamed constants
120+
const _: () = ();
121+
122+
fn issue13298() {
123+
// Rustdoc doesn't generate documentation for items within other items like fns or consts
124+
const MSG: &str = "Hello, world!";
125+
}
126+
119127
// issue #12197
120128
// Undocumented field originated inside of spanned proc-macro attribute
121129
/// Some dox for struct.

tests/ui/missing_doc.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,14 @@ error: missing documentation for a function
8888
LL | fn also_undocumented2() {}
8989
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
9090

91-
error: aborting due to 13 previous errors
91+
error: missing documentation for a function
92+
--> tests/ui/missing_doc.rs:122:1
93+
|
94+
LL | / fn issue13298() {
95+
LL | | // Rustdoc doesn't generate documentation for items within other items like fns or consts
96+
LL | | const MSG: &str = "Hello, world!";
97+
LL | | }
98+
| |_^
99+
100+
error: aborting due to 14 previous errors
92101

0 commit comments

Comments
 (0)