Skip to content

Commit 8337eb7

Browse files
committed
do not lint on macros, add more testcases
1 parent daf579a commit 8337eb7

File tree

5 files changed

+96
-40
lines changed

5 files changed

+96
-40
lines changed

clippy_lints/src/doc/doc_comment_double_space_linebreak.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
use std::borrow::Cow;
2-
31
use clippy_utils::diagnostics::span_lint_and_then;
42
use clippy_utils::source::snippet;
5-
use pulldown_cmark::CowStr;
63
use rustc_errors::Applicability;
74
use rustc_lint::LateContext;
85
use rustc_span::Span;
96

107
use super::DOC_COMMENT_DOUBLE_SPACE_LINEBREAK;
118

12-
pub fn check(cx: &LateContext<'_>, collected_breaks: Vec<(Span, (Span, CowStr<'_>), Cow<'_, str>)>) {
13-
let replacements: Vec<_> = collect_doc_replacements(cx, &collected_breaks);
9+
pub fn check(cx: &LateContext<'_>, collected_breaks: &[Span]) {
10+
let replacements: Vec<_> = collect_doc_replacements(cx, collected_breaks);
1411

1512
if let Some((&(lo_span, _), &(hi_span, _))) = replacements.first().zip(replacements.last()) {
1613
span_lint_and_then(
@@ -29,16 +26,15 @@ pub fn check(cx: &LateContext<'_>, collected_breaks: Vec<(Span, (Span, CowStr<'_
2926
}
3027
}
3128

32-
fn collect_doc_replacements(
33-
cx: &LateContext<'_>,
34-
attrs: &[(Span, (Span, CowStr<'_>), Cow<'_, str>)],
35-
) -> Vec<(Span, String)> {
36-
attrs
29+
fn collect_doc_replacements(cx: &LateContext<'_>, spans: &[Span]) -> Vec<(Span, String)> {
30+
spans
3731
.iter()
38-
.map(|attr| {
39-
let full = attr.0;
40-
let new_comment = format!("\\\n/// ");
41-
(full, new_comment)
32+
.map(|span| {
33+
let s = snippet(cx, *span, "..");
34+
let after_newline = s.trim_start_matches(' ');
35+
36+
let new_comment = format!("\\{after_newline}");
37+
(*span, new_comment)
4238
})
4339
.collect()
4440
}

clippy_lints/src/doc/mod.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ use rustc_resolve::rustdoc::{
3030
};
3131
use rustc_session::impl_lint_pass;
3232
use rustc_span::edition::Edition;
33-
use rustc_span::{sym, Span};
34-
use std::borrow::Cow;
33+
use rustc_span::{Span, sym};
3534
use std::ops::Range;
3635
use url::Url;
3736

37+
mod doc_comment_double_space_linebreak;
38+
mod empty_line_after;
3839
mod empty_line_after;
3940
mod include_in_doc_without_cfg;
40-
mod doc_comment_double_space_linebreak;
4141
mod link_with_quotes;
4242
mod markdown;
4343
mod missing_headers;
@@ -908,7 +908,8 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
908908
let mut paragraph_range = 0..0;
909909
let mut code_level = 0;
910910
let mut blockquote_level = 0;
911-
let mut collected_breaks: Vec<(Span, (Span, CowStr<'_>), Cow<'_, str>)> = Vec::new();
911+
let mut collected_breaks: Vec<Span> = Vec::new();
912+
912913
let mut containers = Vec::new();
913914

914915
let mut events = events.peekable();
@@ -1082,19 +1083,15 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
10821083
);
10831084
}
10841085

1085-
if let Some(span) = fragments.span(cx, range.clone())
1086-
&& let Some(ref p) = prev_text
1086+
if let Some(span) = fragments.span(cx, range.clone())
1087+
&& !span.from_expansion()
10871088
&& let snippet = snippet(cx, span, "..")
1088-
&& !snippet.trim().starts_with("\\")
1089+
&& !snippet.trim().starts_with('\\')
10891090
&& event == HardBreak {
1090-
collected_breaks.push((span, p.clone(), snippet));
1091-
prev_text = None;
1091+
collected_breaks.push(span);
10921092
}
10931093
},
10941094
Text(text) => {
1095-
if let Some(span) = fragments.span(cx, range.clone()) {
1096-
prev_text = Some((span, text.clone()));
1097-
}
10981095
paragraph_range.end = range.end;
10991096
let range_ = range.clone();
11001097
ticks_unbalanced |= text.contains('`')
@@ -1144,7 +1141,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
11441141
}
11451142
}
11461143

1147-
doc_comment_double_space_linebreak::check(cx, collected_breaks);
1144+
doc_comment_double_space_linebreak::check(cx, &collected_breaks);
11481145

11491146
headers
11501147
}

tests/ui/doc/doc_comment_double_space_linebreak.fixed

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![rustfmt::skip]
33

44
#![warn(clippy::doc_comment_double_space_linebreak)]
5-
#![allow(unused)]
5+
#![allow(unused, clippy::empty_docs)]
66

77
//! Should warn on double space linebreaks\
88
//! in file/module doc comment
@@ -58,4 +58,29 @@ macro_rules! macro_that_makes_function {
5858

5959
macro_that_makes_function!();
6060

61+
// dont lint when its alone on a line
62+
///
63+
fn alone() {}
64+
65+
/// | First column | Second column |
66+
/// | ------------ | ------------- |
67+
/// | Not a line | break when |
68+
/// | after a line | in a table |
69+
fn table() {}
70+
71+
/// ```text
72+
/// It's also not a hard line break if
73+
/// there's two spaces at the end of a
74+
/// line in a block code.
75+
/// ```
76+
fn codeblock() {}
77+
78+
/// It's also not a hard line break `if
79+
/// there's` two spaces in the middle of inline code.
80+
fn inline() {}
81+
82+
/// It's also not a hard line break [when](
83+
/// https://example.com) in a URL.
84+
fn url() {}
85+
6186
fn main() {}

tests/ui/doc/doc_comment_double_space_linebreak.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![rustfmt::skip]
33

44
#![warn(clippy::doc_comment_double_space_linebreak)]
5-
#![allow(unused)]
5+
#![allow(unused, clippy::empty_docs)]
66

77
//! Should warn on double space linebreaks
88
//! in file/module doc comment
@@ -58,4 +58,29 @@ macro_rules! macro_that_makes_function {
5858

5959
macro_that_makes_function!();
6060

61+
// dont lint when its alone on a line
62+
///
63+
fn alone() {}
64+
65+
/// | First column | Second column |
66+
/// | ------------ | ------------- |
67+
/// | Not a line | break when |
68+
/// | after a line | in a table |
69+
fn table() {}
70+
71+
/// ```text
72+
/// It's also not a hard line break if
73+
/// there's two spaces at the end of a
74+
/// line in a block code.
75+
/// ```
76+
fn codeblock() {}
77+
78+
/// It's also not a hard line break `if
79+
/// there's` two spaces in the middle of inline code.
80+
fn inline() {}
81+
82+
/// It's also not a hard line break [when](
83+
/// https://example.com) in a URL.
84+
fn url() {}
85+
6186
fn main() {}

tests/ui/doc/doc_comment_double_space_linebreak.stderr

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,53 @@
11
error: doc comments should use a back-slash (\) instead of a double space to indicate a linebreak
2-
--> tests/ui/doc/doc_comment_double_space_linebreak.rs:7:1
2+
--> tests/ui/doc/doc_comment_double_space_linebreak.rs:7:43
33
|
4-
LL | //! Should warn on double space linebreaks
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace this double space with a back-slash: `//! Should warn on double space linebreaks\`
4+
LL | //! Should warn on double space linebreaks
5+
| ___________________________________________^
6+
LL | | //! in file/module doc comment
7+
| |____^
68
|
79
= note: `-D clippy::doc-comment-double-space-linebreak` implied by `-D warnings`
810
= help: to override `-D warnings` add `#[allow(clippy::doc_comment_double_space_linebreak)]`
11+
help: replace this double space with a back-slash
12+
|
13+
LL ~ //! Should warn on double space linebreaks\
14+
LL ~ //! in file/module doc comment
15+
|
916

1017
error: doc comments should use a back-slash (\) instead of a double space to indicate a linebreak
11-
--> tests/ui/doc/doc_comment_double_space_linebreak.rs:35:1
18+
--> tests/ui/doc/doc_comment_double_space_linebreak.rs:35:51
1219
|
13-
LL | / /// Should warn when doc comment uses double space
20+
LL | /// Should warn when doc comment uses double space
21+
| ___________________________________________________^
1422
LL | | /// as a line-break, even when there are multiple
15-
| |___________________________________________________^
23+
LL | | /// in a row
24+
| |____^
1625
|
1726
help: replace this double space with a back-slash
1827
|
19-
LL + /// Should warn when doc comment uses double space\
20-
LL + /// as a line-break, even when there are multiple\
28+
LL ~ /// Should warn when doc comment uses double space\
29+
LL ~ /// as a line-break, even when there are multiple\
30+
LL ~ /// in a row
2131
|
2232

2333
error: doc comments should use a back-slash (\) instead of a double space to indicate a linebreak
24-
--> tests/ui/doc/doc_comment_double_space_linebreak.rs:44:1
34+
--> tests/ui/doc/doc_comment_double_space_linebreak.rs:44:12
2535
|
26-
LL | / /// 🌹 are 🟥
36+
LL | /// 🌹 are 🟥
37+
| ______________^
2738
LL | | /// 🌷 are 🟦
2839
LL | | /// 📎 is 😎
2940
LL | | /// and so are 🫵
30-
| |___________________^
41+
LL | | /// (hopefully no formatting weirdness linting this)
42+
| |____^
3143
|
3244
help: replace this double space with a back-slash
3345
|
3446
LL ~ /// 🌹 are 🟥\
3547
LL ~ /// 🌷 are 🟦\
3648
LL ~ /// 📎 is 😎\
3749
LL ~ /// and so are 🫵\
50+
LL ~ /// (hopefully no formatting weirdness linting this)
3851
|
3952

4053
error: aborting due to 3 previous errors

0 commit comments

Comments
 (0)