Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't lint unnamed consts and nested items within functions in missing_docs_in_private_items #13573

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions clippy_lints/src/missing_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ use clippy_utils::is_from_proc_macro;
use clippy_utils::source::SpanRangeExt;
use rustc_ast::ast::{self, MetaItem, MetaItemKind};
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::LocalDefId;
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::ty::Visibility;
use rustc_session::impl_lint_pass;
use rustc_span::def_id::CRATE_DEF_ID;
use rustc_span::symbol::kw;
use rustc_span::{Span, sym};

declare_clippy_lint! {
Expand Down Expand Up @@ -110,6 +112,21 @@ impl MissingDoc {
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
.iter()
.any(|a| a.doc_str().is_some() || Self::has_include(a.meta()))
Expand Down Expand Up @@ -184,8 +201,12 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
}
}
},
hir::ItemKind::Const(..)
| hir::ItemKind::Enum(..)
hir::ItemKind::Const(..) => {
if it.ident.name == kw::Underscore {
note_prev_span_then_ret!(self.prev_span, it.span);
}
},
hir::ItemKind::Enum(..)
| hir::ItemKind::Macro(..)
| hir::ItemKind::Mod(..)
| hir::ItemKind::Static(..)
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/missing_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ with_span!(span pub fn foo_pm() {});
with_span!(span pub static FOO_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
// Undocumented field originated inside of spanned proc-macro attribute
/// Some dox for struct.
Expand Down
11 changes: 10 additions & 1 deletion tests/ui/missing_doc.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,14 @@ error: missing documentation for a function
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