Skip to content

Commit 2771918

Browse files
committed
Auto merge of #6120 - phansch:replace-lazy-static, r=Manishearth
Replace some lazy_static usage with once_cell feature This replaces some `lazy_static` usage with [`SyncLazy`](https://doc.rust-lang.org/nightly/std/lazy/struct.SyncLazy.html) of the unstable `once_cell` feature. changelog: none
2 parents 14e7269 + da57a16 commit 2771918

File tree

9 files changed

+51
-51
lines changed

9 files changed

+51
-51
lines changed

clippy_dev/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ bytecount = "0.6"
99
clap = "2.33"
1010
itertools = "0.9"
1111
regex = "1"
12-
lazy_static = "1.0"
1312
shell-escape = "0.1"
1413
walkdir = "2"
1514

clippy_dev/src/lib.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
2+
#![feature(once_cell)]
23

34
use itertools::Itertools;
4-
use lazy_static::lazy_static;
55
use regex::Regex;
66
use std::collections::HashMap;
77
use std::ffi::OsStr;
88
use std::fs;
9+
use std::lazy::SyncLazy;
910
use std::path::{Path, PathBuf};
1011
use walkdir::WalkDir;
1112

@@ -15,28 +16,31 @@ pub mod ra_setup;
1516
pub mod stderr_length_check;
1617
pub mod update_lints;
1718

18-
lazy_static! {
19-
static ref DEC_CLIPPY_LINT_RE: Regex = Regex::new(
19+
static DEC_CLIPPY_LINT_RE: SyncLazy<Regex> = SyncLazy::new(|| {
20+
Regex::new(
2021
r#"(?x)
21-
declare_clippy_lint!\s*[\{(]
22-
(?:\s+///.*)*
23-
\s+pub\s+(?P<name>[A-Z_][A-Z_0-9]*)\s*,\s*
24-
(?P<cat>[a-z_]+)\s*,\s*
25-
"(?P<desc>(?:[^"\\]+|\\(?s).(?-s))*)"\s*[})]
26-
"#
22+
declare_clippy_lint!\s*[\{(]
23+
(?:\s+///.*)*
24+
\s+pub\s+(?P<name>[A-Z_][A-Z_0-9]*)\s*,\s*
25+
(?P<cat>[a-z_]+)\s*,\s*
26+
"(?P<desc>(?:[^"\\]+|\\(?s).(?-s))*)"\s*[})]
27+
"#,
2728
)
28-
.unwrap();
29-
static ref DEC_DEPRECATED_LINT_RE: Regex = Regex::new(
29+
.unwrap()
30+
});
31+
32+
static DEC_DEPRECATED_LINT_RE: SyncLazy<Regex> = SyncLazy::new(|| {
33+
Regex::new(
3034
r#"(?x)
31-
declare_deprecated_lint!\s*[{(]\s*
32-
(?:\s+///.*)*
33-
\s+pub\s+(?P<name>[A-Z_][A-Z_0-9]*)\s*,\s*
34-
"(?P<desc>(?:[^"\\]+|\\(?s).(?-s))*)"\s*[})]
35-
"#
35+
declare_deprecated_lint!\s*[{(]\s*
36+
(?:\s+///.*)*
37+
\s+pub\s+(?P<name>[A-Z_][A-Z_0-9]*)\s*,\s*
38+
"(?P<desc>(?:[^"\\]+|\\(?s).(?-s))*)"\s*[})]
39+
"#,
3640
)
37-
.unwrap();
38-
static ref NL_ESCAPE_RE: Regex = Regex::new(r#"\\\n\s*"#).unwrap();
39-
}
41+
.unwrap()
42+
});
43+
static NL_ESCAPE_RE: SyncLazy<Regex> = SyncLazy::new(|| Regex::new(r#"\\\n\s*"#).unwrap());
4044

4145
pub static DOCS_LINK: &str = "https://rust-lang.github.io/rust-clippy/master/index.html";
4246

clippy_lints/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ edition = "2018"
2020
cargo_metadata = "0.11.1"
2121
if_chain = "1.0.0"
2222
itertools = "0.9"
23-
lazy_static = "1.0.2"
2423
pulldown-cmark = { version = "0.8", default-features = false }
2524
quine-mc_cluskey = "0.2.2"
2625
regex-syntax = "0.6"

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#![feature(crate_visibility_modifier)]
88
#![feature(drain_filter)]
99
#![feature(in_band_lifetimes)]
10+
#![feature(once_cell)]
1011
#![feature(or_patterns)]
1112
#![feature(rustc_private)]
1213
#![feature(stmt_expr_attributes)]

clippy_lints/src/macro_use.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ declare_clippy_lint! {
1818
/// **Known problems:** None.
1919
///
2020
/// **Example:**
21-
/// ```rust
21+
/// ```rust,ignore
2222
/// #[macro_use]
23-
/// use lazy_static;
23+
/// use some_macro;
2424
/// ```
2525
pub MACRO_USE_IMPORTS,
2626
pedantic,

clippy_lints/src/utils/conf.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
33
#![deny(clippy::missing_docs_in_private_items)]
44

5-
use lazy_static::lazy_static;
65
use rustc_ast::ast::{LitKind, MetaItemKind, NestedMetaItem};
76
use rustc_span::source_map;
87
use source_map::Span;
8+
use std::lazy::SyncLazy;
99
use std::path::{Path, PathBuf};
1010
use std::sync::Mutex;
1111
use std::{env, fmt, fs, io};
@@ -54,9 +54,8 @@ impl From<io::Error> for Error {
5454
}
5555
}
5656

57-
lazy_static! {
58-
static ref ERRORS: Mutex<Vec<Error>> = Mutex::new(Vec::new());
59-
}
57+
/// Vec of errors that might be collected during config toml parsing
58+
static ERRORS: SyncLazy<Mutex<Vec<Error>>> = SyncLazy::new(|| Mutex::new(Vec::new()));
6059

6160
macro_rules! define_Conf {
6261
($(#[$doc:meta] ($config:ident, $config_str:literal: $Ty:ty, $default:expr),)+) => {
@@ -82,6 +81,7 @@ macro_rules! define_Conf {
8281
use serde::Deserialize;
8382
pub fn deserialize<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<$Ty, D::Error> {
8483
use super::super::{ERRORS, Error};
84+
8585
Ok(
8686
<$Ty>::deserialize(deserializer).unwrap_or_else(|e| {
8787
ERRORS

tests/cargo/mod.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
use lazy_static::lazy_static;
21
use std::env;
2+
use std::lazy::SyncLazy;
33
use std::path::PathBuf;
44

5-
lazy_static! {
6-
pub static ref CARGO_TARGET_DIR: PathBuf = {
7-
match env::var_os("CARGO_TARGET_DIR") {
8-
Some(v) => v.into(),
9-
None => env::current_dir().unwrap().join("target"),
10-
}
11-
};
12-
pub static ref TARGET_LIB: PathBuf = {
13-
if let Some(path) = option_env!("TARGET_LIBS") {
14-
path.into()
15-
} else {
16-
let mut dir = CARGO_TARGET_DIR.clone();
17-
if let Some(target) = env::var_os("CARGO_BUILD_TARGET") {
18-
dir.push(target);
19-
}
20-
dir.push(env!("PROFILE"));
21-
dir
5+
pub static CARGO_TARGET_DIR: SyncLazy<PathBuf> = SyncLazy::new(|| match env::var_os("CARGO_TARGET_DIR") {
6+
Some(v) => v.into(),
7+
None => env::current_dir().unwrap().join("target"),
8+
});
9+
10+
pub static TARGET_LIB: SyncLazy<PathBuf> = SyncLazy::new(|| {
11+
if let Some(path) = option_env!("TARGET_LIBS") {
12+
path.into()
13+
} else {
14+
let mut dir = CARGO_TARGET_DIR.clone();
15+
if let Some(target) = env::var_os("CARGO_BUILD_TARGET") {
16+
dir.push(target);
2217
}
23-
};
24-
}
18+
dir.push(env!("PROFILE"));
19+
dir
20+
}
21+
});
2522

2623
#[must_use]
2724
pub fn is_rustc_test_suite() -> bool {

tests/compile-test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(test)] // compiletest_rs requires this attribute
2+
#![feature(once_cell)]
23

34
use compiletest_rs as compiletest;
45
use compiletest_rs::common::Mode as TestMode;

tests/dogfood.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
// Dogfood cannot run on Windows
22
#![cfg(not(windows))]
3+
#![feature(once_cell)]
34

4-
use lazy_static::lazy_static;
5+
use std::lazy::SyncLazy;
56
use std::path::PathBuf;
67
use std::process::Command;
78

89
mod cargo;
910

10-
lazy_static! {
11-
static ref CLIPPY_PATH: PathBuf = cargo::TARGET_LIB.join("cargo-clippy");
12-
}
11+
static CLIPPY_PATH: SyncLazy<PathBuf> = SyncLazy::new(|| cargo::TARGET_LIB.join("cargo-clippy"));
1312

1413
#[test]
1514
fn dogfood_clippy() {

0 commit comments

Comments
 (0)