diff --git a/Cargo.lock b/Cargo.lock index 0241301062db4..aeed169f2d2bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2136,7 +2136,6 @@ version = "0.0.0" dependencies = [ "anyhow", "colored", - "once_cell", "red_knot_python_semantic", "red_knot_vendored", "regex", @@ -2154,7 +2153,6 @@ dependencies = [ name = "red_knot_vendored" version = "0.0.0" dependencies = [ - "once_cell", "path-slash", "ruff_db", "walkdir", @@ -2382,7 +2380,6 @@ dependencies = [ "codspeed-criterion-compat", "criterion", "mimalloc", - "once_cell", "rayon", "red_knot_python_semantic", "red_knot_workspace", @@ -2515,7 +2512,6 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", - "once_cell", "red_knot_python_semantic", "ruff_cache", "ruff_db", @@ -2560,7 +2556,6 @@ dependencies = [ "log", "memchr", "natord", - "once_cell", "path-absolutize", "pathdiff", "pep440_rs 0.6.6", @@ -2616,7 +2611,6 @@ version = "0.0.0" dependencies = [ "anyhow", "itertools 0.13.0", - "once_cell", "rand", "ruff_diagnostics", "ruff_source_file", @@ -2638,7 +2632,6 @@ dependencies = [ "compact_str", "is-macro", "itertools 0.13.0", - "once_cell", "ruff_cache", "ruff_macros", "ruff_python_trivia", @@ -2664,7 +2657,6 @@ dependencies = [ name = "ruff_python_codegen" version = "0.0.0" dependencies = [ - "once_cell", "ruff_python_ast", "ruff_python_literal", "ruff_python_parser", @@ -2682,7 +2674,6 @@ dependencies = [ "insta", "itertools 0.13.0", "memchr", - "once_cell", "regex", "ruff_cache", "ruff_formatter", @@ -2843,7 +2834,6 @@ name = "ruff_source_file" version = "0.0.0" dependencies = [ "memchr", - "once_cell", "ruff_text_size", "serde", ] diff --git a/Cargo.toml b/Cargo.toml index 57c0712e482ff..f741b5c39f6b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ resolver = "2" [workspace.package] edition = "2021" -rust-version = "1.76" +rust-version = "1.80" homepage = "https://docs.astral.sh/ruff" documentation = "https://docs.astral.sh/ruff" repository = "https://github.com/astral-sh/ruff" @@ -101,7 +101,6 @@ memchr = { version = "2.7.1" } mimalloc = { version = "0.1.39" } natord = { version = "1.0.9" } notify = { version = "6.1.1" } -once_cell = { version = "1.19.0" } ordermap = { version = "0.5.0" } path-absolutize = { version = "3.1.1" } path-slash = { version = "0.2.1" } diff --git a/crates/red_knot_test/Cargo.toml b/crates/red_knot_test/Cargo.toml index 5fcc0b9143edb..2de1e9a6622f1 100644 --- a/crates/red_knot_test/Cargo.toml +++ b/crates/red_knot_test/Cargo.toml @@ -21,7 +21,6 @@ ruff_text_size = { workspace = true } anyhow = { workspace = true } colored = { workspace = true } -once_cell = { workspace = true } regex = { workspace = true } rustc-hash = { workspace = true } salsa = { workspace = true } diff --git a/crates/red_knot_test/src/assertion.rs b/crates/red_knot_test/src/assertion.rs index 160214e36b652..2355b19f2f748 100644 --- a/crates/red_knot_test/src/assertion.rs +++ b/crates/red_knot_test/src/assertion.rs @@ -35,7 +35,6 @@ //! ``` use crate::db::Db; -use once_cell::sync::Lazy; use regex::Regex; use ruff_db::files::File; use ruff_db::parsed::parsed_module; @@ -45,6 +44,7 @@ use ruff_source_file::{LineIndex, Locator, OneIndexed}; use ruff_text_size::{Ranged, TextRange}; use smallvec::SmallVec; use std::ops::Deref; +use std::sync::LazyLock; /// Diagnostic assertion comments in a single embedded file. #[derive(Debug)] @@ -239,10 +239,10 @@ impl<'a> Deref for LineAssertions<'a> { } } -static TYPE_RE: Lazy = - Lazy::new(|| Regex::new(r"^#\s*revealed:\s*(?.+?)\s*$").unwrap()); +static TYPE_RE: LazyLock = + LazyLock::new(|| Regex::new(r"^#\s*revealed:\s*(?.+?)\s*$").unwrap()); -static ERROR_RE: Lazy = Lazy::new(|| { +static ERROR_RE: LazyLock = LazyLock::new(|| { Regex::new( r#"^#\s*error:(\s*(?\d+))?(\s*\[(?.+?)\])?(\s*"(?.+?)")?\s*$"#, ) diff --git a/crates/red_knot_test/src/parser.rs b/crates/red_knot_test/src/parser.rs index 9d760f9036231..9c5f3a19559da 100644 --- a/crates/red_knot_test/src/parser.rs +++ b/crates/red_knot_test/src/parser.rs @@ -1,7 +1,7 @@ -use once_cell::sync::Lazy; use regex::{Captures, Regex}; use ruff_index::{newtype_index, IndexVec}; use rustc_hash::{FxHashMap, FxHashSet}; +use std::sync::LazyLock; /// Parse the Markdown `source` as a test suite with given `title`. pub(crate) fn parse<'s>(title: &'s str, source: &'s str) -> anyhow::Result> { @@ -135,12 +135,12 @@ pub(crate) struct EmbeddedFile<'s> { /// Matches an arbitrary amount of whitespace (including newlines), followed by a sequence of `#` /// characters, followed by a title heading, followed by a newline. -static HEADER_RE: Lazy = - Lazy::new(|| Regex::new(r"^(\s*\n)*(?#+)\s+(?.+)\s*\n").unwrap()); +static HEADER_RE: LazyLock<Regex> = + LazyLock::new(|| Regex::new(r"^(\s*\n)*(?<level>#+)\s+(?<title>.+)\s*\n").unwrap()); /// Matches a code block fenced by triple backticks, possibly with language and `key=val` /// configuration items following the opening backticks (in the "tag string" of the code block). -static CODE_RE: Lazy<Regex> = Lazy::new(|| { +static CODE_RE: LazyLock<Regex> = LazyLock::new(|| { Regex::new(r"^```(?<lang>\w+)(?<config>( +\S+)*)\s*\n(?<code>(.|\n)*?)\n?```\s*\n").unwrap() }); diff --git a/crates/red_knot_vendored/Cargo.toml b/crates/red_knot_vendored/Cargo.toml index 72c7dd542078e..b740bef3f4db5 100644 --- a/crates/red_knot_vendored/Cargo.toml +++ b/crates/red_knot_vendored/Cargo.toml @@ -12,7 +12,6 @@ license = { workspace = true } [dependencies] ruff_db = { workspace = true } -once_cell = { workspace = true } zip = { workspace = true } [build-dependencies] diff --git a/crates/red_knot_vendored/src/lib.rs b/crates/red_knot_vendored/src/lib.rs index de384cd570b97..d1a5816ec2383 100644 --- a/crates/red_knot_vendored/src/lib.rs +++ b/crates/red_knot_vendored/src/lib.rs @@ -1,14 +1,13 @@ -use once_cell::sync::Lazy; - use ruff_db::vendored::VendoredFileSystem; +use std::sync::LazyLock; // The file path here is hardcoded in this crate's `build.rs` script. // Luckily this crate will fail to build if this file isn't available at build time. static TYPESHED_ZIP_BYTES: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/zipped_typeshed.zip")); pub fn file_system() -> &'static VendoredFileSystem { - static VENDORED_TYPESHED_STUBS: Lazy<VendoredFileSystem> = - Lazy::new(|| VendoredFileSystem::new_static(TYPESHED_ZIP_BYTES).unwrap()); + static VENDORED_TYPESHED_STUBS: LazyLock<VendoredFileSystem> = + LazyLock::new(|| VendoredFileSystem::new_static(TYPESHED_ZIP_BYTES).unwrap()); &VENDORED_TYPESHED_STUBS } diff --git a/crates/ruff_benchmark/Cargo.toml b/crates/ruff_benchmark/Cargo.toml index 05328759cd722..c6c8510145d69 100644 --- a/crates/ruff_benchmark/Cargo.toml +++ b/crates/ruff_benchmark/Cargo.toml @@ -39,7 +39,6 @@ harness = false [dependencies] codspeed-criterion-compat = { workspace = true, default-features = false, optional = true } criterion = { workspace = true, default-features = false } -once_cell = { workspace = true } rayon = { workspace = true } rustc-hash = { workspace = true } serde = { workspace = true } diff --git a/crates/ruff_benchmark/src/lib.rs b/crates/ruff_benchmark/src/lib.rs index 5fcf31bf7e586..cb6236c29a1e5 100644 --- a/crates/ruff_benchmark/src/lib.rs +++ b/crates/ruff_benchmark/src/lib.rs @@ -82,7 +82,7 @@ impl TestFile { } } -static TARGET_DIR: once_cell::sync::Lazy<PathBuf> = once_cell::sync::Lazy::new(|| { +static TARGET_DIR: std::sync::LazyLock<PathBuf> = std::sync::LazyLock::new(|| { cargo_target_directory().unwrap_or_else(|| PathBuf::from("target")) }); diff --git a/crates/ruff_graph/Cargo.toml b/crates/ruff_graph/Cargo.toml index 9bb0a67d9ca9d..645c9cd2e4255 100644 --- a/crates/ruff_graph/Cargo.toml +++ b/crates/ruff_graph/Cargo.toml @@ -20,7 +20,6 @@ ruff_python_parser = { workspace = true } anyhow = { workspace = true } clap = { workspace = true, optional = true } -once_cell = { workspace = true } salsa = { workspace = true } schemars = { workspace = true, optional = true } serde = { workspace = true, optional = true } diff --git a/crates/ruff_graph/src/db.rs b/crates/ruff_graph/src/db.rs index a84168763131f..39f74081bc83d 100644 --- a/crates/ruff_graph/src/db.rs +++ b/crates/ruff_graph/src/db.rs @@ -7,12 +7,11 @@ use ruff_db::system::{OsSystem, System, SystemPathBuf}; use ruff_db::vendored::{VendoredFileSystem, VendoredFileSystemBuilder}; use ruff_db::{Db as SourceDb, Upcast}; -static EMPTY_VENDORED: once_cell::sync::Lazy<VendoredFileSystem> = - once_cell::sync::Lazy::new(|| { - let mut builder = VendoredFileSystemBuilder::new(CompressionMethod::Stored); - builder.add_file("stdlib/VERSIONS", "\n").unwrap(); - builder.finish().unwrap() - }); +static EMPTY_VENDORED: std::sync::LazyLock<VendoredFileSystem> = std::sync::LazyLock::new(|| { + let mut builder = VendoredFileSystemBuilder::new(CompressionMethod::Stored); + builder.add_file("stdlib/VERSIONS", "\n").unwrap(); + builder.finish().unwrap() +}); #[salsa::db] #[derive(Default)] diff --git a/crates/ruff_linter/Cargo.toml b/crates/ruff_linter/Cargo.toml index eded5f03f457d..6940b1a4268ab 100644 --- a/crates/ruff_linter/Cargo.toml +++ b/crates/ruff_linter/Cargo.toml @@ -46,10 +46,9 @@ libcst = { workspace = true } log = { workspace = true } memchr = { workspace = true } natord = { workspace = true } -once_cell = { workspace = true } path-absolutize = { workspace = true, features = [ - "once_cell_cache", - "use_unix_paths_on_wasm", + "once_cell_cache", + "use_unix_paths_on_wasm", ] } pathdiff = { workspace = true } pep440_rs = { workspace = true, features = ["serde"] } diff --git a/crates/ruff_linter/src/linter.rs b/crates/ruff_linter/src/linter.rs index ca7dc608f746c..6958b298cf3a3 100644 --- a/crates/ruff_linter/src/linter.rs +++ b/crates/ruff_linter/src/linter.rs @@ -1,4 +1,5 @@ use std::borrow::Cow; +use std::cell::LazyCell; use std::ops::Deref; use std::path::Path; @@ -440,7 +441,7 @@ fn diagnostics_to_messages( locator: &Locator, directives: &Directives, ) -> Vec<Message> { - let file = once_cell::unsync::Lazy::new(|| { + let file = LazyCell::new(|| { let mut builder = SourceFileBuilder::new(path.to_string_lossy().as_ref(), locator.contents()); diff --git a/crates/ruff_linter/src/logging.rs b/crates/ruff_linter/src/logging.rs index ce89402231ffc..4d88c637bcc43 100644 --- a/crates/ruff_linter/src/logging.rs +++ b/crates/ruff_linter/src/logging.rs @@ -1,12 +1,11 @@ use std::fmt::{Display, Formatter, Write}; use std::path::{Path, PathBuf}; -use std::sync::Mutex; +use std::sync::{LazyLock, Mutex}; use anyhow::Result; use colored::Colorize; use fern; use log::Level; -use once_cell::sync::Lazy; use ruff_python_parser::{ParseError, ParseErrorType}; use rustc_hash::FxHashSet; @@ -16,7 +15,7 @@ use crate::fs; use crate::source_kind::SourceKind; use ruff_notebook::Notebook; -pub static IDENTIFIERS: Lazy<Mutex<Vec<&'static str>>> = Lazy::new(Mutex::default); +pub static IDENTIFIERS: LazyLock<Mutex<Vec<&'static str>>> = LazyLock::new(Mutex::default); /// Warn a user once, with uniqueness determined by the given ID. #[macro_export] @@ -35,7 +34,7 @@ macro_rules! warn_user_once_by_id { }; } -pub static MESSAGES: Lazy<Mutex<FxHashSet<String>>> = Lazy::new(Mutex::default); +pub static MESSAGES: LazyLock<Mutex<FxHashSet<String>>> = LazyLock::new(Mutex::default); /// Warn a user once, if warnings are enabled, with uniqueness determined by the content of the /// message. diff --git a/crates/ruff_linter/src/rule_redirects.rs b/crates/ruff_linter/src/rule_redirects.rs index ab5ae7c7a7008..ccbd4ca571627 100644 --- a/crates/ruff_linter/src/rule_redirects.rs +++ b/crates/ruff_linter/src/rule_redirects.rs @@ -1,6 +1,5 @@ use std::collections::HashMap; - -use once_cell::sync::Lazy; +use std::sync::LazyLock; /// Returns the redirect target for the given code. pub(crate) fn get_redirect_target(code: &str) -> Option<&'static str> { @@ -13,7 +12,7 @@ pub(crate) fn get_redirect(code: &str) -> Option<(&'static str, &'static str)> { REDIRECTS.get_key_value(code).map(|(k, v)| (*k, *v)) } -static REDIRECTS: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| { +static REDIRECTS: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| { HashMap::from_iter([ // The following are here because we don't yet have the many-to-one mapping enabled. ("SIM111", "SIM110"), diff --git a/crates/ruff_linter/src/rules/eradicate/detection.rs b/crates/ruff_linter/src/rules/eradicate/detection.rs index f10efcaf5b212..c3ba54a199350 100644 --- a/crates/ruff_linter/src/rules/eradicate/detection.rs +++ b/crates/ruff_linter/src/rules/eradicate/detection.rs @@ -1,29 +1,28 @@ /// See: [eradicate.py](https://github.com/myint/eradicate/blob/98f199940979c94447a461d50d27862b118b282d/eradicate.py) use aho_corasick::AhoCorasick; use itertools::Itertools; -use once_cell::sync::Lazy; use regex::{Regex, RegexSet}; - use ruff_python_parser::parse_module; use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer}; use ruff_text_size::TextSize; +use std::sync::LazyLock; -static CODE_INDICATORS: Lazy<AhoCorasick> = Lazy::new(|| { +static CODE_INDICATORS: LazyLock<AhoCorasick> = LazyLock::new(|| { AhoCorasick::new([ "(", ")", "[", "]", "{", "}", ":", "=", "%", "return", "break", "continue", "import", ]) .unwrap() }); -static ALLOWLIST_REGEX: Lazy<Regex> = Lazy::new(|| { +static ALLOWLIST_REGEX: LazyLock<Regex> = LazyLock::new(|| { Regex::new( r"^(?i)(?:pylint|pyright|noqa|nosec|region|endregion|type:\s*ignore|fmt:\s*(on|off)|isort:\s*(on|off|skip|skip_file|split|dont-add-imports(:\s*\[.*?])?)|mypy:|SPDX-License-Identifier:|(?:en)?coding[:=][ \t]*([-_.a-zA-Z0-9]+))", ).unwrap() }); -static HASH_NUMBER: Lazy<Regex> = Lazy::new(|| Regex::new(r"#\d").unwrap()); +static HASH_NUMBER: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"#\d").unwrap()); -static POSITIVE_CASES: Lazy<RegexSet> = Lazy::new(|| { +static POSITIVE_CASES: LazyLock<RegexSet> = LazyLock::new(|| { RegexSet::new([ // Keywords r"^(?:elif\s+.*\s*:.*|else\s*:.*|try\s*:.*|finally\s*:.*|except.*:.*|case\s+.*\s*:.*)$", diff --git a/crates/ruff_linter/src/rules/flake8_bandit/helpers.rs b/crates/ruff_linter/src/rules/flake8_bandit/helpers.rs index 9f287804ff68d..a79d707f8aa13 100644 --- a/crates/ruff_linter/src/rules/flake8_bandit/helpers.rs +++ b/crates/ruff_linter/src/rules/flake8_bandit/helpers.rs @@ -1,10 +1,10 @@ -use once_cell::sync::Lazy; use regex::Regex; use ruff_python_ast::{self as ast, Expr}; +use std::sync::LazyLock; use ruff_python_semantic::SemanticModel; -static PASSWORD_CANDIDATE_REGEX: Lazy<Regex> = Lazy::new(|| { +static PASSWORD_CANDIDATE_REGEX: LazyLock<Regex> = LazyLock::new(|| { Regex::new(r"(^|_)(?i)(pas+wo?r?d|pass(phrase)?|pwd|token|secrete?)($|_)").unwrap() }); diff --git a/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_sql_expression.rs b/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_sql_expression.rs index 670c7d406fcf1..e68d0dcce5f07 100644 --- a/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_sql_expression.rs +++ b/crates/ruff_linter/src/rules/flake8_bandit/rules/hardcoded_sql_expression.rs @@ -1,5 +1,7 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; + use regex::Regex; + use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::str::raw_contents; @@ -9,7 +11,7 @@ use ruff_text_size::Ranged; use crate::checkers::ast::Checker; -static SQL_REGEX: Lazy<Regex> = Lazy::new(|| { +static SQL_REGEX: LazyLock<Regex> = LazyLock::new(|| { Regex::new(r"(?i)\b(select\s+.*\s+from\s|delete\s+from\s|(insert|replace)\s+.*\s+values\s|update\s+.*\s+set\s)") .unwrap() }); diff --git a/crates/ruff_linter/src/rules/flake8_copyright/settings.rs b/crates/ruff_linter/src/rules/flake8_copyright/settings.rs index 03551b0a0a93f..8eb50bc0a39b0 100644 --- a/crates/ruff_linter/src/rules/flake8_copyright/settings.rs +++ b/crates/ruff_linter/src/rules/flake8_copyright/settings.rs @@ -1,12 +1,14 @@ //! Settings for the `flake8-copyright` plugin. -use once_cell::sync::Lazy; -use regex::Regex; use std::fmt::{Display, Formatter}; +use std::sync::LazyLock; + +use regex::Regex; -use crate::display_settings; use ruff_macros::CacheKey; +use crate::display_settings; + #[derive(Debug, Clone, CacheKey)] pub struct Settings { pub notice_rgx: Regex, @@ -14,8 +16,8 @@ pub struct Settings { pub min_file_size: usize, } -pub static COPYRIGHT: Lazy<Regex> = - Lazy::new(|| Regex::new(r"(?i)Copyright\s+((?:\(C\)|©)\s+)?\d{4}((-|,\s)\d{4})*").unwrap()); +pub static COPYRIGHT: LazyLock<Regex> = + LazyLock::new(|| Regex::new(r"(?i)Copyright\s+((?:\(C\)|©)\s+)?\d{4}((-|,\s)\d{4})*").unwrap()); impl Default for Settings { fn default() -> Self { diff --git a/crates/ruff_linter/src/rules/flake8_pyi/rules/type_comment_in_stub.rs b/crates/ruff_linter/src/rules/flake8_pyi/rules/type_comment_in_stub.rs index ddb8acd333631..719e4b6212f0b 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/rules/type_comment_in_stub.rs +++ b/crates/ruff_linter/src/rules/flake8_pyi/rules/type_comment_in_stub.rs @@ -1,7 +1,7 @@ -use once_cell::sync::Lazy; use regex::Regex; use ruff_python_trivia::CommentRanges; use ruff_source_file::Locator; +use std::sync::LazyLock; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; @@ -49,8 +49,8 @@ pub(crate) fn type_comment_in_stub( } } -static TYPE_COMMENT_REGEX: Lazy<Regex> = - Lazy::new(|| Regex::new(r"^#\s*type:\s*([^#]+)(\s*#.*?)?$").unwrap()); +static TYPE_COMMENT_REGEX: LazyLock<Regex> = + LazyLock::new(|| Regex::new(r"^#\s*type:\s*([^#]+)(\s*#.*?)?$").unwrap()); -static TYPE_IGNORE_REGEX: Lazy<Regex> = - Lazy::new(|| Regex::new(r"^#\s*type:\s*ignore([^#]+)?(\s*#.*?)?$").unwrap()); +static TYPE_IGNORE_REGEX: LazyLock<Regex> = + LazyLock::new(|| Regex::new(r"^#\s*type:\s*ignore([^#]+)?(\s*#.*?)?$").unwrap()); diff --git a/crates/ruff_linter/src/rules/flake8_todos/rules/todos.rs b/crates/ruff_linter/src/rules/flake8_todos/rules/todos.rs index 35f1d6039fcf9..491d94a2f217c 100644 --- a/crates/ruff_linter/src/rules/flake8_todos/rules/todos.rs +++ b/crates/ruff_linter/src/rules/flake8_todos/rules/todos.rs @@ -1,8 +1,8 @@ -use once_cell::sync::Lazy; use regex::RegexSet; use ruff_python_trivia::CommentRanges; use ruff_source_file::Locator; use ruff_text_size::{TextLen, TextRange, TextSize}; +use std::sync::LazyLock; use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; @@ -222,7 +222,7 @@ impl Violation for MissingSpaceAfterTodoColon { } } -static ISSUE_LINK_REGEX_SET: Lazy<RegexSet> = Lazy::new(|| { +static ISSUE_LINK_REGEX_SET: LazyLock<RegexSet> = LazyLock::new(|| { RegexSet::new([ r"^#\s*(http|https)://.*", // issue link r"^#\s*\d+$", // issue code - like "003" diff --git a/crates/ruff_linter/src/rules/pydocstyle/rules/blank_before_after_function.rs b/crates/ruff_linter/src/rules/pydocstyle/rules/blank_before_after_function.rs index 25e335e80eb62..11027c562dee3 100644 --- a/crates/ruff_linter/src/rules/pydocstyle/rules/blank_before_after_function.rs +++ b/crates/ruff_linter/src/rules/pydocstyle/rules/blank_before_after_function.rs @@ -1,5 +1,5 @@ -use once_cell::sync::Lazy; use regex::Regex; +use std::sync::LazyLock; use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; @@ -98,8 +98,8 @@ impl AlwaysFixableViolation for NoBlankLineAfterFunction { } } -static INNER_FUNCTION_OR_CLASS_REGEX: Lazy<Regex> = - Lazy::new(|| Regex::new(r"^\s+(?:(?:class|def|async def)\s|@)").unwrap()); +static INNER_FUNCTION_OR_CLASS_REGEX: LazyLock<Regex> = + LazyLock::new(|| Regex::new(r"^\s+(?:(?:class|def|async def)\s|@)").unwrap()); /// D201, D202 pub(crate) fn blank_before_after_function(checker: &mut Checker, docstring: &Docstring) { diff --git a/crates/ruff_linter/src/rules/pydocstyle/rules/non_imperative_mood.rs b/crates/ruff_linter/src/rules/pydocstyle/rules/non_imperative_mood.rs index 2391aeb11a131..7be580cd1b496 100644 --- a/crates/ruff_linter/src/rules/pydocstyle/rules/non_imperative_mood.rs +++ b/crates/ruff_linter/src/rules/pydocstyle/rules/non_imperative_mood.rs @@ -1,5 +1,6 @@ +use std::sync::LazyLock; + use imperative::Mood; -use once_cell::sync::Lazy; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; @@ -12,7 +13,7 @@ use crate::docstrings::Docstring; use crate::rules::pydocstyle::helpers::normalize_word; use crate::rules::pydocstyle::settings::Settings; -static MOOD: Lazy<Mood> = Lazy::new(Mood::new); +static MOOD: LazyLock<Mood> = LazyLock::new(Mood::new); /// ## What it does /// Checks for docstring first lines that are not in an imperative mood. diff --git a/crates/ruff_linter/src/rules/pydocstyle/rules/sections.rs b/crates/ruff_linter/src/rules/pydocstyle/rules/sections.rs index 10346ec19482a..e18d7948ea1bd 100644 --- a/crates/ruff_linter/src/rules/pydocstyle/rules/sections.rs +++ b/crates/ruff_linter/src/rules/pydocstyle/rules/sections.rs @@ -1,7 +1,7 @@ use itertools::Itertools; -use once_cell::sync::Lazy; use regex::Regex; use rustc_hash::FxHashSet; +use std::sync::LazyLock; use ruff_diagnostics::{AlwaysFixableViolation, Violation}; use ruff_diagnostics::{Diagnostic, Edit, Fix}; @@ -1846,8 +1846,8 @@ fn missing_args(checker: &mut Checker, docstring: &Docstring, docstrings_args: & } // See: `GOOGLE_ARGS_REGEX` in `pydocstyle/checker.py`. -static GOOGLE_ARGS_REGEX: Lazy<Regex> = - Lazy::new(|| Regex::new(r"^\s*(\*?\*?\w+)\s*(\(.*?\))?\s*:(\r\n|\n)?\s*.+").unwrap()); +static GOOGLE_ARGS_REGEX: LazyLock<Regex> = + LazyLock::new(|| Regex::new(r"^\s*(\*?\*?\w+)\s*(\(.*?\))?\s*:(\r\n|\n)?\s*.+").unwrap()); fn args_section(context: &SectionContext) -> FxHashSet<String> { let mut following_lines = context.following_lines().peekable(); diff --git a/crates/ruff_linter/src/rules/pygrep_hooks/rules/blanket_type_ignore.rs b/crates/ruff_linter/src/rules/pygrep_hooks/rules/blanket_type_ignore.rs index 1fd73eb5c661e..662d76709968f 100644 --- a/crates/ruff_linter/src/rules/pygrep_hooks/rules/blanket_type_ignore.rs +++ b/crates/ruff_linter/src/rules/pygrep_hooks/rules/blanket_type_ignore.rs @@ -1,7 +1,7 @@ use anyhow::{anyhow, Result}; use memchr::memchr_iter; -use once_cell::sync::Lazy; use regex::Regex; +use std::sync::LazyLock; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; @@ -102,8 +102,8 @@ pub(crate) fn blanket_type_ignore( // Match, e.g., `[attr-defined]` or `[attr-defined, misc]`. // See: https://github.com/python/mypy/blob/b43e0d34247a6d1b3b9d9094d184bbfcb9808bb9/mypy/fastparse.py#L327 -static TYPE_IGNORE_TAG_PATTERN: Lazy<Regex> = - Lazy::new(|| Regex::new(r"^\s*\[(?P<codes>[^]#]*)]\s*(#.*)?$").unwrap()); +static TYPE_IGNORE_TAG_PATTERN: LazyLock<Regex> = + LazyLock::new(|| Regex::new(r"^\s*\[(?P<codes>[^]#]*)]\s*(#.*)?$").unwrap()); /// Parse the optional `[...]` tag in a `# type: ignore[...]` comment. /// diff --git a/crates/ruff_linter/src/rules/pyupgrade/helpers.rs b/crates/ruff_linter/src/rules/pyupgrade/helpers.rs index 07196a9127332..84108102078f4 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/helpers.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/helpers.rs @@ -1,8 +1,9 @@ -use once_cell::sync::Lazy; use regex::{Captures, Regex}; use std::borrow::Cow; +use std::sync::LazyLock; -static CURLY_BRACES: Lazy<Regex> = Lazy::new(|| Regex::new(r"(\\N\{[^}]+})|([{}])").unwrap()); +static CURLY_BRACES: LazyLock<Regex> = + LazyLock::new(|| Regex::new(r"(\\N\{[^}]+})|([{}])").unwrap()); pub(super) fn curly_escape(text: &str) -> Cow<'_, str> { // Match all curly braces. This will include named unicode escapes (like @@ -20,7 +21,8 @@ pub(super) fn curly_escape(text: &str) -> Cow<'_, str> { }) } -static DOUBLE_CURLY_BRACES: Lazy<Regex> = Lazy::new(|| Regex::new(r"((\{\{)|(\}\}))").unwrap()); +static DOUBLE_CURLY_BRACES: LazyLock<Regex> = + LazyLock::new(|| Regex::new(r"((\{\{)|(\}\}))").unwrap()); pub(super) fn curly_unescape(text: &str) -> Cow<'_, str> { // Match all double curly braces and replace with a single diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/deprecated_unittest_alias.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/deprecated_unittest_alias.rs index 6205df6ec0fc8..431ef71b90fac 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/deprecated_unittest_alias.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/deprecated_unittest_alias.rs @@ -1,6 +1,6 @@ -use once_cell::sync::Lazy; use ruff_python_ast::{self as ast, Expr}; use rustc_hash::FxHashMap; +use std::sync::LazyLock; use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; @@ -57,7 +57,7 @@ impl AlwaysFixableViolation for DeprecatedUnittestAlias { } } -static DEPRECATED_ALIASES: Lazy<FxHashMap<&'static str, &'static str>> = Lazy::new(|| { +static DEPRECATED_ALIASES: LazyLock<FxHashMap<&'static str, &'static str>> = LazyLock::new(|| { FxHashMap::from_iter([ ("assertAlmostEquals", "assertAlmostEqual"), ("assertEquals", "assertEqual"), diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/format_literals.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/format_literals.rs index ab6753f0c8ad3..dd1f4e943d6f4 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/format_literals.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/format_literals.rs @@ -1,7 +1,7 @@ use anyhow::{anyhow, Result}; use libcst_native::{Arg, Expression}; -use once_cell::sync::Lazy; use regex::Regex; +use std::sync::LazyLock; use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation}; use ruff_macros::{derive_message_formats, violation}; @@ -127,8 +127,8 @@ fn is_sequential(indices: &[usize]) -> bool { // An opening curly brace, followed by any integer, followed by any text, // followed by a closing brace. -static FORMAT_SPECIFIER: Lazy<Regex> = - Lazy::new(|| Regex::new(r"\{(?P<int>\d+)(?P<fmt>.*?)}").unwrap()); +static FORMAT_SPECIFIER: LazyLock<Regex> = + LazyLock::new(|| Regex::new(r"\{(?P<int>\d+)(?P<fmt>.*?)}").unwrap()); /// Remove the explicit positional indices from a format string. fn remove_specifiers<'a>(value: &mut Expression<'a>, arena: &'a typed_arena::Arena<String>) { diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs index 68b0ee777f391..f48c05a474264 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/unnecessary_coding_comment.rs @@ -1,5 +1,5 @@ -use once_cell::sync::Lazy; use regex::Regex; +use std::sync::LazyLock; use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; @@ -42,8 +42,8 @@ impl AlwaysFixableViolation for UTF8EncodingDeclaration { } // Regex from PEP263. -static CODING_COMMENT_REGEX: Lazy<Regex> = - Lazy::new(|| Regex::new(r"^[ \t\f]*#.*?coding[:=][ \t]*utf-?8").unwrap()); +static CODING_COMMENT_REGEX: LazyLock<Regex> = + LazyLock::new(|| Regex::new(r"^[ \t\f]*#.*?coding[:=][ \t]*utf-?8").unwrap()); /// UP009 pub(crate) fn unnecessary_coding_comment( diff --git a/crates/ruff_linter/src/settings/mod.rs b/crates/ruff_linter/src/settings/mod.rs index 06e6239bf0c0f..2bc98bc930a8f 100644 --- a/crates/ruff_linter/src/settings/mod.rs +++ b/crates/ruff_linter/src/settings/mod.rs @@ -2,13 +2,12 @@ //! command-line options. Structure is optimized for internal usage, as opposed //! to external visibility or parsing. -use std::fmt::{Display, Formatter}; -use std::path::{Path, PathBuf}; - -use once_cell::sync::Lazy; use path_absolutize::path_dedot; use regex::Regex; use rustc_hash::FxHashSet; +use std::fmt::{Display, Formatter}; +use std::path::{Path, PathBuf}; +use std::sync::LazyLock; use crate::codes::RuleCodePrefix; use ruff_macros::CacheKey; @@ -355,8 +354,8 @@ pub const DEFAULT_SELECTORS: &[RuleSelector] = &[ pub const TASK_TAGS: &[&str] = &["TODO", "FIXME", "XXX"]; -pub static DUMMY_VARIABLE_RGX: Lazy<Regex> = - Lazy::new(|| Regex::new("^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$").unwrap()); +pub static DUMMY_VARIABLE_RGX: LazyLock<Regex> = + LazyLock::new(|| Regex::new("^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$").unwrap()); impl LinterSettings { pub fn for_rule(rule_code: Rule) -> Self { diff --git a/crates/ruff_notebook/Cargo.toml b/crates/ruff_notebook/Cargo.toml index 6de68f30415dd..c8273399edfaa 100644 --- a/crates/ruff_notebook/Cargo.toml +++ b/crates/ruff_notebook/Cargo.toml @@ -20,7 +20,6 @@ ruff_text_size = { workspace = true } anyhow = { workspace = true } itertools = { workspace = true } -once_cell = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } serde_with = { workspace = true, default-features = false, features = ["macros"] } diff --git a/crates/ruff_notebook/src/notebook.rs b/crates/ruff_notebook/src/notebook.rs index 9098227f7c2d4..3ec9108e0373d 100644 --- a/crates/ruff_notebook/src/notebook.rs +++ b/crates/ruff_notebook/src/notebook.rs @@ -1,15 +1,14 @@ +use itertools::Itertools; +use rand::{Rng, SeedableRng}; +use serde::Serialize; +use serde_json::error::Category; use std::cmp::Ordering; use std::collections::HashSet; use std::fs::File; use std::io::{BufReader, Cursor, Read, Seek, SeekFrom, Write}; use std::path::Path; +use std::sync::OnceLock; use std::{io, iter}; - -use itertools::Itertools; -use once_cell::sync::OnceCell; -use rand::{Rng, SeedableRng}; -use serde::Serialize; -use serde_json::error::Category; use thiserror::Error; use ruff_diagnostics::{SourceMap, SourceMarker}; @@ -63,7 +62,7 @@ pub struct Notebook { source_code: String, /// The index of the notebook. This is used to map between the concatenated /// source code and the original notebook. - index: OnceCell<NotebookIndex>, + index: OnceLock<NotebookIndex>, /// The raw notebook i.e., the deserialized version of JSON string. raw: RawNotebook, /// The offsets of each cell in the concatenated source code. This includes @@ -194,7 +193,7 @@ impl Notebook { Ok(Self { raw: raw_notebook, - index: OnceCell::new(), + index: OnceLock::new(), // The additional newline at the end is to maintain consistency for // all cells. These newlines will be removed before updating the // source code with the transformed content. Refer `update_cell_content`. diff --git a/crates/ruff_python_ast/Cargo.toml b/crates/ruff_python_ast/Cargo.toml index 401f56975cba9..497782a4eef59 100644 --- a/crates/ruff_python_ast/Cargo.toml +++ b/crates/ruff_python_ast/Cargo.toml @@ -23,7 +23,6 @@ aho-corasick = { workspace = true } bitflags = { workspace = true } is-macro = { workspace = true } itertools = { workspace = true } -once_cell = { workspace = true } rustc-hash = { workspace = true } schemars = { workspace = true, optional = true } serde = { workspace = true, optional = true } diff --git a/crates/ruff_python_ast/src/str.rs b/crates/ruff_python_ast/src/str.rs index 220df07857f70..13a08d2ccc793 100644 --- a/crates/ruff_python_ast/src/str.rs +++ b/crates/ruff_python_ast/src/str.rs @@ -1,7 +1,6 @@ -use std::fmt; - use aho_corasick::{AhoCorasick, AhoCorasickKind, Anchored, Input, MatchKind, StartKind}; -use once_cell::sync::Lazy; +use std::fmt; +use std::sync::LazyLock; use ruff_text_size::{TextLen, TextRange}; @@ -205,7 +204,7 @@ pub fn raw_contents_range(contents: &str) -> Option<TextRange> { } /// An [`AhoCorasick`] matcher for string and byte literal prefixes. -static PREFIX_MATCHER: Lazy<AhoCorasick> = Lazy::new(|| { +static PREFIX_MATCHER: LazyLock<AhoCorasick> = LazyLock::new(|| { AhoCorasick::builder() .start_kind(StartKind::Anchored) .match_kind(MatchKind::LeftmostLongest) diff --git a/crates/ruff_python_codegen/Cargo.toml b/crates/ruff_python_codegen/Cargo.toml index cf273027bb79d..35dcbc30116d9 100644 --- a/crates/ruff_python_codegen/Cargo.toml +++ b/crates/ruff_python_codegen/Cargo.toml @@ -20,7 +20,6 @@ ruff_python_parser = { workspace = true } ruff_source_file = { workspace = true } ruff_text_size = { workspace = true } -once_cell = { workspace = true } [lints] workspace = true diff --git a/crates/ruff_python_codegen/src/stylist.rs b/crates/ruff_python_codegen/src/stylist.rs index ea37492c2f0dd..b78417add2971 100644 --- a/crates/ruff_python_codegen/src/stylist.rs +++ b/crates/ruff_python_codegen/src/stylist.rs @@ -1,9 +1,8 @@ //! Detect code style from Python source code. +use std::cell::OnceCell; use std::ops::Deref; -use once_cell::unsync::OnceCell; - use ruff_python_ast::str::Quote; use ruff_python_parser::{Token, TokenKind, Tokens}; use ruff_source_file::{find_newline, LineEnding, Locator}; diff --git a/crates/ruff_python_formatter/Cargo.toml b/crates/ruff_python_formatter/Cargo.toml index befb78e31f4ff..3341f94027c70 100644 --- a/crates/ruff_python_formatter/Cargo.toml +++ b/crates/ruff_python_formatter/Cargo.toml @@ -28,7 +28,6 @@ clap = { workspace = true } countme = { workspace = true } itertools = { workspace = true } memchr = { workspace = true } -once_cell = { workspace = true } regex = { workspace = true } rustc-hash = { workspace = true } serde = { workspace = true, optional = true } diff --git a/crates/ruff_python_formatter/src/string/docstring.rs b/crates/ruff_python_formatter/src/string/docstring.rs index 5c177a0142acd..bb1afeee76454 100644 --- a/crates/ruff_python_formatter/src/string/docstring.rs +++ b/crates/ruff_python_formatter/src/string/docstring.rs @@ -2,15 +2,15 @@ // "reStructuredText." #![allow(clippy::doc_markdown)] +use itertools::Itertools; use std::cmp::Ordering; +use std::sync::LazyLock; use std::{borrow::Cow, collections::VecDeque}; -use itertools::Itertools; - +use regex::Regex; use ruff_formatter::printer::SourceMapGeneration; use ruff_python_ast::{str::Quote, StringFlags}; use ruff_python_trivia::CommentRanges; -use {once_cell::sync::Lazy, regex::Regex}; use { ruff_formatter::{write, FormatOptions, IndentStyle, LineWidth, Printed}, ruff_python_trivia::{is_python_whitespace, PythonWhitespace}, @@ -1075,7 +1075,7 @@ impl<'src> CodeExampleRst<'src> { // [directives]: https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#directives // [Pygments lexer names]: https://pygments.org/docs/lexers/ // [code-block]: https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-code-block - static DIRECTIVE_START: Lazy<Regex> = Lazy::new(|| { + static DIRECTIVE_START: LazyLock<Regex> = LazyLock::new(|| { Regex::new( r"(?m)^\s*\.\. \s*(?i:code-block|sourcecode)::\s*(?i:python|py|python3|py3)$", ) @@ -1320,7 +1320,7 @@ impl<'src> CodeExampleMarkdown<'src> { /// /// [fenced code block]: https://spec.commonmark.org/0.30/#fenced-code-blocks fn new(original: InputDocstringLine<'src>) -> Option<CodeExampleMarkdown<'src>> { - static FENCE_START: Lazy<Regex> = Lazy::new(|| { + static FENCE_START: LazyLock<Regex> = LazyLock::new(|| { Regex::new( r"(?xm) ^ diff --git a/crates/ruff_python_formatter/tests/normalizer.rs b/crates/ruff_python_formatter/tests/normalizer.rs index b2cfc4dd6b08c..e5c84c5ab799e 100644 --- a/crates/ruff_python_formatter/tests/normalizer.rs +++ b/crates/ruff_python_formatter/tests/normalizer.rs @@ -1,6 +1,6 @@ +use std::sync::LazyLock; use { itertools::Either::{Left, Right}, - once_cell::sync::Lazy, regex::Regex, }; @@ -60,7 +60,7 @@ impl Transformer for Normalizer { } fn visit_string_literal(&self, string_literal: &mut ast::StringLiteral) { - static STRIP_DOC_TESTS: Lazy<Regex> = Lazy::new(|| { + static STRIP_DOC_TESTS: LazyLock<Regex> = LazyLock::new(|| { Regex::new( r"(?mx) ( @@ -75,14 +75,14 @@ impl Transformer for Normalizer { ) .unwrap() }); - static STRIP_RST_BLOCKS: Lazy<Regex> = Lazy::new(|| { + static STRIP_RST_BLOCKS: LazyLock<Regex> = LazyLock::new(|| { // This is kind of unfortunate, but it's pretty tricky (likely // impossible) to detect a reStructuredText block with a simple // regex. So we just look for the start of a block and remove // everything after it. Talk about a hammer. Regex::new(r"::(?s:.*)").unwrap() }); - static STRIP_MARKDOWN_BLOCKS: Lazy<Regex> = Lazy::new(|| { + static STRIP_MARKDOWN_BLOCKS: LazyLock<Regex> = LazyLock::new(|| { // This covers more than valid Markdown blocks, but that's OK. Regex::new(r"(```|~~~)\p{any}*(```|~~~|$)").unwrap() }); diff --git a/crates/ruff_source_file/Cargo.toml b/crates/ruff_source_file/Cargo.toml index b57c2eb638bc9..0f82f37169ee8 100644 --- a/crates/ruff_source_file/Cargo.toml +++ b/crates/ruff_source_file/Cargo.toml @@ -16,7 +16,6 @@ license = { workspace = true } ruff_text_size = { workspace = true } memchr = { workspace = true } -once_cell = { workspace = true } serde = { workspace = true, optional = true } [dev-dependencies] diff --git a/crates/ruff_source_file/src/lib.rs b/crates/ruff_source_file/src/lib.rs index 078c50cdc21e8..a0de92b1c14a0 100644 --- a/crates/ruff_source_file/src/lib.rs +++ b/crates/ruff_source_file/src/lib.rs @@ -1,6 +1,6 @@ use std::cmp::Ordering; use std::fmt::{Debug, Display, Formatter}; -use std::sync::Arc; +use std::sync::{Arc, OnceLock}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -134,9 +134,9 @@ impl SourceFileBuilder { /// Consumes `self` and returns the [`SourceFile`]. pub fn finish(self) -> SourceFile { let index = if let Some(index) = self.index { - once_cell::sync::OnceCell::with_value(index) + OnceLock::from(index) } else { - once_cell::sync::OnceCell::new() + OnceLock::new() }; SourceFile { @@ -218,7 +218,7 @@ impl Ord for SourceFile { struct SourceFileInner { name: Box<str>, code: Box<str>, - line_index: once_cell::sync::OnceCell<LineIndex>, + line_index: OnceLock<LineIndex>, } impl PartialEq for SourceFileInner { diff --git a/crates/ruff_source_file/src/locator.rs b/crates/ruff_source_file/src/locator.rs index 30bd59d830fe4..f133377a9e195 100644 --- a/crates/ruff_source_file/src/locator.rs +++ b/crates/ruff_source_file/src/locator.rs @@ -1,10 +1,9 @@ //! Struct used to efficiently slice source code at (row, column) Locations. -use std::ops::Add; - use memchr::{memchr2, memrchr2}; -use once_cell::unsync::OnceCell; use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; +use std::cell::OnceCell; +use std::ops::Add; use crate::newlines::find_newline; use crate::{LineIndex, OneIndexed, SourceCode, SourceLocation}; @@ -23,10 +22,10 @@ impl<'a> Locator<'a> { } } - pub const fn with_index(contents: &'a str, index: LineIndex) -> Self { + pub fn with_index(contents: &'a str, index: LineIndex) -> Self { Self { contents, - index: OnceCell::with_value(index), + index: OnceCell::from(index), } }