Skip to content

Commit 871113e

Browse files
GKFXytmimi
authored andcommitted
Remove lazy_static dependency
1 parent 28e43b6 commit 871113e

File tree

5 files changed

+26
-34
lines changed

5 files changed

+26
-34
lines changed

Diff for: Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

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

Diff for: src/comment.rs

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

55
use itertools::{multipeek, MultiPeek};
6-
use lazy_static::lazy_static;
7-
use regex::Regex;
86
use rustc_span::Span;
97

108
use crate::config::Config;
@@ -17,17 +15,6 @@ use crate::utils::{
1715
};
1816
use crate::{ErrorKind, FormattingError};
1917

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-
3118
fn is_custom_comment(comment: &str) -> bool {
3219
if !comment.starts_with("//") {
3320
false
@@ -976,12 +963,21 @@ fn trim_custom_comment_prefix(s: &str) -> String {
976963

977964
/// Returns `true` if the given string MAY include URLs or alike.
978965
fn has_url(s: &str) -> bool {
966+
// A regex matching reference doc links.
967+
//
968+
// ```markdown
969+
// /// An [example].
970+
// ///
971+
// /// [example]: this::is::a::link
972+
// ```
973+
let reference_link_url = static_regex!(r"^\[.+\]\s?:");
974+
979975
// This function may return false positive, but should get its job done in most cases.
980976
s.contains("https://")
981977
|| s.contains("http://")
982978
|| s.contains("ftp://")
983979
|| s.contains("file://")
984-
|| REFERENCE_LINK_URL.is_match(s)
980+
|| reference_link_url.is_match(s)
985981
}
986982

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

Diff for: src/lib.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
#![recursion_limit = "256"]
55
#![allow(clippy::match_like_matches_macro)]
66

7-
#[cfg(test)]
8-
#[macro_use]
9-
extern crate lazy_static;
107
#[macro_use]
118
extern crate tracing;
129

@@ -61,6 +58,13 @@ pub use crate::rustfmt_diff::{ModifiedChunk, ModifiedLines};
6158
#[macro_use]
6259
mod utils;
6360

61+
macro_rules! static_regex {
62+
($re:literal) => {{
63+
static RE: ::std::sync::OnceLock<::regex::Regex> = ::std::sync::OnceLock::new();
64+
RE.get_or_init(|| ::regex::Regex::new($re).unwrap())
65+
}};
66+
}
67+
6468
mod attr;
6569
mod chains;
6670
mod closures;

Diff for: src/test/configuration_snippet.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,13 @@ 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-
27+
let config_name_regex = static_regex!(r"^## `([^`]+)`");
28+
// Configuration values, which will be passed to `from_str`:
29+
//
30+
// - must be prefixed with `####`
31+
// - must be wrapped in backticks
32+
// - may by wrapped in double quotes (which will be stripped)
33+
let config_value_regex = static_regex!(r#"^#### `"?([^`]+?)"?`"#);
4034
loop {
4135
match file.next() {
4236
Some((i, line)) => {
@@ -53,9 +47,9 @@ impl ConfigurationSection {
5347
let start_line = (i + 2) as u32;
5448

5549
return Some(ConfigurationSection::CodeBlock((block, start_line)));
56-
} else if let Some(c) = CONFIG_NAME_REGEX.captures(&line) {
50+
} else if let Some(c) = config_name_regex.captures(&line) {
5751
return Some(ConfigurationSection::ConfigName(String::from(&c[1])));
58-
} else if let Some(c) = CONFIG_VALUE_REGEX.captures(&line) {
52+
} else if let Some(c) = config_value_regex.captures(&line) {
5953
return Some(ConfigurationSection::ConfigValue(String::from(&c[1])));
6054
}
6155
}

0 commit comments

Comments
 (0)