1use std::ffi::OsStr;
21use std::path::Path;
22use std::sync::LazyLock;
23
24use regex::RegexSetBuilder;
25use rustc_hash::FxHashMap;
26
27use crate::walk::{filter_dirs, walk};
28
29#[cfg(test)]
30mod tests;
31
32const ERROR_CODE_COLS: usize = 80;
35const COLS: usize = 100;
36const GOML_COLS: usize = 120;
37
38const LINES: usize = 3000;
39
40const UNEXPLAINED_IGNORE_DOCTEST_INFO: &str = r#"unexplained "```ignore" doctest; try one:
41
42* make the test actually pass, by adding necessary imports and declarations, or
43* use "```text", if the code is not Rust code, or
44* use "```compile_fail,Ennnn", if the code is expected to fail at compile time, or
45* use "```should_panic", if the code is expected to fail at run time, or
46* use "```no_run", if the code should type-check but not necessary linkable/runnable, or
47* explain it like "```ignore (cannot-test-this-because-xxxx)", if the annotation cannot be avoided.
48
49"#;
50
51const LLVM_UNREACHABLE_INFO: &str = r"\
52C++ code used llvm_unreachable, which triggers undefined behavior
53when executed when assertions are disabled.
54Use llvm::report_fatal_error for increased robustness.";
55
56const DOUBLE_SPACE_AFTER_DOT: &str = r"\
57Use a single space after dots in comments.";
58
59const ANNOTATIONS_TO_IGNORE: &[&str] = &[
60 "// @!has",
61 "// @has",
62 "// @matches",
63 "// CHECK",
64 "// EMIT_MIR",
65 "// compile-flags",
66 "//@ compile-flags",
67 "// error-pattern",
68 "//@ error-pattern",
69 "// gdb",
70 "// lldb",
71 "// cdb",
72 "//@ normalize-stderr",
73];
74
75const CONFIGURABLE_CHECKS: [&str; 11] = [
77 "cr",
78 "undocumented-unsafe",
79 "tab",
80 "linelength",
81 "filelength",
82 "end-whitespace",
83 "trailing-newlines",
84 "leading-newlines",
85 "copyright",
86 "dbg",
87 "odd-backticks",
88];
89
90fn generate_problems<'a>(
91 consts: &'a [u32],
92 letter_digit: &'a FxHashMap<char, char>,
93) -> impl Iterator<Item = u32> + 'a {
94 consts.iter().flat_map(move |const_value| {
95 let problem =
96 letter_digit.iter().fold(format!("{:X}", const_value), |acc, (key, value)| {
97 acc.replace(&value.to_string(), &key.to_string())
98 });
99 let indexes: Vec<usize> = problem
100 .chars()
101 .enumerate()
102 .filter_map(|(index, c)| if letter_digit.contains_key(&c) { Some(index) } else { None })
103 .collect();
104 (0..1 << indexes.len()).map(move |i| {
105 u32::from_str_radix(
106 &problem
107 .chars()
108 .enumerate()
109 .map(|(index, c)| {
110 if let Some(pos) = indexes.iter().position(|&x| x == index) {
111 if (i >> pos) & 1 == 1 { letter_digit[&c] } else { c }
112 } else {
113 c
114 }
115 })
116 .collect::<String>(),
117 0x10,
118 )
119 .unwrap()
120 })
121 })
122}
123
124const ROOT_PROBLEMATIC_CONSTS: &[u32] = &[
126 184594741, 2880289470, 2881141438, 2965027518, 2976579765, 3203381950, 3405691582, 3405697037,
127 3735927486, 3735932941, 4027431614, 4276992702, 195934910, 252707358, 762133, 179681982,
128 173390526, 721077,
129];
130
131const LETTER_DIGIT: &[(char, char)] = &[('A', '4'), ('B', '8'), ('E', '3')];
132
133fn generate_problematic_strings(
135 consts: &[u32],
136 letter_digit: &FxHashMap<char, char>,
137) -> Vec<String> {
138 generate_problems(consts, letter_digit)
139 .flat_map(|v| vec![v.to_string(), format!("{:X}", v)])
140 .collect()
141}
142
143static PROBLEMATIC_CONSTS_STRINGS: LazyLock<Vec<String>> = LazyLock::new(|| {
144 generate_problematic_strings(ROOT_PROBLEMATIC_CONSTS, &LETTER_DIGIT.iter().cloned().collect())
145});
146
147fn contains_problematic_const(trimmed: &str) -> bool {
148 PROBLEMATIC_CONSTS_STRINGS.iter().any(|s| trimmed.to_uppercase().contains(s))
149}
150
151const INTERNAL_COMPILER_DOCS_LINE: &str = "#### This error code is internal to the compiler and will not be emitted with normal Rust code.";
152
153#[derive(Clone, Copy, PartialEq)]
155#[allow(non_camel_case_types)]
156enum LIUState {
157 EXP_COMMENT_START,
158 EXP_LINK_LABEL_OR_URL,
159 EXP_URL,
160 EXP_END,
161}
162
163fn line_is_url(is_error_code: bool, columns: usize, line: &str) -> bool {
170 if is_error_code {
172 return line.starts_with('[') && line.contains("]:") && line.contains("http");
173 }
174
175 use self::LIUState::*;
176 let mut state: LIUState = EXP_COMMENT_START;
177 let is_url = |w: &str| w.starts_with("http://") || w.starts_with("https://");
178
179 for tok in line.split_whitespace() {
180 match (state, tok) {
181 (EXP_COMMENT_START, "//") | (EXP_COMMENT_START, "///") | (EXP_COMMENT_START, "//!") => {
182 state = EXP_LINK_LABEL_OR_URL
183 }
184
185 (EXP_LINK_LABEL_OR_URL, w)
186 if w.len() >= 4 && w.starts_with('[') && w.ends_with("]:") =>
187 {
188 state = EXP_URL
189 }
190
191 (EXP_LINK_LABEL_OR_URL, w) if is_url(w) => state = EXP_END,
192
193 (EXP_URL, w) if is_url(w) || w.starts_with("../") => state = EXP_END,
194
195 (_, w) if w.len() > columns && is_url(w) => state = EXP_END,
196
197 (_, _) => {}
198 }
199 }
200
201 state == EXP_END
202}
203
204fn should_ignore(line: &str) -> bool {
207 static_regex!("\\s*//(\\[.*\\])?~.*").is_match(line)
211 || ANNOTATIONS_TO_IGNORE.iter().any(|a| line.contains(a))
212
213 || static_regex!("\\s*//@(\\[.*\\]) (compile-flags|normalize-stderr|error-pattern).*")
217 .is_match(line)
218 || static_regex!(
221 "\\s*//@ \\!?(count|files|has|has-dir|hasraw|matches|matchesraw|snapshot)\\s.*"
222 ).is_match(line)
223}
224
225fn long_line_is_ok(extension: &str, is_error_code: bool, max_columns: usize, line: &str) -> bool {
227 match extension {
228 "ftl" => true,
230 "md" if !is_error_code => true,
232 "md" if line == INTERNAL_COMPILER_DOCS_LINE => true,
234 _ => line_is_url(is_error_code, max_columns, line) || should_ignore(line),
235 }
236}
237
238#[derive(Clone, Copy)]
239enum Directive {
240 Deny,
242
243 Ignore(bool),
248}
249
250fn contains_ignore_directives<const N: usize>(
253 can_contain: bool,
254 contents: &str,
255 checks: [&str; N],
256) -> [Directive; N] {
257 if !can_contain {
258 return [Directive::Deny; N];
259 }
260 checks.map(|check| {
261 if contents.contains(&format!("// ignore-tidy-{check}"))
263 || contents.contains(&format!("# ignore-tidy-{check}"))
264 || contents.contains(&format!("/* ignore-tidy-{check} */"))
265 || contents.contains(&format!("<!-- ignore-tidy-{check} -->"))
266 {
267 Directive::Ignore(false)
268 } else {
269 Directive::Deny
270 }
271 })
272}
273
274macro_rules! suppressible_tidy_err {
275 ($err:ident, $skip:ident, $msg:literal) => {
276 if let Directive::Deny = $skip {
277 $err(&format!($msg));
278 } else {
279 $skip = Directive::Ignore(true);
280 }
281 };
282}
283
284pub fn is_in(full_path: &Path, parent_folder_to_find: &str, folder_to_find: &str) -> bool {
285 if let Some(parent) = full_path.parent() {
286 if parent.file_name().map_or_else(
287 || false,
288 |f| {
289 f == folder_to_find
290 && parent
291 .parent()
292 .and_then(|f| f.file_name())
293 .map_or_else(|| false, |f| f == parent_folder_to_find)
294 },
295 ) {
296 true
297 } else {
298 is_in(parent, parent_folder_to_find, folder_to_find)
299 }
300 } else {
301 false
302 }
303}
304
305fn skip_markdown_path(path: &Path) -> bool {
306 const SKIP_MD: &[&str] = &[
308 "src/doc/edition-guide",
309 "src/doc/embedded-book",
310 "src/doc/nomicon",
311 "src/doc/reference",
312 "src/doc/rust-by-example",
313 "src/doc/rustc-dev-guide",
314 ];
315 SKIP_MD.iter().any(|p| path.ends_with(p))
316}
317
318fn is_unexplained_ignore(extension: &str, line: &str) -> bool {
319 if !line.ends_with("```ignore") && !line.ends_with("```rust,ignore") {
320 return false;
321 }
322 if extension == "md" && line.trim().starts_with("//") {
323 return false;
326 }
327 true
328}
329
330pub fn check(path: &Path, bad: &mut bool) {
331 fn skip(path: &Path, is_dir: bool) -> bool {
332 if path.file_name().map_or(false, |name| name.to_string_lossy().starts_with(".#")) {
333 return true;
335 }
336
337 if filter_dirs(path) || skip_markdown_path(path) {
338 return true;
339 }
340
341 if is_dir {
343 return false;
344 }
345
346 let extensions = ["rs", "py", "js", "sh", "c", "cpp", "h", "md", "css", "ftl", "goml"];
347
348 if path.extension().map_or(true, |ext| !extensions.iter().any(|e| ext == OsStr::new(e))) {
350 return true;
351 }
352
353 path.extension().map_or(false, |e| e == "css") && !is_in(path, "src", "librustdoc")
355 }
356
357 let problematic_regex = RegexSetBuilder::new(PROBLEMATIC_CONSTS_STRINGS.as_slice())
360 .case_insensitive(true)
361 .build()
362 .unwrap();
363
364 let this_file = Path::new(file!());
367
368 walk(path, skip, &mut |entry, contents| {
369 let file = entry.path();
370 let filename = file.file_name().unwrap().to_string_lossy();
371
372 let is_css_file = filename.ends_with(".css");
373 let under_rustfmt = filename.ends_with(".rs") &&
374 !file.ancestors().any(|a| {
377 (a.ends_with("tests") && a.join("COMPILER_TESTS.md").exists()) ||
378 a.ends_with("src/doc/book")
379 });
380
381 if contents.is_empty() {
382 tidy_error!(bad, "{}: empty file", file.display());
383 }
384
385 let extension = file.extension().unwrap().to_string_lossy();
386 let is_error_code = extension == "md" && is_in(file, "src", "error_codes");
387 let is_goml_code = extension == "goml";
388
389 let max_columns = if is_error_code {
390 ERROR_CODE_COLS
391 } else if is_goml_code {
392 GOML_COLS
393 } else {
394 COLS
395 };
396
397 let can_contain = contents.contains("// ignore-tidy-")
399 || contents.contains("# ignore-tidy-")
400 || contents.contains("/* ignore-tidy-")
401 || contents.contains("<!-- ignore-tidy-");
402 if filename.contains("ignore-tidy") {
405 return;
406 }
407 if let Some(p) = file.parent() {
409 if p.ends_with(Path::new("src/etc/completions")) {
410 return;
411 }
412 }
413 let [
414 mut skip_cr,
415 mut skip_undocumented_unsafe,
416 mut skip_tab,
417 mut skip_line_length,
418 mut skip_file_length,
419 mut skip_end_whitespace,
420 mut skip_trailing_newlines,
421 mut skip_leading_newlines,
422 mut skip_copyright,
423 mut skip_dbg,
424 mut skip_odd_backticks,
425 ] = contains_ignore_directives(can_contain, &contents, CONFIGURABLE_CHECKS);
426 let mut leading_new_lines = false;
427 let mut trailing_new_lines = 0;
428 let mut lines = 0;
429 let mut last_safety_comment = false;
430 let mut comment_block: Option<(usize, usize)> = None;
431 let is_test = file.components().any(|c| c.as_os_str() == "tests")
432 || file.file_stem().unwrap() == "tests";
433 let is_this_file = file.ends_with(this_file) || this_file.ends_with(file);
434 let is_test_for_this_file =
435 is_test && file.parent().unwrap().ends_with(this_file.with_extension(""));
436 let any_problematic_line =
439 !is_this_file && !is_test_for_this_file && problematic_regex.is_match(contents);
440 for (i, line) in contents.split('\n').enumerate() {
441 if line.is_empty() {
442 if i == 0 {
443 leading_new_lines = true;
444 }
445 trailing_new_lines += 1;
446 continue;
447 } else {
448 trailing_new_lines = 0;
449 }
450
451 let trimmed = line.trim();
452
453 if !trimmed.starts_with("//") {
454 lines += 1;
455 }
456
457 let mut err = |msg: &str| {
458 tidy_error!(bad, "{}:{}: {}", file.display(), i + 1, msg);
459 };
460
461 if trimmed.contains("dbg!")
462 && !trimmed.starts_with("//")
463 && !file.ancestors().any(|a| {
464 (a.ends_with("tests") && a.join("COMPILER_TESTS.md").exists())
465 || a.ends_with("library/alloc/tests")
466 })
467 && filename != "tests.rs"
468 {
469 suppressible_tidy_err!(
470 err,
471 skip_dbg,
472 "`dbg!` macro is intended as a debugging tool. It should not be in version control."
473 )
474 }
475
476 if !under_rustfmt
477 && line.chars().count() > max_columns
478 && !long_line_is_ok(&extension, is_error_code, max_columns, line)
479 {
480 suppressible_tidy_err!(
481 err,
482 skip_line_length,
483 "line longer than {max_columns} chars"
484 );
485 }
486 if !is_css_file && line.contains('\t') {
487 suppressible_tidy_err!(err, skip_tab, "tab character");
488 }
489 if line.ends_with(' ') || line.ends_with('\t') {
490 suppressible_tidy_err!(err, skip_end_whitespace, "trailing whitespace");
491 }
492 if is_css_file && line.starts_with(' ') {
493 err("CSS files use tabs for indent");
494 }
495 if line.contains('\r') {
496 suppressible_tidy_err!(err, skip_cr, "CR character");
497 }
498 if !is_this_file {
499 let directive_line_starts = ["// ", "# ", "/* ", "<!-- "];
500 let possible_line_start =
501 directive_line_starts.into_iter().any(|s| line.starts_with(s));
502 let contains_potential_directive =
503 possible_line_start && (line.contains("-tidy") || line.contains("tidy-"));
504 let has_recognized_ignore_directive =
505 contains_ignore_directives(can_contain, line, CONFIGURABLE_CHECKS)
506 .into_iter()
507 .any(|directive| matches!(directive, Directive::Ignore(_)));
508 let has_alphabetical_directive = line.contains("tidy-alphabetical-start")
509 || line.contains("tidy-alphabetical-end");
510 let has_recognized_directive =
511 has_recognized_ignore_directive || has_alphabetical_directive;
512 if contains_potential_directive && (!has_recognized_directive) {
513 err("Unrecognized tidy directive")
514 }
515 if trimmed.contains("TODO") && !trimmed.contains("ignore-tidy-todo") {
518 err(
519 "TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME",
520 )
521 }
522 if trimmed.contains("//") && trimmed.contains(" XXX") {
523 err("Instead of XXX use FIXME")
524 }
525 if any_problematic_line && contains_problematic_const(trimmed) {
526 err("Don't use magic numbers that spell things (consider 0x12345678)");
527 }
528 }
529 if trimmed.contains("unsafe {")
531 && !trimmed.starts_with("//")
532 && !last_safety_comment
533 && file.components().any(|c| c.as_os_str() == "core")
534 && !is_test
535 {
536 suppressible_tidy_err!(err, skip_undocumented_unsafe, "undocumented unsafe");
537 }
538 if trimmed.contains("// SAFETY:") {
539 last_safety_comment = true;
540 } else if trimmed.starts_with("//") || trimmed.is_empty() {
541 } else {
543 last_safety_comment = false;
544 }
545 if (line.starts_with("// Copyright")
546 || line.starts_with("# Copyright")
547 || line.starts_with("Copyright"))
548 && (trimmed.contains("Rust Developers")
549 || trimmed.contains("Rust Project Developers"))
550 {
551 suppressible_tidy_err!(
552 err,
553 skip_copyright,
554 "copyright notices attributed to the Rust Project Developers are deprecated"
555 );
556 }
557 if !file.components().any(|c| c.as_os_str() == "rustc_baked_icu_data")
558 && is_unexplained_ignore(&extension, line)
559 {
560 err(UNEXPLAINED_IGNORE_DOCTEST_INFO);
561 }
562
563 if filename.ends_with(".cpp") && line.contains("llvm_unreachable") {
564 err(LLVM_UNREACHABLE_INFO);
565 }
566
567 let is_compiler = || file.components().any(|c| c.as_os_str() == "compiler");
569
570 if is_compiler() {
571 if line.contains("//")
572 && line
573 .chars()
574 .collect::<Vec<_>>()
575 .windows(4)
576 .any(|cs| matches!(cs, ['.', ' ', ' ', last] if last.is_alphabetic()))
577 {
578 err(DOUBLE_SPACE_AFTER_DOT)
579 }
580
581 if filename.ends_with(".ftl") {
582 let line_backticks = trimmed.chars().filter(|ch| *ch == '`').count();
583 if line_backticks % 2 == 1 {
584 suppressible_tidy_err!(err, skip_odd_backticks, "odd number of backticks");
585 }
586 } else if trimmed.contains("//") {
587 let (start_line, mut backtick_count) = comment_block.unwrap_or((i + 1, 0));
588 let line_backticks = trimmed.chars().filter(|ch| *ch == '`').count();
589 let comment_text = trimmed.split("//").nth(1).unwrap();
590 if line_backticks % 2 == 1 {
592 backtick_count += comment_text.chars().filter(|ch| *ch == '`').count();
593 }
594 comment_block = Some((start_line, backtick_count));
595 } else if let Some((start_line, backtick_count)) = comment_block.take() {
596 if backtick_count % 2 == 1 {
597 let mut err = |msg: &str| {
598 tidy_error!(bad, "{}:{start_line}: {msg}", file.display());
599 };
600 let block_len = (i + 1) - start_line;
601 if block_len == 1 {
602 suppressible_tidy_err!(
603 err,
604 skip_odd_backticks,
605 "comment with odd number of backticks"
606 );
607 } else {
608 suppressible_tidy_err!(
609 err,
610 skip_odd_backticks,
611 "{block_len}-line comment block with odd number of backticks"
612 );
613 }
614 }
615 }
616 }
617 }
618 if leading_new_lines {
619 let mut err = |_| {
620 tidy_error!(bad, "{}: leading newline", file.display());
621 };
622 suppressible_tidy_err!(err, skip_leading_newlines, "missing leading newline");
623 }
624 let mut err = |msg: &str| {
625 tidy_error!(bad, "{}: {}", file.display(), msg);
626 };
627 match trailing_new_lines {
628 0 => suppressible_tidy_err!(err, skip_trailing_newlines, "missing trailing newline"),
629 1 => {}
630 n => suppressible_tidy_err!(
631 err,
632 skip_trailing_newlines,
633 "too many trailing newlines ({n})"
634 ),
635 };
636 if lines > LINES {
637 let mut err = |_| {
638 tidy_error!(
639 bad,
640 "{}: too many lines ({}) (add `// \
641 ignore-tidy-filelength` to the file to suppress this error)",
642 file.display(),
643 lines
644 );
645 };
646 suppressible_tidy_err!(err, skip_file_length, "");
647 }
648
649 if let Directive::Ignore(false) = skip_cr {
650 tidy_error!(bad, "{}: ignoring CR characters unnecessarily", file.display());
651 }
652 if let Directive::Ignore(false) = skip_tab {
653 tidy_error!(bad, "{}: ignoring tab characters unnecessarily", file.display());
654 }
655 if let Directive::Ignore(false) = skip_end_whitespace {
656 tidy_error!(bad, "{}: ignoring trailing whitespace unnecessarily", file.display());
657 }
658 if let Directive::Ignore(false) = skip_trailing_newlines {
659 tidy_error!(bad, "{}: ignoring trailing newlines unnecessarily", file.display());
660 }
661 if let Directive::Ignore(false) = skip_leading_newlines {
662 tidy_error!(bad, "{}: ignoring leading newlines unnecessarily", file.display());
663 }
664 if let Directive::Ignore(false) = skip_copyright {
665 tidy_error!(bad, "{}: ignoring copyright unnecessarily", file.display());
666 }
667 let _unused = skip_line_length;
670 let _unused = skip_file_length;
671 })
672}