Skip to content

Commit 4d56366

Browse files
committed
Auto merge of rust-lang#11842 - GuillaumeGomez:check_private_items-config, r=blyxyas
Add new `check_private_items` config Fixes rust-lang/rust-clippy#11742. It adds a new configuration which allows for `MISSING_SAFETY_DOC`, `UNNECESSARY_SAFETY_DOC`, `MISSING_PANICS_DOC` and `MISSING_ERRORS_DOC` lints to run on private items. changelog: [`missing_safety_doc`], [`unnecessary_safety_doc`], [`missing_panics_doc`], [`missing_errors_doc`]: Added the [`check-private-items`] configuration to enable lints on private items. [rust-lang#11842](rust-lang/rust-clippy#11842) r? `@blyxyas`
2 parents 72c0d80 + 5cdda53 commit 4d56366

File tree

10 files changed

+171
-11
lines changed

10 files changed

+171
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5740,4 +5740,5 @@ Released 2018-09-13
57405740
[`absolute-paths-allowed-crates`]: https://doc.rust-lang.org/clippy/lint_configuration.html#absolute-paths-allowed-crates
57415741
[`allowed-dotfiles`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allowed-dotfiles
57425742
[`enforce-iter-loop-reborrow`]: https://doc.rust-lang.org/clippy/lint_configuration.html#enforce-iter-loop-reborrow
5743+
[`check-private-items`]: https://doc.rust-lang.org/clippy/lint_configuration.html#check-private-items
57435744
<!-- end autogenerated links to configuration documentation -->

book/src/lint_configuration.md

+13
Original file line numberDiff line numberDiff line change
@@ -791,3 +791,16 @@ for _ in &mut *rmvec {}
791791
* [`explicit_iter_loop`](https://rust-lang.github.io/rust-clippy/master/index.html#explicit_iter_loop)
792792

793793

794+
## `check-private-items`
795+
796+
797+
**Default Value:** `false`
798+
799+
---
800+
**Affected lints:**
801+
* [`missing_safety_doc`](https://rust-lang.github.io/rust-clippy/master/index.html#missing_safety_doc)
802+
* [`unnecessary_safety_doc`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_safety_doc)
803+
* [`missing_panics_doc`](https://rust-lang.github.io/rust-clippy/master/index.html#missing_panics_doc)
804+
* [`missing_errors_doc`](https://rust-lang.github.io/rust-clippy/master/index.html#missing_errors_doc)
805+
806+

clippy_config/src/conf.rs

+4
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/missing_headers.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ pub fn check(
1515
headers: DocHeaders,
1616
body_id: Option<BodyId>,
1717
panic_span: Option<Span>,
18+
check_private_items: bool,
1819
) {
19-
if !cx.effective_visibilities.is_exported(owner_id.def_id) {
20+
if !check_private_items && !cx.effective_visibilities.is_exported(owner_id.def_id) {
2021
return; // Private functions do not require doc comments
2122
}
2223

2324
// do not lint if any parent has `#[doc(hidden)]` attribute (#7347)
24-
if cx
25-
.tcx
26-
.hir()
27-
.parent_iter(owner_id.into())
28-
.any(|(id, _node)| is_doc_hidden(cx.tcx.hir().attrs(id)))
25+
if !check_private_items
26+
&& cx
27+
.tcx
28+
.hir()
29+
.parent_iter(owner_id.into())
30+
.any(|(id, _node)| is_doc_hidden(cx.tcx.hir().attrs(id)))
2931
{
3032
return;
3133
}

clippy_lints/src/doc/mod.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,15 @@ declare_clippy_lint! {
310310
pub struct DocMarkdown {
311311
valid_idents: FxHashSet<String>,
312312
in_trait_impl: bool,
313+
check_private_items: bool,
313314
}
314315

315316
impl DocMarkdown {
316-
pub fn new(valid_idents: &[String]) -> Self {
317+
pub fn new(valid_idents: &[String], check_private_items: bool) -> Self {
317318
Self {
318319
valid_idents: valid_idents.iter().cloned().collect(),
319320
in_trait_impl: false,
321+
check_private_items,
320322
}
321323
}
322324
}
@@ -349,7 +351,15 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
349351
let body = cx.tcx.hir().body(body_id);
350352

351353
let panic_span = FindPanicUnwrap::find_span(cx, cx.tcx.typeck(item.owner_id), body.value);
352-
missing_headers::check(cx, item.owner_id, sig, headers, Some(body_id), panic_span);
354+
missing_headers::check(
355+
cx,
356+
item.owner_id,
357+
sig,
358+
headers,
359+
Some(body_id),
360+
panic_span,
361+
self.check_private_items,
362+
);
353363
}
354364
},
355365
hir::ItemKind::Impl(impl_) => {
@@ -387,7 +397,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
387397
};
388398
if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind {
389399
if !in_external_macro(cx.tcx.sess, item.span) {
390-
missing_headers::check(cx, item.owner_id, sig, headers, None, None);
400+
missing_headers::check(cx, item.owner_id, sig, headers, None, None, self.check_private_items);
391401
}
392402
}
393403
}
@@ -404,7 +414,15 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
404414
let body = cx.tcx.hir().body(body_id);
405415

406416
let panic_span = FindPanicUnwrap::find_span(cx, cx.tcx.typeck(item.owner_id), body.value);
407-
missing_headers::check(cx, item.owner_id, sig, headers, Some(body_id), panic_span);
417+
missing_headers::check(
418+
cx,
419+
item.owner_id,
420+
sig,
421+
headers,
422+
Some(body_id),
423+
panic_span,
424+
self.check_private_items,
425+
);
408426
}
409427
}
410428
}

clippy_lints/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
563563
vec_box_size_threshold,
564564
verbose_bit_mask_threshold,
565565
warn_on_all_wildcard_imports,
566+
check_private_items,
566567

567568
blacklisted_names: _,
568569
cyclomatic_complexity_threshold: _,
@@ -746,7 +747,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
746747
avoid_breaking_exported_api,
747748
))
748749
});
749-
store.register_late_pass(move |_| Box::new(doc::DocMarkdown::new(doc_valid_idents)));
750+
store.register_late_pass(move |_| Box::new(doc::DocMarkdown::new(doc_valid_idents, check_private_items)));
750751
store.register_late_pass(|_| Box::new(neg_multiply::NegMultiply));
751752
store.register_late_pass(|_| Box::new(let_if_seq::LetIfSeq));
752753
store.register_late_pass(|_| Box::new(mixed_read_write_in_expression::EvalOrderDependence));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
check-private-items = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#![deny(
2+
clippy::unnecessary_safety_doc,
3+
clippy::missing_errors_doc,
4+
clippy::missing_panics_doc
5+
)]
6+
7+
/// This is a private function, skip to match behavior with `missing_safety_doc`.
8+
///
9+
/// # Safety
10+
///
11+
/// Boo!
12+
fn you_dont_see_me() {
13+
//~^ ERROR: safe function's docs have unnecessary `# Safety` section
14+
unimplemented!();
15+
}
16+
17+
mod private_mod {
18+
/// This is public but unexported function.
19+
///
20+
/// # Safety
21+
///
22+
/// Very safe!
23+
pub fn only_crate_wide_accessible() -> Result<(), ()> {
24+
//~^ ERROR: safe function's docs have unnecessary `# Safety` section
25+
//~| ERROR: docs for function returning `Result` missing `# Errors` section
26+
unimplemented!();
27+
}
28+
}
29+
30+
pub struct S;
31+
32+
impl S {
33+
/// Private, fine again to stay consistent with `missing_safety_doc`.
34+
///
35+
/// # Safety
36+
///
37+
/// Unnecessary!
38+
fn private(&self) {
39+
//~^ ERROR: safe function's docs have unnecessary `# Safety` section
40+
//~| ERROR: docs for function which may panic missing `# Panics` section
41+
panic!();
42+
}
43+
}
44+
45+
#[doc(hidden)]
46+
pub mod __macro {
47+
pub struct T;
48+
impl T {
49+
pub unsafe fn f() {}
50+
//~^ ERROR: unsafe function's docs miss `# Safety` section
51+
}
52+
}
53+
54+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
error: safe function's docs have unnecessary `# Safety` section
2+
--> $DIR/doc_lints.rs:12:1
3+
|
4+
LL | fn you_dont_see_me() {
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/doc_lints.rs:2:5
9+
|
10+
LL | clippy::unnecessary_safety_doc,
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: safe function's docs have unnecessary `# Safety` section
14+
--> $DIR/doc_lints.rs:23:5
15+
|
16+
LL | pub fn only_crate_wide_accessible() -> Result<(), ()> {
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
19+
error: docs for function returning `Result` missing `# Errors` section
20+
--> $DIR/doc_lints.rs:23:5
21+
|
22+
LL | pub fn only_crate_wide_accessible() -> Result<(), ()> {
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
|
25+
note: the lint level is defined here
26+
--> $DIR/doc_lints.rs:3:5
27+
|
28+
LL | clippy::missing_errors_doc,
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
31+
error: safe function's docs have unnecessary `# Safety` section
32+
--> $DIR/doc_lints.rs:38:5
33+
|
34+
LL | fn private(&self) {
35+
| ^^^^^^^^^^^^^^^^^
36+
37+
error: docs for function which may panic missing `# Panics` section
38+
--> $DIR/doc_lints.rs:38:5
39+
|
40+
LL | fn private(&self) {
41+
| ^^^^^^^^^^^^^^^^^
42+
|
43+
note: first possible panic found here
44+
--> $DIR/doc_lints.rs:41:9
45+
|
46+
LL | panic!();
47+
| ^^^^^^^^
48+
note: the lint level is defined here
49+
--> $DIR/doc_lints.rs:4:5
50+
|
51+
LL | clippy::missing_panics_doc
52+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
53+
54+
error: unsafe function's docs miss `# Safety` section
55+
--> $DIR/doc_lints.rs:49:9
56+
|
57+
LL | pub unsafe fn f() {}
58+
| ^^^^^^^^^^^^^^^^^
59+
|
60+
= note: `-D clippy::missing-safety-doc` implied by `-D warnings`
61+
= help: to override `-D warnings` add `#[allow(clippy::missing_safety_doc)]`
62+
63+
error: aborting due to 6 previous errors
64+

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

+2
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)