|
18 | 18 | // ignore-tidy-dbg
|
19 | 19 |
|
20 | 20 | use crate::walk::{filter_dirs, walk};
|
| 21 | +use regex::RegexSet; |
21 | 22 | use rustc_hash::FxHashMap;
|
22 |
| -use std::{ffi::OsStr, path::Path, sync::LazyLock}; |
| 23 | +use std::{ffi::OsStr, path::Path}; |
23 | 24 |
|
24 | 25 | #[cfg(test)]
|
25 | 26 | mod tests;
|
@@ -109,32 +110,16 @@ const ROOT_PROBLEMATIC_CONSTS: &[u32] = &[
|
109 | 110 | 173390526, 721077,
|
110 | 111 | ];
|
111 | 112 |
|
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 |
| - |
| 113 | +// Returns all permutations of problematic consts, over 2000 elements. |
118 | 114 | fn generate_problematic_strings(
|
119 | 115 | consts: &[u32],
|
120 | 116 | letter_digit: &FxHashMap<char, char>,
|
121 | 117 | ) -> Vec<String> {
|
122 | 118 | generate_problems(consts, letter_digit)
|
123 |
| - .flat_map(|v| vec![v.to_string(), format!("{:X}", v)]) |
| 119 | + .flat_map(|v| vec![v.to_string(), format!("{:x}", v), format!("{:X}", v)]) |
124 | 120 | .collect()
|
125 | 121 | }
|
126 | 122 |
|
127 |
| -static PROBLEMATIC_CONSTS_STRINGS: LazyLock<Vec<String>> = LazyLock::new(|| { |
128 |
| - generate_problematic_strings( |
129 |
| - ROOT_PROBLEMATIC_CONSTS, |
130 |
| - &FxHashMap::from_iter(LETTER_DIGIT.iter().copied()), |
131 |
| - ) |
132 |
| -}); |
133 |
| - |
134 |
| -fn contains_problematic_const(trimmed: &str) -> bool { |
135 |
| - PROBLEMATIC_CONSTS_STRINGS.iter().any(|s| trimmed.to_uppercase().contains(s)) |
136 |
| -} |
137 |
| - |
138 | 123 | const INTERNAL_COMPILER_DOCS_LINE: &str = "#### This error code is internal to the compiler and will not be emitted with normal Rust code.";
|
139 | 124 |
|
140 | 125 | /// Parser states for `line_is_url`.
|
@@ -331,6 +316,13 @@ pub fn check(path: &Path, bad: &mut bool) {
|
331 | 316 | // We only check CSS files in rustdoc.
|
332 | 317 | path.extension().map_or(false, |e| e == "css") && !is_in(path, "src", "librustdoc")
|
333 | 318 | }
|
| 319 | + let problematic_consts_strings = generate_problematic_strings( |
| 320 | + ROOT_PROBLEMATIC_CONSTS, |
| 321 | + &[('A', '4'), ('B', '8'), ('E', '3')].iter().cloned().collect(), |
| 322 | + ); |
| 323 | + // This creates a RegexSet as regex contains performance optimizations to be able to deal with these over |
| 324 | + // 2000 needles efficiently. This runs over the entire source code, so performance matters. |
| 325 | + let problematic_regex = RegexSet::new(problematic_consts_strings.as_slice()).unwrap(); |
334 | 326 |
|
335 | 327 | walk(path, skip, &mut |entry, contents| {
|
336 | 328 | let file = entry.path();
|
@@ -400,6 +392,7 @@ pub fn check(path: &Path, bad: &mut bool) {
|
400 | 392 | let is_test = file.components().any(|c| c.as_os_str() == "tests");
|
401 | 393 | // scanning the whole file for multiple needles at once is more efficient than
|
402 | 394 | // executing lines times needles separate searches.
|
| 395 | + let any_problematic_line = problematic_regex.is_match(contents); |
403 | 396 | for (i, line) in contents.split('\n').enumerate() {
|
404 | 397 | if line.is_empty() {
|
405 | 398 | if i == 0 {
|
@@ -469,8 +462,12 @@ pub fn check(path: &Path, bad: &mut bool) {
|
469 | 462 | if trimmed.contains("//") && trimmed.contains(" XXX") {
|
470 | 463 | err("Instead of XXX use FIXME")
|
471 | 464 | }
|
472 |
| - if contains_problematic_const(trimmed) { |
473 |
| - err("Don't use magic numbers that spell things (consider 0x12345678)"); |
| 465 | + if any_problematic_line { |
| 466 | + for s in problematic_consts_strings.iter() { |
| 467 | + if trimmed.contains(s) { |
| 468 | + err("Don't use magic numbers that spell things (consider 0x12345678)"); |
| 469 | + } |
| 470 | + } |
474 | 471 | }
|
475 | 472 | }
|
476 | 473 | // for now we just check libcore
|
|
0 commit comments