about summary refs log tree commit diff
diff options
context:
space:
mode:
authoryanglsh <yanglsh@shanghaitech.edu.cn>2025-02-22 19:04:25 +0800
committeryanglsh <yanglsh@shanghaitech.edu.cn>2025-02-22 19:05:07 +0800
commit2ad3520d514d41eeb892652ea8da95323e1242ed (patch)
treea068cb199a3733cdf8d2e5ece6f8439d7ac3be48
parente479a9ff959df44e8b582834d123705e4b3477e1 (diff)
downloadrust-2ad3520d514d41eeb892652ea8da95323e1242ed.tar.gz
rust-2ad3520d514d41eeb892652ea8da95323e1242ed.zip
fix: `too_long_first_doc_paragraph` suggests wrongly when first line too long
-rw-r--r--clippy_lints/src/doc/too_long_first_doc_paragraph.rs4
-rw-r--r--tests/ui/too_long_first_doc_paragraph.rs7
-rw-r--r--tests/ui/too_long_first_doc_paragraph.stderr11
3 files changed, 21 insertions, 1 deletions
diff --git a/clippy_lints/src/doc/too_long_first_doc_paragraph.rs b/clippy_lints/src/doc/too_long_first_doc_paragraph.rs
index 1f89cab9148..8f9a54d50f8 100644
--- a/clippy_lints/src/doc/too_long_first_doc_paragraph.rs
+++ b/clippy_lints/src/doc/too_long_first_doc_paragraph.rs
@@ -51,7 +51,11 @@ pub(super) fn check(
                 // We make this suggestion only if the first doc line ends with a punctuation
                 // because it might just need to add an empty line with `///`.
                 should_suggest_empty_doc = doc.ends_with('.') || doc.ends_with('!') || doc.ends_with('?');
+            } else if spans.len() == 2 {
+                // We make this suggestion only if the second doc line is not empty.
+                should_suggest_empty_doc &= !doc.is_empty();
             }
+
             let len = doc.chars().count();
             if len >= first_paragraph_len {
                 break;
diff --git a/tests/ui/too_long_first_doc_paragraph.rs b/tests/ui/too_long_first_doc_paragraph.rs
index 2321e228866..49420841c88 100644
--- a/tests/ui/too_long_first_doc_paragraph.rs
+++ b/tests/ui/too_long_first_doc_paragraph.rs
@@ -61,6 +61,13 @@ pub union Union2 {
 /// gravida non lacinia at, rhoncus eu lacus.
 fn f() {}
 
+#[rustfmt::skip]
+/// Some function. This doc-string paragraph is too long. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
+//~^ too_long_first_doc_paragraph
+///
+/// Here's a second paragraph. It would be preferable to put the details here.
+pub fn issue_14274() {}
+
 fn main() {
     // test code goes here
 }
diff --git a/tests/ui/too_long_first_doc_paragraph.stderr b/tests/ui/too_long_first_doc_paragraph.stderr
index 2d4abaa9d63..287a2c1c3e4 100644
--- a/tests/ui/too_long_first_doc_paragraph.stderr
+++ b/tests/ui/too_long_first_doc_paragraph.stderr
@@ -39,5 +39,14 @@ LL | | /// a dolor in, pellentesque aliquet enim. Cras nec maximus sem. Mauris a
 LL | | /// gravida non lacinia at, rhoncus eu lacus.
    | |_^
 
-error: aborting due to 3 previous errors
+error: first doc comment paragraph is too long
+  --> tests/ui/too_long_first_doc_paragraph.rs:65:1
+   |
+LL | / /// Some function. This doc-string paragraph is too long. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lore...
+LL | |
+LL | | ///
+LL | | /// Here's a second paragraph. It would be preferable to put the details here.
+   | |_^
+
+error: aborting due to 4 previous errors