Skip to content
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

Remove lazy_static dependency #6154

Merged
merged 1 commit into from
May 31, 2024
Merged
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
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ dirs = "5.0"
getopts = "0.2"
ignore = "0.4"
itertools = "0.12"
lazy_static = "1.4"
regex = "1.7"
serde = { version = "1.0.160", features = ["derive"] }
serde_json = "1.0"
Expand Down
24 changes: 10 additions & 14 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
use std::{borrow::Cow, iter};

use itertools::{multipeek, MultiPeek};
use lazy_static::lazy_static;
use regex::Regex;
use rustc_span::Span;

use crate::config::Config;
Expand All @@ -17,17 +15,6 @@ use crate::utils::{
};
use crate::{ErrorKind, FormattingError};

lazy_static! {
/// A regex matching reference doc links.
///
/// ```markdown
/// /// An [example].
/// ///
/// /// [example]: this::is::a::link
/// ```
static ref REFERENCE_LINK_URL: Regex = Regex::new(r"^\[.+\]\s?:").unwrap();
}

fn is_custom_comment(comment: &str) -> bool {
if !comment.starts_with("//") {
false
Expand Down Expand Up @@ -976,12 +963,21 @@ fn trim_custom_comment_prefix(s: &str) -> String {

/// Returns `true` if the given string MAY include URLs or alike.
fn has_url(s: &str) -> bool {
// A regex matching reference doc links.
//
// ```markdown
// /// An [example].
// ///
// /// [example]: this::is::a::link
// ```
let reference_link_url = static_regex!(r"^\[.+\]\s?:");

// This function may return false positive, but should get its job done in most cases.
s.contains("https://")
|| s.contains("http://")
|| s.contains("ftp://")
|| s.contains("file://")
|| REFERENCE_LINK_URL.is_match(s)
|| reference_link_url.is_match(s)
}

/// Returns true if the given string may be part of a Markdown table.
Expand Down
10 changes: 7 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
#![recursion_limit = "256"]
#![allow(clippy::match_like_matches_macro)]

#[cfg(test)]
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate tracing;

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

macro_rules! static_regex {
($re:literal) => {{
static RE: ::std::sync::OnceLock<::regex::Regex> = ::std::sync::OnceLock::new();
RE.get_or_init(|| ::regex::Regex::new($re).unwrap())
}};
}

mod attr;
mod chains;
mod closures;
Expand Down
24 changes: 9 additions & 15 deletions src/test/configuration_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,13 @@ impl ConfigurationSection {
fn get_section<I: Iterator<Item = String>>(
file: &mut Enumerate<I>,
) -> Option<ConfigurationSection> {
lazy_static! {
static ref CONFIG_NAME_REGEX: regex::Regex =
regex::Regex::new(r"^## `([^`]+)`").expect("failed creating configuration pattern");
// Configuration values, which will be passed to `from_str`:
//
// - must be prefixed with `####`
// - must be wrapped in backticks
// - may by wrapped in double quotes (which will be stripped)
static ref CONFIG_VALUE_REGEX: regex::Regex =
regex::Regex::new(r#"^#### `"?([^`]+?)"?`"#)
.expect("failed creating configuration value pattern");
}

GKFX marked this conversation as resolved.
Show resolved Hide resolved
let config_name_regex = static_regex!(r"^## `([^`]+)`");
// Configuration values, which will be passed to `from_str`:
//
// - must be prefixed with `####`
// - must be wrapped in backticks
// - may by wrapped in double quotes (which will be stripped)
let config_value_regex = static_regex!(r#"^#### `"?([^`]+?)"?`"#);
loop {
match file.next() {
Some((i, line)) => {
Expand All @@ -53,9 +47,9 @@ impl ConfigurationSection {
let start_line = (i + 2) as u32;

return Some(ConfigurationSection::CodeBlock((block, start_line)));
} else if let Some(c) = CONFIG_NAME_REGEX.captures(&line) {
} else if let Some(c) = config_name_regex.captures(&line) {
return Some(ConfigurationSection::ConfigName(String::from(&c[1])));
} else if let Some(c) = CONFIG_VALUE_REGEX.captures(&line) {
} else if let Some(c) = config_value_regex.captures(&line) {
return Some(ConfigurationSection::ConfigValue(String::from(&c[1])));
}
}
Expand Down
Loading