Skip to content

Commit 8aa3c59

Browse files
committed
Move rustfmt changes out
Now in rust-lang/rustfmt#6154
1 parent a0a8442 commit 8aa3c59

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4884,6 +4884,7 @@ dependencies = [
48844884
"getopts",
48854885
"ignore",
48864886
"itertools 0.11.0",
4887+
"lazy_static",
48874888
"regex",
48884889
"rustfmt-config_proc_macro",
48894890
"serde",

src/tools/rustfmt/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ dirs = "4.0"
4444
getopts = "0.2"
4545
ignore = "0.4"
4646
itertools = "0.11"
47+
lazy_static = "1.4"
4748
regex = "1.7"
4849
serde = { version = "1.0.160", features = ["derive"] }
4950
serde_json = "1.0"

src/tools/rustfmt/src/comment.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use std::{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
@@ -967,16 +980,11 @@ fn trim_custom_comment_prefix(s: &str) -> String {
967980
/// Returns `true` if the given string MAY include URLs or alike.
968981
fn has_url(s: &str) -> bool {
969982
// This function may return false positive, but should get its job done in most cases.
970-
// The regex is indended to capture text such as the below.
971-
//
972-
// /// An [example].
973-
// ///
974-
// /// [example]: this::is::a::link
975983
s.contains("https://")
976984
|| s.contains("http://")
977985
|| s.contains("ftp://")
978986
|| s.contains("file://")
979-
|| static_regex!(r"^\[.+\]\s?:").is_match(s)
987+
|| REFERENCE_LINK_URL.is_match(s)
980988
}
981989

982990
/// Returns true if the given string may be part of a Markdown table.

src/tools/rustfmt/src/lib.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#![allow(clippy::match_like_matches_macro)]
66
#![allow(unreachable_pub)]
77

8+
#[cfg(test)]
9+
#[macro_use]
10+
extern crate lazy_static;
811
#[macro_use]
912
extern crate tracing;
1013

@@ -59,13 +62,6 @@ pub use crate::rustfmt_diff::{ModifiedChunk, ModifiedLines};
5962
#[macro_use]
6063
mod utils;
6164

62-
macro_rules! static_regex {
63-
($re:literal) => {{
64-
static RE: ::std::sync::OnceLock<::regex::Regex> = ::std::sync::OnceLock::new();
65-
RE.get_or_init(|| ::regex::Regex::new($re).unwrap())
66-
}};
67-
}
68-
6965
mod attr;
7066
mod chains;
7167
mod closures;

src/tools/rustfmt/src/test/configuration_snippet.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ impl ConfigurationSection {
2424
fn get_section<I: Iterator<Item = String>>(
2525
file: &mut Enumerate<I>,
2626
) -> Option<ConfigurationSection> {
27+
lazy_static! {
28+
static ref CONFIG_NAME_REGEX: regex::Regex =
29+
regex::Regex::new(r"^## `([^`]+)`").expect("failed creating configuration pattern");
30+
// Configuration values, which will be passed to `from_str`:
31+
//
32+
// - must be prefixed with `####`
33+
// - must be wrapped in backticks
34+
// - may by wrapped in double quotes (which will be stripped)
35+
static ref CONFIG_VALUE_REGEX: regex::Regex =
36+
regex::Regex::new(r#"^#### `"?([^`]+?)"?`"#)
37+
.expect("failed creating configuration value pattern");
38+
}
39+
2740
loop {
2841
match file.next() {
2942
Some((i, line)) => {
@@ -40,14 +53,9 @@ impl ConfigurationSection {
4053
let start_line = (i + 2) as u32;
4154

4255
return Some(ConfigurationSection::CodeBlock((block, start_line)));
43-
} else if let Some(c) = static_regex!(r"^## `([^`]+)`").captures(&line) {
56+
} else if let Some(c) = CONFIG_NAME_REGEX.captures(&line) {
4457
return Some(ConfigurationSection::ConfigName(String::from(&c[1])));
45-
} else if let Some(c) = static_regex!(r#"^#### `"?([^`]+?)"?`"#).captures(&line) {
46-
// Configuration values, which will be passed to `from_str`
47-
//
48-
// - must be prefixed with `####`
49-
// - must be wrapped in backticks
50-
// - may by wrapped in double quotes (which will be stripped)
58+
} else if let Some(c) = CONFIG_VALUE_REGEX.captures(&line) {
5159
return Some(ConfigurationSection::ConfigValue(String::from(&c[1])));
5260
}
5361
}

0 commit comments

Comments
 (0)