Skip to content

Commit 8ed4add

Browse files
Add new check_private_items config used in MISSING_SAFETY_DOC, UNNECESSARY_SAFETY_DOC, MISSING_PANICS_DOC and MISSING_ERRORS_DOC lints
1 parent 6eb935a commit 8ed4add

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

clippy_config/src/conf.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,10 @@ define_Conf! {
543543
/// for _ in &mut *rmvec {}
544544
/// ```
545545
(enforce_iter_loop_reborrow: bool = false),
546+
/// Lint: MISSING_SAFETY_DOC, UNNECESSARY_SAFETY_DOC, MISSING_PANICS_DOC, MISSING_ERRORS_DOC
547+
///
548+
/// Whether to also run the listed lints on private items.
549+
(check_private_items: bool = false),
546550
}
547551

548552
/// Search for the configuration file.

clippy_lints/src/doc.rs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,15 @@ declare_clippy_lint! {
314314
pub struct DocMarkdown {
315315
valid_idents: FxHashSet<String>,
316316
in_trait_impl: bool,
317+
check_private_items: bool,
317318
}
318319

319320
impl DocMarkdown {
320-
pub fn new(valid_idents: &[String]) -> Self {
321+
pub fn new(valid_idents: &[String], check_private_items: bool) -> Self {
321322
Self {
322323
valid_idents: valid_idents.iter().cloned().collect(),
323324
in_trait_impl: false,
325+
check_private_items,
324326
}
325327
}
326328
}
@@ -357,7 +359,15 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
357359
panic_span: None,
358360
};
359361
fpu.visit_expr(body.value);
360-
lint_for_missing_headers(cx, item.owner_id, sig, headers, Some(body_id), fpu.panic_span);
362+
lint_for_missing_headers(
363+
cx,
364+
item.owner_id,
365+
sig,
366+
headers,
367+
Some(body_id),
368+
fpu.panic_span,
369+
self.check_private_items,
370+
);
361371
}
362372
},
363373
hir::ItemKind::Impl(impl_) => {
@@ -395,7 +405,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
395405
};
396406
if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind {
397407
if !in_external_macro(cx.tcx.sess, item.span) {
398-
lint_for_missing_headers(cx, item.owner_id, sig, headers, None, None);
408+
lint_for_missing_headers(cx, item.owner_id, sig, headers, None, None, self.check_private_items);
399409
}
400410
}
401411
}
@@ -416,7 +426,15 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
416426
panic_span: None,
417427
};
418428
fpu.visit_expr(body.value);
419-
lint_for_missing_headers(cx, item.owner_id, sig, headers, Some(body_id), fpu.panic_span);
429+
lint_for_missing_headers(
430+
cx,
431+
item.owner_id,
432+
sig,
433+
headers,
434+
Some(body_id),
435+
fpu.panic_span,
436+
self.check_private_items,
437+
);
420438
}
421439
}
422440
}
@@ -428,17 +446,19 @@ fn lint_for_missing_headers(
428446
headers: DocHeaders,
429447
body_id: Option<hir::BodyId>,
430448
panic_span: Option<Span>,
449+
check_private_items: bool,
431450
) {
432-
if !cx.effective_visibilities.is_exported(owner_id.def_id) {
451+
if !cx.effective_visibilities.is_exported(owner_id.def_id) && !check_private_items {
433452
return; // Private functions do not require doc comments
434453
}
435454

436455
// do not lint if any parent has `#[doc(hidden)]` attribute (#7347)
437-
if cx
438-
.tcx
439-
.hir()
440-
.parent_iter(owner_id.into())
441-
.any(|(id, _node)| is_doc_hidden(cx.tcx.hir().attrs(id)))
456+
if !check_private_items
457+
&& cx
458+
.tcx
459+
.hir()
460+
.parent_iter(owner_id.into())
461+
.any(|(id, _node)| is_doc_hidden(cx.tcx.hir().attrs(id)))
442462
{
443463
return;
444464
}

clippy_lints/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
562562
vec_box_size_threshold,
563563
verbose_bit_mask_threshold,
564564
warn_on_all_wildcard_imports,
565+
check_private_items,
565566

566567
blacklisted_names: _,
567568
cyclomatic_complexity_threshold: _,
@@ -745,7 +746,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
745746
avoid_breaking_exported_api,
746747
))
747748
});
748-
store.register_late_pass(move |_| Box::new(doc::DocMarkdown::new(doc_valid_idents)));
749+
store.register_late_pass(move |_| Box::new(doc::DocMarkdown::new(doc_valid_idents, check_private_items)));
749750
store.register_late_pass(|_| Box::new(neg_multiply::NegMultiply));
750751
store.register_late_pass(|_| Box::new(let_if_seq::LetIfSeq));
751752
store.register_late_pass(|_| Box::new(mixed_read_write_in_expression::EvalOrderDependence));

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
2121
await-holding-invalid-types
2222
blacklisted-names
2323
cargo-ignore-publish
24+
check-private-items
2425
cognitive-complexity-threshold
2526
cyclomatic-complexity-threshold
2627
disallowed-macros
@@ -95,6 +96,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
9596
await-holding-invalid-types
9697
blacklisted-names
9798
cargo-ignore-publish
99+
check-private-items
98100
cognitive-complexity-threshold
99101
cyclomatic-complexity-threshold
100102
disallowed-macros

0 commit comments

Comments
 (0)