Skip to content

Commit 73dd05c

Browse files
add allow_unused config to missing_docs_in_private_items (rust-lang#14453)
fixes rust-lang#14413 add allow_unused config to missing_docs_in_private_items for underscored field changelog: [`missing_docs_in_private_items`]: add allow_unused config to missing_docs_in_private_items for underscored field Explaination (quoted from the issue's author): When writing structures that must adhere to a specific format (such as memory mapped I/O structures) there are inevitably fields that are "reserved" by specifications and thus need no documentation. In cases where private docs are preferred but reserved fields need no explanation, having to #[allow/expect] this lint removes the ability to check for otherwise used fields' documentation.
2 parents cf6bebb + 64880ca commit 73dd05c

File tree

8 files changed

+90
-4
lines changed

8 files changed

+90
-4
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 an 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 an 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: 8 additions & 4 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 an 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
}
@@ -260,11 +263,12 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
260263
}
261264

262265
fn check_field_def(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::FieldDef<'_>) {
263-
if !sf.is_positional() {
266+
if !(sf.is_positional()
267+
|| is_from_proc_macro(cx, sf)
268+
|| self.allow_unused && sf.ident.as_str().starts_with('_'))
269+
{
264270
let attrs = cx.tcx.hir_attrs(sf.hir_id);
265-
if !is_from_proc_macro(cx, sf) {
266-
self.check_missing_docs_attrs(cx, sf.def_id, attrs, sf.span, "a", "struct field");
267-
}
271+
self.check_missing_docs_attrs(cx, sf.def_id, attrs, sf.span, "a", "struct field");
268272
}
269273
self.prev_span = Some(sf.span);
270274
}
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 an 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+
struct Test3 {
20+
//~^ missing_docs_in_private_items
21+
/// This field is documented although this is not mandatory
22+
_unused: i32, // This should not trigger a warning because it starts with an underscore
23+
field2: i32, //~ missing_docs_in_private_items
24+
}
25+
26+
fn main() {}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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: missing documentation for a struct
21+
--> tests/ui-toml/missing_docs_allow_unused/missing_docs_allow_unused.rs:19:1
22+
|
23+
LL | / struct Test3 {
24+
LL | |
25+
LL | | /// This field is documented although this is not mandatory
26+
LL | | _unused: i32, // This should not trigger a warning because it starts with an underscore
27+
LL | | field2: i32,
28+
LL | | }
29+
| |_^
30+
31+
error: missing documentation for a struct field
32+
--> tests/ui-toml/missing_docs_allow_unused/missing_docs_allow_unused.rs:23:5
33+
|
34+
LL | field2: i32,
35+
| ^^^^^^^^^^^
36+
37+
error: aborting due to 4 previous errors
38+

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)