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 https://github.com/rust-lang/rust-clippy/issues/5736#issuecomment-668524296

changelog: [`missing_docs_in_private_items`]: avoid linting in more cases where rustdoc generates no documentation
This commit is contained in:
bors 2024-11-03 09:10:22 +00:00
commit ddd1a86c66
3 changed files with 41 additions and 3 deletions

View File

@ -12,11 +12,13 @@
use clippy_utils::source::SpanRangeExt; use clippy_utils::source::SpanRangeExt;
use rustc_ast::ast::{self, MetaItem, MetaItemKind}; use rustc_ast::ast::{self, MetaItem, MetaItemKind};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::LocalDefId; use rustc_hir::def_id::LocalDefId;
use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::ty::Visibility; use rustc_middle::ty::Visibility;
use rustc_session::impl_lint_pass; use rustc_session::impl_lint_pass;
use rustc_span::def_id::CRATE_DEF_ID; use rustc_span::def_id::CRATE_DEF_ID;
use rustc_span::symbol::kw;
use rustc_span::{Span, sym}; use rustc_span::{Span, sym};
declare_clippy_lint! { declare_clippy_lint! {
@ -110,6 +112,21 @@ fn check_missing_docs_attrs(
return; return;
} }
if let Some(parent_def_id) = cx.tcx.opt_parent(def_id.to_def_id())
&& let DefKind::AnonConst
| DefKind::AssocConst
| DefKind::AssocFn
| DefKind::Closure
| DefKind::Const
| DefKind::Fn
| DefKind::InlineConst
| DefKind::Static { .. }
| DefKind::SyntheticCoroutineBody = cx.tcx.def_kind(parent_def_id)
{
// Nested item has no generated documentation, so it doesn't need to be documented.
return;
}
let has_doc = attrs let has_doc = attrs
.iter() .iter()
.any(|a| a.doc_str().is_some() || Self::has_include(a.meta())) .any(|a| a.doc_str().is_some() || Self::has_include(a.meta()))
@ -184,8 +201,12 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
} }
} }
}, },
hir::ItemKind::Const(..) hir::ItemKind::Const(..) => {
| hir::ItemKind::Enum(..) if it.ident.name == kw::Underscore {
note_prev_span_then_ret!(self.prev_span, it.span);
}
},
hir::ItemKind::Enum(..)
| hir::ItemKind::Macro(..) | hir::ItemKind::Macro(..)
| hir::ItemKind::Mod(..) | hir::ItemKind::Mod(..)
| hir::ItemKind::Static(..) | hir::ItemKind::Static(..)

View File

@ -116,6 +116,14 @@ fn main() {}
with_span!(span pub static FOO_PM: u32 = 0;); with_span!(span pub static FOO_PM: u32 = 0;);
with_span!(span pub const FOO2_PM: u32 = 0;); with_span!(span pub const FOO2_PM: u32 = 0;);
// Don't lint unnamed constants
const _: () = ();
fn issue13298() {
// Rustdoc doesn't generate documentation for items within other items like fns or consts
const MSG: &str = "Hello, world!";
}
// issue #12197 // issue #12197
// Undocumented field originated inside of spanned proc-macro attribute // Undocumented field originated inside of spanned proc-macro attribute
/// Some dox for struct. /// Some dox for struct.

View File

@ -88,5 +88,14 @@ error: missing documentation for a function
LL | fn also_undocumented2() {} LL | fn also_undocumented2() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 13 previous errors error: missing documentation for a function
--> tests/ui/missing_doc.rs:122:1
|
LL | / fn issue13298() {
LL | | // Rustdoc doesn't generate documentation for items within other items like fns or consts
LL | | const MSG: &str = "Hello, world!";
LL | | }
| |_^
error: aborting due to 14 previous errors