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
This commit is contained in:
Alejandra González 2025-07-29 17:05:13 +00:00 committed by GitHub
commit c7dd98c809
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 35 additions and 0 deletions

View file

@ -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')
}

View file

@ -0,0 +1,6 @@
//@no-rustfix
#![warn(clippy::four_forward_slashes)]
//~v four_forward_slashes
//// nondoc comment with bare CR: ' '
fn main() {}

View file

@ -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