Skip to content

Commit 1cd17ad

Browse files
committed
Auto merge of #88745 - hnj2:allow-trait-impl-missing-code, r=GuillaumeGomez
Allow missing code examples in trait impls. Excludes Trait implementations from the items that need to have doc code examples when using the `rustdoc::missing_doc_code_examples` lint. For details see #88741 fixes #88741 r? `@jyn514`
2 parents 61a1029 + c86c634 commit 1cd17ad

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/librustdoc/passes/doc_test_lints.rs

+20
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::core::DocContext;
1010
use crate::fold::DocFolder;
1111
use crate::html::markdown::{find_testable_code, ErrorCodes, Ignore, LangString};
1212
use crate::visit_ast::inherits_doc_hidden;
13+
use rustc_hir as hir;
1314
use rustc_middle::lint::LintLevelSource;
1415
use rustc_session::lint;
1516
use rustc_span::symbol::sym;
@@ -67,13 +68,32 @@ crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> boo
6768
| clean::ImportItem(_)
6869
| clean::PrimitiveItem(_)
6970
| clean::KeywordItem(_)
71+
// check for trait impl
72+
| clean::ImplItem(clean::Impl { trait_: Some(_), .. })
7073
)
7174
{
7275
return false;
7376
}
77+
7478
// The `expect_def_id()` should be okay because `local_def_id_to_hir_id`
7579
// would presumably panic if a fake `DefIndex` were passed.
7680
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(item.def_id.expect_def_id().expect_local());
81+
82+
// check if parent is trait impl
83+
if let Some(parent_hir_id) = cx.tcx.hir().find_parent_node(hir_id) {
84+
if let Some(parent_node) = cx.tcx.hir().find(parent_hir_id) {
85+
if matches!(
86+
parent_node,
87+
hir::Node::Item(hir::Item {
88+
kind: hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }),
89+
..
90+
})
91+
) {
92+
return false;
93+
}
94+
}
95+
}
96+
7797
if cx.tcx.hir().attrs(hir_id).lists(sym::doc).has_word(sym::hidden)
7898
|| inherits_doc_hidden(cx.tcx, hir_id)
7999
{

src/test/rustdoc-ui/lint-missing-doc-code-example.rs

+7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ pub union Union {
7070
b: f32,
7171
}
7272

73+
// no code example and it's fine!
74+
impl Clone for Struct {
75+
fn clone(&self) -> Self {
76+
Self { field: self.field }
77+
}
78+
}
79+
7380

7481
#[doc(hidden)]
7582
pub mod foo {

0 commit comments

Comments
 (0)