Skip to content

Do not autofix comments containing bare CR #15175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions clippy_lints/src/four_forward_slashes.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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
Expand All @@ -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')
}
6 changes: 6 additions & 0 deletions tests/ui/four_forward_slashes_bare_cr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//@no-rustfix
#![warn(clippy::four_forward_slashes)]

//~v four_forward_slashes
//// nondoc comment with bare CR: ''
fn main() {}
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/four_forward_slashes_bare_cr.stderr
Original file line number Diff line number Diff line change
@@ -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