Skip to content

Commit c468552

Browse files
committed
add allow_unused config to missing_docs_in_private_items
add allow_unused config to missing_docs_in_private_items for underscored field
1 parent 56f0182 commit c468552

File tree

8 files changed

+69
-0
lines changed

8 files changed

+69
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6435,6 +6435,7 @@ Released 2018-09-13
64356435
[`max-suggested-slice-pattern-length`]: https://doc.rust-lang.org/clippy/lint_configuration.html#max-suggested-slice-pattern-length
64366436
[`max-trait-bounds`]: https://doc.rust-lang.org/clippy/lint_configuration.html#max-trait-bounds
64376437
[`min-ident-chars-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#min-ident-chars-threshold
6438+
[`missing-docs-allow-unused`]: https://doc.rust-lang.org/clippy/lint_configuration.html#missing-docs-allow-unused
64386439
[`missing-docs-in-crate-items`]: https://doc.rust-lang.org/clippy/lint_configuration.html#missing-docs-in-crate-items
64396440
[`module-item-order-groupings`]: https://doc.rust-lang.org/clippy/lint_configuration.html#module-item-order-groupings
64406441
[`module-items-ordered-within-groupings`]: https://doc.rust-lang.org/clippy/lint_configuration.html#module-items-ordered-within-groupings

book/src/lint_configuration.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,16 @@ Minimum chars an ident can have, anything below or equal to this will be linted.
734734
* [`min_ident_chars`](https://rust-lang.github.io/rust-clippy/master/index.html#min_ident_chars)
735735

736736

737+
## `missing-docs-allow-unused`
738+
Whether to allow fields starting with underscore to skip documentation requirements
739+
740+
**Default Value:** `false`
741+
742+
---
743+
**Affected lints:**
744+
* [`missing_docs_in_private_items`](https://rust-lang.github.io/rust-clippy/master/index.html#missing_docs_in_private_items)
745+
746+
737747
## `missing-docs-in-crate-items`
738748
Whether to **only** check for missing documentation in items visible within the current
739749
crate. For example, `pub(crate)` items.

clippy_config/src/conf.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,9 @@ define_Conf! {
675675
/// Minimum chars an ident can have, anything below or equal to this will be linted.
676676
#[lints(min_ident_chars)]
677677
min_ident_chars_threshold: u64 = 1,
678+
/// Whether to allow fields starting with underscore to skip documentation requirements
679+
#[lints(missing_docs_in_private_items)]
680+
missing_docs_allow_unused: bool = false,
678681
/// Whether to **only** check for missing documentation in items visible within the current
679682
/// crate. For example, `pub(crate)` items.
680683
#[lints(missing_docs_in_private_items)]

clippy_lints/src/missing_doc.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ pub struct MissingDoc {
4848
/// Whether to **only** check for missing documentation in items visible within the current
4949
/// crate. For example, `pub(crate)` items.
5050
crate_items_only: bool,
51+
/// Whether to allow fields starting with underscore to skip documentation requirements
52+
allow_unused: bool,
5153
/// Stack of whether #[doc(hidden)] is set
5254
/// at each level which has lint attributes.
5355
doc_hidden_stack: Vec<bool>,
@@ -59,6 +61,7 @@ impl MissingDoc {
5961
pub fn new(conf: &'static Conf) -> Self {
6062
Self {
6163
crate_items_only: conf.missing_docs_in_crate_items,
64+
allow_unused: conf.missing_docs_allow_unused,
6265
doc_hidden_stack: vec![false],
6366
prev_span: None,
6467
}
@@ -88,6 +91,14 @@ impl MissingDoc {
8891
article: &'static str,
8992
desc: &'static str,
9093
) {
94+
// Skip checking if the item starts with underscore and allow_unused is enabled
95+
if self.allow_unused {
96+
if let Some(name) = cx.tcx.opt_item_name(def_id.to_def_id()) {
97+
if name.as_str().starts_with('_') {
98+
return;
99+
}
100+
}
101+
}
91102
// If we're building a test harness, then warning about
92103
// documentation is probably not really relevant right now.
93104
if cx.sess().opts.test {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
missing-docs-allow-unused = true
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//! Test file for missing_docs_in_private_items lint with allow_unused configuration
2+
#![warn(clippy::missing_docs_in_private_items)]
3+
#![allow(dead_code)]
4+
5+
/// A struct with some documented and undocumented fields
6+
struct Test {
7+
/// This field is documented
8+
field1: i32,
9+
_unused: i32, // This should not trigger a warning because it starts with underscore
10+
field3: i32, //~ missing_docs_in_private_items
11+
}
12+
13+
struct Test2 {
14+
//~^ missing_docs_in_private_items
15+
_field1: i32, // This should not trigger a warning
16+
_field2: i32, // This should not trigger a warning
17+
}
18+
19+
fn main() {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: missing documentation for a struct field
2+
--> tests/ui-toml/missing_docs_allow_unused/missing_docs_allow_unused.rs:10:5
3+
|
4+
LL | field3: i32,
5+
| ^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::missing-docs-in-private-items` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::missing_docs_in_private_items)]`
9+
10+
error: missing documentation for a struct
11+
--> tests/ui-toml/missing_docs_allow_unused/missing_docs_allow_unused.rs:13:1
12+
|
13+
LL | / struct Test2 {
14+
LL | |
15+
LL | | _field1: i32, // This should not trigger a warning
16+
LL | | _field2: i32, // This should not trigger a warning
17+
LL | | }
18+
| |_^
19+
20+
error: aborting due to 2 previous errors
21+

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
5757
max-suggested-slice-pattern-length
5858
max-trait-bounds
5959
min-ident-chars-threshold
60+
missing-docs-allow-unused
6061
missing-docs-in-crate-items
6162
module-item-order-groupings
6263
module-items-ordered-within-groupings
@@ -149,6 +150,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
149150
max-suggested-slice-pattern-length
150151
max-trait-bounds
151152
min-ident-chars-threshold
153+
missing-docs-allow-unused
152154
missing-docs-in-crate-items
153155
module-item-order-groupings
154156
module-items-ordered-within-groupings
@@ -241,6 +243,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
241243
max-suggested-slice-pattern-length
242244
max-trait-bounds
243245
min-ident-chars-threshold
246+
missing-docs-allow-unused
244247
missing-docs-in-crate-items
245248
module-item-order-groupings
246249
module-items-ordered-within-groupings

0 commit comments

Comments
 (0)