diff --git a/Cargo.lock b/Cargo.lock index cbc9053a69..1c3110e1be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1260,9 +1260,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.13.0" +version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1" +checksum = "074864da206b4973b84eb91683020dbefd6a8c3f0f38e054d93954e891935e4e" [[package]] name = "opaque-debug" @@ -1657,6 +1657,7 @@ dependencies = [ "glob", "log", "notify", + "once_cell", "rayon", "regex", "rustpython-parser", diff --git a/Cargo.toml b/Cargo.toml index 4b761c02fc..fce4a9c6a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ filetime = { version = "0.2.17" } glob = { version = "0.3.0"} log = { version = "0.4.17" } notify = { version = "4.0.17" } +once_cell = { version = "1.13.1" } rayon = { version = "1.5.3" } regex = { version = "1.6.0" } rustpython-parser = { features = ["lalrpop"], git = "https://github.com/charliermarsh/RustPython.git", rev = "1613f6c6990011a4bc559e79aaf28d715f9f729b" } diff --git a/src/checks.rs b/src/checks.rs index 0ef927f0fb..26290c8017 100644 --- a/src/checks.rs +++ b/src/checks.rs @@ -1,6 +1,7 @@ use std::str::FromStr; use anyhow::Result; +use once_cell::sync::Lazy; use regex::Regex; use rustpython_parser::ast::Location; use serde::{Deserialize, Serialize}; @@ -162,14 +163,17 @@ pub struct Check { pub location: Location, } +static NO_QA_REGEX: Lazy = Lazy::new(|| { + Regex::new(r"(?i)# noqa(?::\s?(?P([A-Z]+[0-9]+(?:[,\s]+)?)+))?").expect("Invalid regex") +}); +static SPLIT_COMMA_REGEX: Lazy = Lazy::new(|| Regex::new(r"[,\s]").expect("Invalid regex")); + impl Check { pub fn is_inline_ignored(&self, line: &str) -> bool { - let re = Regex::new(r"(?i)# noqa(?::\s?(?P([A-Z]+[0-9]+(?:[,\s]+)?)+))?").unwrap(); - match re.captures(line) { + match NO_QA_REGEX.captures(line) { Some(caps) => match caps.name("codes") { Some(codes) => { - let re = Regex::new(r"[,\s]").unwrap(); - for code in re + for code in SPLIT_COMMA_REGEX .split(codes.as_str()) .map(|code| code.trim()) .filter(|code| !code.is_empty())