Skip to content

Commit 1ac5a35

Browse files
committed
fix: do not wrap reference-style doc links
Prevents wrap_comments from incorrectly wrapping reference-style doc links.
1 parent 2c442cc commit 1ac5a35

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/comment.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use std::{self, borrow::Cow, iter};
44

55
use itertools::{multipeek, MultiPeek};
6+
use lazy_static::lazy_static;
7+
use regex::Regex;
68
use rustc_span::Span;
79

810
use crate::config::Config;
@@ -15,6 +17,17 @@ use crate::utils::{
1517
};
1618
use crate::{ErrorKind, FormattingError};
1719

20+
lazy_static! {
21+
/// A regex matching reference doc links.
22+
///
23+
/// ```markdown
24+
/// /// An [example].
25+
/// ///
26+
/// /// [example]: this::is::a::link
27+
/// ```
28+
static ref REFERENCE_LINK_URL: Regex = Regex::new(r"^\[.+\]\s?:").unwrap();
29+
}
30+
1831
fn is_custom_comment(comment: &str) -> bool {
1932
if !comment.starts_with("//") {
2033
false
@@ -842,7 +855,11 @@ fn trim_custom_comment_prefix(s: &str) -> String {
842855
/// Returns `true` if the given string MAY include URLs or alike.
843856
fn has_url(s: &str) -> bool {
844857
// This function may return false positive, but should get its job done in most cases.
845-
s.contains("https://") || s.contains("http://") || s.contains("ftp://") || s.contains("file://")
858+
s.contains("https://")
859+
|| s.contains("http://")
860+
|| s.contains("ftp://")
861+
|| s.contains("file://")
862+
|| REFERENCE_LINK_URL.is_match(s)
846863
}
847864

848865
/// Given the span, rewrite the missing comment inside it if available.

tests/target/issue-5095.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// rustfmt-wrap_comments: true
2+
// format_code_in_doc_comments: true
3+
4+
pub mod a_long_name {
5+
pub mod b_long_name {
6+
pub mod c_long_name {
7+
pub mod d_long_name {
8+
pub mod e_long_name {
9+
pub struct Bananas;
10+
impl Bananas {
11+
pub fn fantastic() {}
12+
}
13+
14+
pub mod f_long_name {
15+
pub struct Apples;
16+
}
17+
}
18+
}
19+
}
20+
}
21+
}
22+
23+
/// Check out [my other struct] ([`Bananas`]) and [the method it has].
24+
///
25+
/// [my other struct]: a_long_name::b_long_name::c_long_name::d_long_name::e_long_name::f_long_name::Apples
26+
/// [`Bananas`]: a_long_name::b_long_name::c_long_name::d_long_name::e_long_name::Bananas::fantastic()
27+
/// [the method it has]: a_long_name::b_long_name::c_long_name::d_long_name::e_long_name::Bananas::fantastic()
28+
pub struct A;

0 commit comments

Comments
 (0)