From f89cd3c1f3feffaa960e13c7bec07a6163d18fd3 Mon Sep 17 00:00:00 2001 From: xxchan Date: Tue, 29 Aug 2023 20:46:44 +0800 Subject: [PATCH] Don't treat lines starting with `.` or `)` as ordered markdown lists (#5839) Fixes 5835 Ordered markdown lists start with 0-9 digits followed by a `.` or a `)`. Now, rustfmt ensure that the `.` or `)` is only preceded by numeric characters before deciding that it's reached an `ItemizedBlock` --- src/comment.rs | 7 ++++++- tests/source/issue-5835.rs | 8 ++++++++ tests/target/issue-5835.rs | 8 ++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/source/issue-5835.rs create mode 100644 tests/target/issue-5835.rs diff --git a/src/comment.rs b/src/comment.rs index a000d110daab..dec925e14c01 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -482,7 +482,9 @@ impl ItemizedBlock { // allowed. for suffix in [". ", ") "] { if let Some((prefix, _)) = trimmed.split_once(suffix) { - if prefix.len() <= 2 && prefix.chars().all(|c| char::is_ascii_digit(&c)) { + let has_leading_digits = (1..=2).contains(&prefix.len()) + && prefix.chars().all(|c| char::is_ascii_digit(&c)); + if has_leading_digits { return Some(prefix.len() + suffix.len()); } } @@ -2126,6 +2128,9 @@ fn main() { // https://spec.commonmark.org/0.30 says: "A start number may not be negative": "-1. Not a list item.", "-1 Not a list item.", + // Marker without prefix are not recognized as item markers: + ". Not a list item.", + ") Not a list item.", ]; for line in test_inputs.iter() { let maybe_block = ItemizedBlock::new(line); diff --git a/tests/source/issue-5835.rs b/tests/source/issue-5835.rs new file mode 100644 index 000000000000..3e4da3492ce0 --- /dev/null +++ b/tests/source/issue-5835.rs @@ -0,0 +1,8 @@ +// rustfmt-wrap_comments: true + +/// . a +pub fn foo() {} + +pub fn main() { + // . a +} diff --git a/tests/target/issue-5835.rs b/tests/target/issue-5835.rs new file mode 100644 index 000000000000..3e4da3492ce0 --- /dev/null +++ b/tests/target/issue-5835.rs @@ -0,0 +1,8 @@ +// rustfmt-wrap_comments: true + +/// . a +pub fn foo() {} + +pub fn main() { + // . a +}