Skip to content

Commit fb0ef67

Browse files
authored
don't use regexes
1 parent 28cc0b6 commit fb0ef67

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/tools/tidy/src/style.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
// ignore-tidy-dbg
1919

2020
use crate::walk::{filter_dirs, walk};
21-
use regex::RegexSet;
2221
use rustc_hash::FxHashMap;
2322
use std::{ffi::OsStr, path::Path};
2423

@@ -110,15 +109,31 @@ const ROOT_PROBLEMATIC_CONSTS: &[u32] = &[
110109
173390526, 721077,
111110
];
112111

112+
#[cfg(not(test))]
113+
const LETTER_DIGIT: &[(char, char)] = &[('A', '4'), ('B', '8'), ('E', '3')];
114+
115+
#[cfg(test)]
116+
const LETTER_DIGIT: &[(char, char)] = &[('A', '4'), ('B', '8'), ('E', '3'), ('0', 'F')]; // use "futile" F intentionally
117+
113118
fn generate_problematic_strings(
114119
consts: &[u32],
115120
letter_digit: &FxHashMap<char, char>,
116121
) -> Vec<String> {
117122
generate_problems(consts, letter_digit)
118-
.flat_map(|v| vec![v.to_string(), format!("{:x}", v), format!("{:X}", v)])
123+
.flat_map(|v| vec![v.to_string(), format!("{:X}", v)])
119124
.collect()
120125
}
121126

127+
fn contains_problematic_const(trimmed: &str) -> bool {
128+
lazy_static::lazy_static! {
129+
static ref PROBLEMATIC_CONSTS_STRINGS: Vec<String> = generate_problematic_strings(
130+
ROOT_PROBLEMATIC_CONSTS,
131+
&FxHashMap::from_iter(LETTER_DIGIT.iter().copied()),
132+
);
133+
}
134+
PROBLEMATIC_CONSTS_STRINGS.iter().any(|s| trimmed.to_uppercase().contains(s))
135+
}
136+
122137
const INTERNAL_COMPILER_DOCS_LINE: &str = "#### This error code is internal to the compiler and will not be emitted with normal Rust code.";
123138

124139
/// Parser states for `line_is_url`.
@@ -315,11 +330,6 @@ pub fn check(path: &Path, bad: &mut bool) {
315330
// We only check CSS files in rustdoc.
316331
path.extension().map_or(false, |e| e == "css") && !is_in(path, "src", "librustdoc")
317332
}
318-
let problematic_consts_strings = generate_problematic_strings(
319-
ROOT_PROBLEMATIC_CONSTS,
320-
&[('A', '4'), ('B', '8'), ('E', '3')].iter().cloned().collect(),
321-
);
322-
let problematic_regex = RegexSet::new(problematic_consts_strings.as_slice()).unwrap();
323333

324334
walk(path, skip, &mut |entry, contents| {
325335
let file = entry.path();
@@ -389,7 +399,6 @@ pub fn check(path: &Path, bad: &mut bool) {
389399
let is_test = file.components().any(|c| c.as_os_str() == "tests");
390400
// scanning the whole file for multiple needles at once is more efficient than
391401
// executing lines times needles separate searches.
392-
let any_problematic_line = problematic_regex.is_match(contents);
393402
for (i, line) in contents.split('\n').enumerate() {
394403
if line.is_empty() {
395404
if i == 0 {
@@ -459,12 +468,8 @@ pub fn check(path: &Path, bad: &mut bool) {
459468
if trimmed.contains("//") && trimmed.contains(" XXX") {
460469
err("Instead of XXX use FIXME")
461470
}
462-
if any_problematic_line {
463-
for s in problematic_consts_strings.iter() {
464-
if trimmed.contains(s) {
465-
err("Don't use magic numbers that spell things (consider 0x12345678)");
466-
}
467-
}
471+
if contains_problematic_const(trimmed) {
472+
err("Don't use magic numbers that spell things (consider 0x12345678)");
468473
}
469474
}
470475
// for now we just check libcore

0 commit comments

Comments
 (0)