Skip to content

Commit f455587

Browse files
Set the limit of characters to 200 and don't run the lint on private items unless config allows it
1 parent 855a9d1 commit f455587

6 files changed

+37
-18
lines changed

clippy_lints/src/doc/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,13 @@ impl<'tcx> LateLintPass<'tcx> for Documentation {
493493

494494
match cx.tcx.hir_node(cx.last_node_with_lint_attrs) {
495495
Node::Item(item) => {
496-
too_long_first_doc_paragraph::check(cx, attrs, item.kind, headers.first_paragraph_len);
496+
too_long_first_doc_paragraph::check(
497+
cx,
498+
item,
499+
attrs,
500+
headers.first_paragraph_len,
501+
self.check_private_items,
502+
);
497503
match item.kind {
498504
ItemKind::Fn(sig, _, body_id) => {
499505
if !(is_entrypoint_fn(cx, item.owner_id.to_def_id())
@@ -627,9 +633,8 @@ fn check_attrs(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &[
627633
acc
628634
});
629635
doc.pop();
630-
let doc = doc.trim();
631636

632-
if doc.is_empty() {
637+
if doc.trim().is_empty() {
633638
if let Some(span) = span_of_fragments(&fragments) {
634639
span_lint_and_help(
635640
cx,
@@ -653,7 +658,7 @@ fn check_attrs(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &[
653658
cx,
654659
valid_idents,
655660
parser.into_offset_iter(),
656-
doc,
661+
&doc,
657662
Fragments {
658663
fragments: &fragments,
659664
doc: &doc,

clippy_lints/src/doc/too_long_first_doc_paragraph.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_ast::ast::Attribute;
22
use rustc_errors::Applicability;
3-
use rustc_hir::ItemKind;
3+
use rustc_hir::{Item, ItemKind};
44
use rustc_lint::LateContext;
55

66
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
@@ -10,13 +10,17 @@ use super::TOO_LONG_FIRST_DOC_PARAGRAPH;
1010

1111
pub(super) fn check(
1212
cx: &LateContext<'_>,
13+
item: &Item<'_>,
1314
attrs: &[Attribute],
14-
item_kind: ItemKind<'_>,
1515
mut first_paragraph_len: usize,
16+
check_private_items: bool,
1617
) {
17-
if first_paragraph_len <= 100
18+
if !check_private_items && !cx.effective_visibilities.is_exported(item.owner_id.def_id) {
19+
return;
20+
}
21+
if first_paragraph_len <= 200
1822
|| !matches!(
19-
item_kind,
23+
item.kind,
2024
ItemKind::Static(..)
2125
| ItemKind::Const(..)
2226
| ItemKind::Fn(..)
@@ -32,6 +36,7 @@ pub(super) fn check(
3236
{
3337
return;
3438
}
39+
3540
let mut spans = Vec::new();
3641
let mut should_suggest_empty_doc = false;
3742

@@ -42,7 +47,7 @@ pub(super) fn check(
4247
let doc = doc.trim();
4348
if spans.len() == 1 {
4449
// We make this suggestion only if the first doc line ends with a punctuation
45-
// because if might just need to add an empty line with `///`.
50+
// because it might just need to add an empty line with `///`.
4651
should_suggest_empty_doc = doc.ends_with('.') || doc.ends_with('!') || doc.ends_with('?');
4752
}
4853
let len = doc.chars().count();
@@ -68,7 +73,7 @@ pub(super) fn check(
6873
|diag| {
6974
diag.span_suggestion(
7075
new_span,
71-
"add",
76+
"add an empty line",
7277
format!("{snippet}///\n"),
7378
Applicability::MachineApplicable,
7479
);

tests/ui/too_long_first_doc_paragraph-fix.fixed

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
///
55
/// A much longer explanation that goes into a lot more detail about
66
/// how the thing works, possibly with doclinks and so one,
7-
/// and probably spanning a many rows.
8-
struct Foo;
7+
/// and probably spanning a many rows. Blablabla, it needs to be over
8+
/// 200 characters so I needed to write something longeeeeeeer.
9+
pub struct Foo;

tests/ui/too_long_first_doc_paragraph-fix.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
/// A very short summary.
44
/// A much longer explanation that goes into a lot more detail about
55
/// how the thing works, possibly with doclinks and so one,
6-
/// and probably spanning a many rows.
7-
struct Foo;
6+
/// and probably spanning a many rows. Blablabla, it needs to be over
7+
/// 200 characters so I needed to write something longeeeeeeer.
8+
pub struct Foo;

tests/ui/too_long_first_doc_paragraph-fix.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ error: first doc comment paragraph is too long
44
LL | / /// A very short summary.
55
LL | | /// A much longer explanation that goes into a lot more detail about
66
LL | | /// how the thing works, possibly with doclinks and so one,
7-
LL | | /// and probably spanning a many rows.
7+
LL | | /// and probably spanning a many rows. Blablabla, it needs to be over
8+
LL | | /// 200 characters so I needed to write something longeeeeeeer.
89
| |_
910
|
1011
= note: `-D clippy::too-long-first-doc-paragraph` implied by `-D warnings`

tests/ui/too_long_first_doc_paragraph.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ impl Bar {}
1919
/// Nunc turpis nunc, lacinia
2020
/// a dolor in, pellentesque aliquet enim. Cras nec maximus sem. Mauris arcu libero,
2121
/// gravida non lacinia at, rhoncus eu lacus.
22-
enum Enum {
22+
pub enum Enum {
2323
A,
2424
}
2525

2626
/// Lorem
2727
/// ipsum dolor sit amet, consectetur adipiscing elit. Nunc turpis nunc, lacinia
2828
/// a dolor in, pellentesque aliquet enim. Cras nec maximus sem. Mauris arcu libero,
2929
/// gravida non lacinia at, rhoncus eu lacus.
30-
union Union {
30+
pub union Union {
3131
a: u8,
3232
b: u8,
3333
}
@@ -37,11 +37,17 @@ union Union {
3737
/// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc turpis nunc, lacinia
3838
/// a dolor in, pellentesque aliquet enim. Cras nec maximus sem. Mauris arcu libero,
3939
/// gravida non lacinia at, rhoncus eu lacus.
40-
union Union2 {
40+
pub union Union2 {
4141
a: u8,
4242
b: u8,
4343
}
4444

45+
// Should not warn! (not public)
46+
/// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc turpis nunc, lacinia
47+
/// a dolor in, pellentesque aliquet enim. Cras nec maximus sem. Mauris arcu libero,
48+
/// gravida non lacinia at, rhoncus eu lacus.
49+
fn f() {}
50+
4551
fn main() {
4652
// test code goes here
4753
}

0 commit comments

Comments
 (0)