about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlejandra González <blyxyas@gmail.com>2025-07-29 17:05:13 +0000
committerGitHub <noreply@github.com>2025-07-29 17:05:13 +0000
commitc7dd98c80913b9845f49997142e5bec3524bc343 (patch)
tree377d7a09f9380d31814757523aa39ac02ef22b8f
parentb7cc5c45bdf89ea049fc63c41479a1cae265ead5 (diff)
parent0920486fa6c0a3c08e23a867b1e6ca6a5563849b (diff)
Do not autofix comments containing bare CR (#15175)
When a bare CR is present in a four slashes comment, keep triggering the
lint but do not issue a fix suggestion as bare CRs are not supported in
doc comments. An extra note is added to the diagnostic to warn the user
about it.

I have put the test in a separate file to make it clear that the bare CR
is not a formatting error.

Fixes rust-lang/rust-clippy#15174

changelog: [`four_forward_slashes`]: warn about bare CR in comment, and
do not propose invalid autofix
-rw-r--r--clippy_lints/src/four_forward_slashes.rs15
-rw-r--r--tests/ui/four_forward_slashes_bare_cr.rs6
-rw-r--r--tests/ui/four_forward_slashes_bare_cr.stderr14
3 files changed, 35 insertions, 0 deletions
diff --git a/clippy_lints/src/four_forward_slashes.rs b/clippy_lints/src/four_forward_slashes.rs
index 8822b87f92f..a7b0edeb799 100644
--- a/clippy_lints/src/four_forward_slashes.rs
+++ b/clippy_lints/src/four_forward_slashes.rs
@@ -1,4 +1,6 @@
 use clippy_utils::diagnostics::span_lint_and_then;
+use clippy_utils::source::SpanRangeExt as _;
+use itertools::Itertools;
 use rustc_errors::Applicability;
 use rustc_hir::Item;
 use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -81,6 +83,14 @@ impl<'tcx> LateLintPass<'tcx> for FourForwardSlashes {
                         "turn these into doc comments by removing one `/`"
                     };
 
+                    // If the comment contains a bare CR (not followed by a LF), do not propose an auto-fix
+                    // as bare CR are not allowed in doc comments.
+                    if span.check_source_text(cx, contains_bare_cr) {
+                        diag.help(msg)
+                            .note("bare CR characters are not allowed in doc comments");
+                        return;
+                    }
+
                     diag.multipart_suggestion(
                         msg,
                         bad_comments
@@ -97,3 +107,8 @@ impl<'tcx> LateLintPass<'tcx> for FourForwardSlashes {
         }
     }
 }
+
+/// Checks if `text` contains any CR not followed by a LF
+fn contains_bare_cr(text: &str) -> bool {
+    text.bytes().tuple_windows().any(|(a, b)| a == b'\r' && b != b'\n')
+}
diff --git a/tests/ui/four_forward_slashes_bare_cr.rs b/tests/ui/four_forward_slashes_bare_cr.rs
new file mode 100644
index 00000000000..19123cd206e
--- /dev/null
+++ b/tests/ui/four_forward_slashes_bare_cr.rs
@@ -0,0 +1,6 @@
+//@no-rustfix
+#![warn(clippy::four_forward_slashes)]
+
+//~v four_forward_slashes
+//// nondoc comment with bare CR: '
'
+fn main() {}
diff --git a/tests/ui/four_forward_slashes_bare_cr.stderr b/tests/ui/four_forward_slashes_bare_cr.stderr
new file mode 100644
index 00000000000..64e70b97db9
--- /dev/null
+++ b/tests/ui/four_forward_slashes_bare_cr.stderr
@@ -0,0 +1,14 @@
+error: this item has comments with 4 forward slashes (`////`). These look like doc comments, but they aren't
+  --> tests/ui/four_forward_slashes_bare_cr.rs:5:1
+   |
+LL | / //// nondoc comment with bare CR: '␍'
+LL | | fn main() {}
+   | |_^
+   |
+   = help: make this a doc comment by removing one `/`
+   = note: bare CR characters are not allowed in doc comments
+   = note: `-D clippy::four-forward-slashes` implied by `-D warnings`
+   = help: to override `-D warnings` add `#[allow(clippy::four_forward_slashes)]`
+
+error: aborting due to 1 previous error
+