Skip to content

Commit 30d8cf9

Browse files
committed
Make it trim the contents
1 parent 914e58b commit 30d8cf9

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

clippy_lints/src/four_forward_slashes.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,25 @@ impl<'tcx> LateLintPass<'tcx> for FourForwardSlashes {
5454
let Some(contents) = file.get_line(line) else {
5555
continue;
5656
};
57+
let contents = contents.trim();
5758
if contents.is_empty() {
5859
break;
5960
}
6061
if contents.starts_with("////") {
6162
let bounds = file.line_bounds(line);
6263
let span = Span::new(bounds.start, bounds.end, SyntaxContext::root(), None);
6364

64-
if snippet_opt(cx, span).is_some_and(|s| s.starts_with("////")) {
65+
if snippet_opt(cx, span).is_some_and(|s| s.trim().starts_with("////")) {
6566
span_lint_and_sugg(
6667
cx,
6768
FOUR_FORWARD_SLASHES,
6869
span,
6970
"comment with 4 forward slashes (`////`). This looks like a doc comment, but it isn't",
7071
"make this a doc comment by removing one `/`",
7172
// It's a little unfortunate but the span includes the `\n` yet the contents
72-
// do not, so we must add it back. If some codebase uses `\r\n` instead this
73-
// they will need normalization but it should be fine
74-
contents[1..].to_owned() + "\n",
73+
// do not, so we must add it back. If some codebase uses `\r\n` instead they
74+
// will need normalization but it should be fine
75+
contents.replacen("////", "///", 1) + "\n",
7576
Applicability::MachineApplicable,
7677
);
7778
}

tests/ui/four_forward_slashes.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//// first line borked doc comment. doesn't combust!
22
//@run-rustfix
33
//@aux-build:proc_macros.rs:proc-macro
4+
#![feature(custom_inner_attributes)]
45
#![allow(unused)]
56
#![warn(clippy::four_forward_slashes)]
67
#![no_main]
8+
#![rustfmt::skip]
79

810
#[macro_use]
911
extern crate proc_macros;
@@ -27,6 +29,9 @@ fn d() {}
2729
#[allow(dead_code)]
2830
fn g() {}
2931

32+
/// not very start of contents
33+
fn h() {}
34+
3035
external! {
3136
//// don't lint me bozo
3237
fn e() {}

tests/ui/four_forward_slashes.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//// first line borked doc comment. doesn't combust!
22
//@run-rustfix
33
//@aux-build:proc_macros.rs:proc-macro
4+
#![feature(custom_inner_attributes)]
45
#![allow(unused)]
56
#![warn(clippy::four_forward_slashes)]
67
#![no_main]
8+
#![rustfmt::skip]
79

810
#[macro_use]
911
extern crate proc_macros;
@@ -27,6 +29,9 @@ fn d() {}
2729
#[allow(dead_code)]
2830
fn g() {}
2931

32+
//// not very start of contents
33+
fn h() {}
34+
3035
external! {
3136
//// don't lint me bozo
3237
fn e() {}

tests/ui/four_forward_slashes.stderr

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: comment with 4 forward slashes (`////`). This looks like a doc comment, but it isn't
2-
--> $DIR/four_forward_slashes.rs:11:1
2+
--> $DIR/four_forward_slashes.rs:13:1
33
|
44
LL | / //// whoops
55
LL | | fn a() {}
@@ -12,7 +12,7 @@ LL + /// whoops
1212
|
1313

1414
error: comment with 4 forward slashes (`////`). This looks like a doc comment, but it isn't
15-
--> $DIR/four_forward_slashes.rs:14:1
15+
--> $DIR/four_forward_slashes.rs:16:1
1616
|
1717
LL | / //// whoops
1818
LL | | #[allow(dead_code)]
@@ -24,7 +24,7 @@ LL + /// whoops
2424
|
2525

2626
error: comment with 4 forward slashes (`////`). This looks like a doc comment, but it isn't
27-
--> $DIR/four_forward_slashes.rs:19:1
27+
--> $DIR/four_forward_slashes.rs:21:1
2828
|
2929
LL | / //// two borked comments!
3030
LL | | #[track_caller]
@@ -36,7 +36,7 @@ LL + /// two borked comments!
3636
|
3737

3838
error: comment with 4 forward slashes (`////`). This looks like a doc comment, but it isn't
39-
--> $DIR/four_forward_slashes.rs:18:1
39+
--> $DIR/four_forward_slashes.rs:20:1
4040
|
4141
LL | / //// whoops
4242
LL | | //// two borked comments!
@@ -48,7 +48,7 @@ LL + /// whoops
4848
|
4949

5050
error: comment with 4 forward slashes (`////`). This looks like a doc comment, but it isn't
51-
--> $DIR/four_forward_slashes.rs:26:1
51+
--> $DIR/four_forward_slashes.rs:28:1
5252
|
5353
LL | / //// between attributes
5454
LL | | #[allow(dead_code)]
@@ -59,5 +59,17 @@ help: make this a doc comment by removing one `/`
5959
LL + /// between attributes
6060
|
6161

62-
error: aborting due to 5 previous errors
62+
error: comment with 4 forward slashes (`////`). This looks like a doc comment, but it isn't
63+
--> $DIR/four_forward_slashes.rs:32:1
64+
|
65+
LL | / //// not very start of contents
66+
LL | | fn h() {}
67+
| |_
68+
|
69+
help: make this a doc comment by removing one `/`
70+
|
71+
LL + /// not very start of contents
72+
|
73+
74+
error: aborting due to 6 previous errors
6375

0 commit comments

Comments
 (0)