Skip to content

Commit 2b78fc6

Browse files
committed
Do not autofix comments containing bare CR
1 parent 428208e commit 2b78fc6

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

clippy_lints/src/four_forward_slashes.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::source::SpanRangeExt as _;
3+
use itertools::Itertools;
24
use rustc_errors::Applicability;
35
use rustc_hir::Item;
46
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -81,6 +83,14 @@ impl<'tcx> LateLintPass<'tcx> for FourForwardSlashes {
8183
"turn these into doc comments by removing one `/`"
8284
};
8385

86+
// If the comment contains a bare CR (not followed by a LF), do not propose an auto-fix
87+
// as bare CR are not allowed in doc comments.
88+
if span.check_source_text(cx, |s| contains_bare_cr(s)) {
89+
diag.help(msg)
90+
.note("bare CR characters are not allowed in doc comments");
91+
return;
92+
}
93+
8494
diag.multipart_suggestion(
8595
msg,
8696
bad_comments
@@ -97,3 +107,10 @@ impl<'tcx> LateLintPass<'tcx> for FourForwardSlashes {
97107
}
98108
}
99109
}
110+
111+
fn contains_bare_cr(text: &str) -> bool {
112+
text.bytes()
113+
.tuple_windows()
114+
.find(|(a, b)| *a == b'\r' && *b != b'\n')
115+
.is_some()
116+
}

tests/ui/four_forward_slashes_bare_cr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//@no-rustfix
2+
#![warn(clippy::four_forward_slashes)]
3+
4+
//~v four_forward_slashes
5+
//// nondoc comment with bare CR: ''
6+
fn main() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: this item has comments with 4 forward slashes (`////`). These look like doc comments, but they aren't
2+
--> tests/ui/four_forward_slashes_bare_cr.rs:5:1
3+
|
4+
LL | / //// nondoc comment with bare CR: '␍'
5+
LL | | fn main() {}
6+
| |_^
7+
|
8+
= help: make this a doc comment by removing one `/`
9+
= note: bare CR characters are not allowed in doc comments
10+
= note: `-D clippy::four-forward-slashes` implied by `-D warnings`
11+
= help: to override `-D warnings` add `#[allow(clippy::four_forward_slashes)]`
12+
13+
error: aborting due to 1 previous error
14+

0 commit comments

Comments
 (0)