Skip to content

Commit 61b1f19

Browse files
doc_comments_missing_terminal_punctuation: handle sentences in parens
1 parent 88bb926 commit 61b1f19

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

clippy_lints/src/doc/doc_comments_missing_terminal_punctuation.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ pub fn check(cx: &LateContext<'_>, doc: &str, fragments: Fragments<'_>) {
3030
}
3131
}
3232

33+
/// If punctuation is missing, returns the offset where new punctuation should be inserted.
3334
#[must_use]
34-
/// If punctuation is missing, returns the docstring and the offset
35-
/// where new punctuation should be inserted.
3635
fn is_missing_punctuation(doc_string: &str) -> Option<usize> {
3736
const TERMINAL_PUNCTUATION_MARKS: &[char] = &['.', '?', '!', '…'];
3837

@@ -65,22 +64,27 @@ fn is_missing_punctuation(doc_string: &str) -> Option<usize> {
6564
Event::InlineHtml(_) | Event::Start(Tag::Image { .. }) | Event::End(TagEnd::Image) => {
6665
text_offset = None;
6766
},
68-
Event::Text(..) | Event::Start(Tag::Link { .. }) | Event::End(TagEnd::Link)
69-
if no_report_depth == 0 && !offset.is_empty() =>
70-
{
71-
text_offset = Some(offset);
67+
Event::Start(Tag::Link { .. }) | Event::End(TagEnd::Link) if no_report_depth == 0 && !offset.is_empty() => {
68+
text_offset = Some(offset.end);
69+
},
70+
Event::Text(..) if no_report_depth == 0 && !offset.is_empty() => {
71+
if doc_string[..offset.end].trim_end().ends_with(')') {
72+
text_offset = Some(offset.end - 1);
73+
} else {
74+
text_offset = Some(offset.end);
75+
}
7276
},
7377
_ => {},
7478
}
7579
}
7680

7781
let text_offset = text_offset?;
78-
if doc_string[..text_offset.end]
79-
.trim_end_matches(|c: char| c.is_whitespace() || c == ')' || c == ']' || c == '}')
82+
if doc_string[..text_offset]
83+
.trim_end()
8084
.ends_with(TERMINAL_PUNCTUATION_MARKS)
8185
{
8286
None
8387
} else {
84-
Some(text_offset.end)
88+
Some(text_offset)
8589
}
8690
}

tests/ui/doc/doc_comments_missing_terminal_punctuation.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ mod module {
7878
enum Trailers {
7979
/// (Sometimes the last sentence is in parentheses, and that's ok.)
8080
ParensPassing,
81-
/// (But sometimes it is missing a period).
81+
/// (But sometimes it is missing a period.)
8282
//~^ doc_comments_missing_terminal_punctuation
8383
ParensFailing,
8484
/// **Sometimes the last sentence is in bold, and that's ok.**

tests/ui/doc/doc_comments_missing_terminal_punctuation.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ LL | //! inner attributes too
4444
| ^ help: end the doc comment with some punctuation: `.`
4545

4646
error: doc comments should end with a terminal punctuation mark
47-
--> tests/ui/doc/doc_comments_missing_terminal_punctuation.rs:81:47
47+
--> tests/ui/doc/doc_comments_missing_terminal_punctuation.rs:81:46
4848
|
4949
LL | /// (But sometimes it is missing a period)
50-
| ^ help: end the doc comment with some punctuation: `.`
50+
| ^ help: end the doc comment with some punctuation: `.`
5151

5252
error: doc comments should end with a terminal punctuation mark
5353
--> tests/ui/doc/doc_comments_missing_terminal_punctuation.rs:86:47

0 commit comments

Comments
 (0)