-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
too_long_first_doc_paragraph
first paragraph lint
- Loading branch information
1 parent
345c94c
commit 760d03e
Showing
9 changed files
with
270 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use rustc_ast::ast::Attribute; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::ItemKind; | ||
use rustc_lint::LateContext; | ||
|
||
use clippy_utils::diagnostics::{span_lint, span_lint_and_then}; | ||
use clippy_utils::source::snippet_opt; | ||
|
||
use super::TOO_LONG_FIRST_DOC_PARAGRAPH; | ||
|
||
pub(super) fn check( | ||
cx: &LateContext<'_>, | ||
attrs: &[Attribute], | ||
item_kind: ItemKind<'_>, | ||
mut first_paragraph_len: usize, | ||
) { | ||
if first_paragraph_len <= 100 | ||
|| !matches!( | ||
item_kind, | ||
ItemKind::Static(..) | ||
| ItemKind::Const(..) | ||
| ItemKind::Fn(..) | ||
| ItemKind::Macro(..) | ||
| ItemKind::Mod(..) | ||
| ItemKind::TyAlias(..) | ||
| ItemKind::Enum(..) | ||
| ItemKind::Struct(..) | ||
| ItemKind::Union(..) | ||
| ItemKind::Trait(..) | ||
| ItemKind::TraitAlias(..) | ||
) | ||
{ | ||
return; | ||
} | ||
let mut spans = Vec::new(); | ||
let mut should_suggest_empty_doc = false; | ||
|
||
for attr in attrs { | ||
if let Some(doc) = attr.doc_str() { | ||
spans.push(attr.span); | ||
let doc = doc.as_str(); | ||
let doc = doc.trim(); | ||
if spans.len() == 1 { | ||
// We make this suggestion only if the first doc line ends with a punctuation | ||
// because if might just need to add an empty line with `///`. | ||
should_suggest_empty_doc = doc.ends_with('.') || doc.ends_with('!') || doc.ends_with('?'); | ||
} | ||
let len = doc.chars().count(); | ||
if len >= first_paragraph_len { | ||
break; | ||
} | ||
first_paragraph_len -= len; | ||
} | ||
} | ||
|
||
let &[first_span, .., last_span] = spans.as_slice() else { return }; | ||
|
||
if should_suggest_empty_doc | ||
&& let Some(second_span) = spans.get(1) | ||
&& let new_span = first_span.with_hi(second_span.lo()).with_lo(first_span.hi()) | ||
&& let Some(snippet) = snippet_opt(cx, new_span) | ||
{ | ||
span_lint_and_then( | ||
cx, | ||
TOO_LONG_FIRST_DOC_PARAGRAPH, | ||
first_span.with_hi(last_span.lo()), | ||
"first doc comment paragraph is too long", | ||
|diag| { | ||
diag.span_suggestion( | ||
new_span, | ||
"add", | ||
format!("{snippet}///\n"), | ||
Applicability::MachineApplicable, | ||
); | ||
}, | ||
); | ||
return; | ||
} | ||
span_lint( | ||
cx, | ||
TOO_LONG_FIRST_DOC_PARAGRAPH, | ||
first_span.with_hi(last_span.lo()), | ||
"first doc comment paragraph is too long", | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#![warn(clippy::too_long_first_doc_paragraph)] | ||
|
||
/// A very short summary. | ||
/// | ||
/// A much longer explanation that goes into a lot more detail about | ||
/// how the thing works, possibly with doclinks and so one, | ||
/// and probably spanning a many rows. | ||
struct Foo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#![warn(clippy::too_long_first_doc_paragraph)] | ||
|
||
/// A very short summary. | ||
/// A much longer explanation that goes into a lot more detail about | ||
/// how the thing works, possibly with doclinks and so one, | ||
/// and probably spanning a many rows. | ||
struct Foo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error: first doc comment paragraph is too long | ||
--> tests/ui/too_long_first_doc_paragraph-fix.rs:3:1 | ||
| | ||
LL | / /// A very short summary. | ||
LL | | /// A much longer explanation that goes into a lot more detail about | ||
LL | | /// how the thing works, possibly with doclinks and so one, | ||
LL | | /// and probably spanning a many rows. | ||
| |_ | ||
| | ||
= note: `-D clippy::too-long-first-doc-paragraph` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::too_long_first_doc_paragraph)]` | ||
help: add | ||
| | ||
LL ~ /// A very short summary. | ||
LL + /// | ||
| | ||
|
||
error: aborting due to 1 previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//@no-rustfix | ||
|
||
#![warn(clippy::too_long_first_doc_paragraph)] | ||
|
||
/// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc turpis nunc, lacinia | ||
/// a dolor in, pellentesque aliquet enim. Cras nec maximus sem. Mauris arcu libero, | ||
/// gravida non lacinia at, rhoncus eu lacus. | ||
pub struct Bar; | ||
|
||
// Should not warn! (not an item visible on mod page) | ||
/// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc turpis nunc, lacinia | ||
/// a dolor in, pellentesque aliquet enim. Cras nec maximus sem. Mauris arcu libero, | ||
/// gravida non lacinia at, rhoncus eu lacus. | ||
impl Bar {} | ||
|
||
// Should not warn! (less than 80 characters) | ||
/// Lorem ipsum dolor sit amet, consectetur adipiscing elit. | ||
/// | ||
/// Nunc turpis nunc, lacinia | ||
/// a dolor in, pellentesque aliquet enim. Cras nec maximus sem. Mauris arcu libero, | ||
/// gravida non lacinia at, rhoncus eu lacus. | ||
enum Enum { | ||
A, | ||
} | ||
|
||
/// Lorem | ||
/// ipsum dolor sit amet, consectetur adipiscing elit. Nunc turpis nunc, lacinia | ||
/// a dolor in, pellentesque aliquet enim. Cras nec maximus sem. Mauris arcu libero, | ||
/// gravida non lacinia at, rhoncus eu lacus. | ||
union Union { | ||
a: u8, | ||
b: u8, | ||
} | ||
|
||
// Should not warn! (title) | ||
/// # bla | ||
/// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc turpis nunc, lacinia | ||
/// a dolor in, pellentesque aliquet enim. Cras nec maximus sem. Mauris arcu libero, | ||
/// gravida non lacinia at, rhoncus eu lacus. | ||
union Union2 { | ||
a: u8, | ||
b: u8, | ||
} | ||
|
||
fn main() { | ||
// test code goes here | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: first doc comment paragraph is too long | ||
--> tests/ui/too_long_first_doc_paragraph.rs:5:1 | ||
| | ||
LL | / /// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc turpis nunc, lacinia | ||
LL | | /// a dolor in, pellentesque aliquet enim. Cras nec maximus sem. Mauris arcu libero, | ||
LL | | /// gravida non lacinia at, rhoncus eu lacus. | ||
| |_ | ||
| | ||
= note: `-D clippy::too-long-first-doc-paragraph` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::too_long_first_doc_paragraph)]` | ||
|
||
error: first doc comment paragraph is too long | ||
--> tests/ui/too_long_first_doc_paragraph.rs:26:1 | ||
| | ||
LL | / /// Lorem | ||
LL | | /// ipsum dolor sit amet, consectetur adipiscing elit. Nunc turpis nunc, lacinia | ||
LL | | /// a dolor in, pellentesque aliquet enim. Cras nec maximus sem. Mauris arcu libero, | ||
LL | | /// gravida non lacinia at, rhoncus eu lacus. | ||
| |_ | ||
|
||
error: aborting due to 2 previous errors | ||
|