|
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,15 @@ 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 |
| - |
118 | 113 | fn generate_problematic_strings(
|
119 | 114 | consts: &[u32],
|
120 | 115 | letter_digit: &FxHashMap<char, char>,
|
121 | 116 | ) -> Vec<String> {
|
122 | 117 | generate_problems(consts, letter_digit)
|
123 |
| - .flat_map(|v| vec![v.to_string(), format!("{:X}", v)]) |
| 118 | + .flat_map(|v| vec![v.to_string(), format!("{:x}", v), format!("{:X}", v)]) |
124 | 119 | .collect()
|
125 | 120 | }
|
126 | 121 |
|
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 | 122 | const INTERNAL_COMPILER_DOCS_LINE: &str = "#### This error code is internal to the compiler and will not be emitted with normal Rust code.";
|
139 | 123 |
|
140 | 124 | /// Parser states for `line_is_url`.
|
@@ -331,6 +315,11 @@ pub fn check(path: &Path, bad: &mut bool) {
|
331 | 315 | // We only check CSS files in rustdoc.
|
332 | 316 | path.extension().map_or(false, |e| e == "css") && !is_in(path, "src", "librustdoc")
|
333 | 317 | }
|
| 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(); |
334 | 323 |
|
335 | 324 | walk(path, skip, &mut |entry, contents| {
|
336 | 325 | let file = entry.path();
|
@@ -400,6 +389,7 @@ pub fn check(path: &Path, bad: &mut bool) {
|
400 | 389 | let is_test = file.components().any(|c| c.as_os_str() == "tests");
|
401 | 390 | // scanning the whole file for multiple needles at once is more efficient than
|
402 | 391 | // executing lines times needles separate searches.
|
| 392 | + let any_problematic_line = problematic_regex.is_match(contents); |
403 | 393 | for (i, line) in contents.split('\n').enumerate() {
|
404 | 394 | if line.is_empty() {
|
405 | 395 | if i == 0 {
|
@@ -469,8 +459,12 @@ pub fn check(path: &Path, bad: &mut bool) {
|
469 | 459 | if trimmed.contains("//") && trimmed.contains(" XXX") {
|
470 | 460 | err("Instead of XXX use FIXME")
|
471 | 461 | }
|
472 |
| - if contains_problematic_const(trimmed) { |
473 |
| - err("Don't use magic numbers that spell things (consider 0x12345678)"); |
| 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 | + } |
474 | 468 | }
|
475 | 469 | }
|
476 | 470 | // for now we just check libcore
|
|
0 commit comments