Skip to content

Commit 9d07114

Browse files
committed
do not lint on macros, add more testcases
1 parent 6b947b1 commit 9d07114

File tree

5 files changed

+92
-39
lines changed

5 files changed

+92
-39
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: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use rustc_resolve::rustdoc::{
2828
use rustc_session::impl_lint_pass;
2929
use rustc_span::edition::Edition;
3030
use rustc_span::{sym, Span};
31-
use std::borrow::Cow;
3231
use std::ops::Range;
3332
use url::Url;
3433

@@ -691,8 +690,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
691690
let mut paragraph_range = 0..0;
692691
let mut code_level = 0;
693692
let mut blockquote_level = 0;
694-
let mut prev_text: Option<(Span, CowStr<'_>)> = None;
695-
let mut collected_breaks: Vec<(Span, (Span, CowStr<'_>), Cow<'_, str>)> = Vec::new();
693+
let mut collected_breaks: Vec<Span> = Vec::new();
696694

697695
let mut containers = Vec::new();
698696

@@ -808,19 +806,15 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
808806
);
809807
}
810808

811-
if let Some(span) = fragments.span(cx, range.clone())
812-
&& let Some(ref p) = prev_text
809+
if let Some(span) = fragments.span(cx, range.clone())
810+
&& !span.from_expansion()
813811
&& let snippet = snippet(cx, span, "..")
814-
&& !snippet.trim().starts_with("\\")
812+
&& !snippet.trim().starts_with('\\')
815813
&& event == HardBreak {
816-
collected_breaks.push((span, p.clone(), snippet));
817-
prev_text = None;
814+
collected_breaks.push(span);
818815
}
819816
},
820817
Text(text) => {
821-
if let Some(span) = fragments.span(cx, range.clone()) {
822-
prev_text = Some((span, text.clone()));
823-
}
824818
paragraph_range.end = range.end;
825819
let range_ = range.clone();
826820
ticks_unbalanced |= text.contains('`')
@@ -869,7 +863,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
869863
}
870864
}
871865

872-
doc_comment_double_space_linebreak::check(cx, collected_breaks);
866+
doc_comment_double_space_linebreak::check(cx, &collected_breaks);
873867

874868
headers
875869
}

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)