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 LINELENGTH_CHECK: &str = "linelength";
76
77const CONFIGURABLE_CHECKS: [&str; 11] = [
79 "cr",
80 "undocumented-unsafe",
81 "tab",
82 LINELENGTH_CHECK,
83 "filelength",
84 "end-whitespace",
85 "trailing-newlines",
86 "leading-newlines",
87 "copyright",
88 "dbg",
89 "odd-backticks",
90];
91
92fn generate_problems<'a>(
93 consts: &'a [u32],
94 letter_digit: &'a FxHashMap<char, char>,
95) -> impl Iterator<Item = u32> + 'a {
96 consts.iter().flat_map(move |const_value| {
97 let problem = letter_digit.iter().fold(format!("{const_value:X}"), |acc, (key, value)| {
98 acc.replace(&value.to_string(), &key.to_string())
99 });
100 let indexes: Vec<usize> = problem
101 .chars()
102 .enumerate()
103 .filter_map(|(index, c)| if letter_digit.contains_key(&c) { Some(index) } else { None })
104 .collect();
105 (0..1 << indexes.len()).map(move |i| {
106 u32::from_str_radix(
107 &problem
108 .chars()
109 .enumerate()
110 .map(|(index, c)| {
111 if let Some(pos) = indexes.iter().position(|&x| x == index) {
112 if (i >> pos) & 1 == 1 { letter_digit[&c] } else { c }
113 } else {
114 c
115 }
116 })
117 .collect::<String>(),
118 0x10,
119 )
120 .unwrap()
121 })
122 })
123}
124
125const ROOT_PROBLEMATIC_CONSTS: &[u32] = &[
127 184594741, 2880289470, 2881141438, 2965027518, 2976579765, 3203381950, 3405691582, 3405697037,
128 3735927486, 3735932941, 4027431614, 4276992702, 195934910, 252707358, 762133, 179681982,
129 173390526, 721077,
130];
131
132const LETTER_DIGIT: &[(char, char)] = &[('A', '4'), ('B', '8'), ('E', '3')];
133
134fn generate_problematic_strings(
136 consts: &[u32],
137 letter_digit: &FxHashMap<char, char>,
138) -> Vec<String> {
139 generate_problems(consts, letter_digit)
140 .flat_map(|v| vec![v.to_string(), format!("{:X}", v)])
141 .collect()
142}
143
144static PROBLEMATIC_CONSTS_STRINGS: LazyLock<Vec<String>> = LazyLock::new(|| {
145 generate_problematic_strings(ROOT_PROBLEMATIC_CONSTS, &LETTER_DIGIT.iter().cloned().collect())
146});
147
148fn contains_problematic_const(trimmed: &str) -> bool {
149 PROBLEMATIC_CONSTS_STRINGS.iter().any(|s| trimmed.to_uppercase().contains(s))
150}
151
152const INTERNAL_COMPILER_DOCS_LINE: &str = "#### This error code is internal to the compiler and will not be emitted with normal Rust code.";
153
154#[derive(Clone, Copy, PartialEq)]
156#[allow(non_camel_case_types)]
157enum LIUState {
158 EXP_COMMENT_START,
159 EXP_LINK_LABEL_OR_URL,
160 EXP_URL,
161 EXP_END,
162}
163
164fn line_is_url(is_error_code: bool, columns: usize, line: &str) -> bool {
171 if is_error_code {
173 return line.starts_with('[') && line.contains("]:") && line.contains("http");
174 }
175
176 use self::LIUState::*;
177 let mut state: LIUState = EXP_COMMENT_START;
178 let is_url = |w: &str| w.starts_with("http://") || w.starts_with("https://");
179
180 for tok in line.split_whitespace() {
181 match (state, tok) {
182 (EXP_COMMENT_START, "//") | (EXP_COMMENT_START, "///") | (EXP_COMMENT_START, "//!") => {
183 state = EXP_LINK_LABEL_OR_URL
184 }
185
186 (EXP_LINK_LABEL_OR_URL, w)
187 if w.len() >= 4 && w.starts_with('[') && w.ends_with("]:") =>
188 {
189 state = EXP_URL
190 }
191
192 (EXP_LINK_LABEL_OR_URL, w) if is_url(w) => state = EXP_END,
193
194 (EXP_URL, w) if is_url(w) || w.starts_with("../") => state = EXP_END,
195
196 (_, w) if w.len() > columns && is_url(w) => state = EXP_END,
197
198 (_, _) => {}
199 }
200 }
201
202 state == EXP_END
203}
204
205fn should_ignore(line: &str) -> bool {
208 static_regex!("\\s*//(\\[.*\\])?~.*").is_match(line)
212 || ANNOTATIONS_TO_IGNORE.iter().any(|a| line.contains(a))
213
214 || static_regex!("\\s*//@(\\[.*\\]) (compile-flags|normalize-stderr|error-pattern).*")
218 .is_match(line)
219 || static_regex!(
222 "\\s*//@ \\!?(count|files|has|has-dir|hasraw|matches|matchesraw|snapshot)\\s.*"
223 ).is_match(line)
224}
225
226fn long_line_is_ok(extension: &str, is_error_code: bool, max_columns: usize, line: &str) -> bool {
228 match extension {
229 "ftl" => true,
231 "md" if !is_error_code => true,
233 "md" if line == INTERNAL_COMPILER_DOCS_LINE => true,
235 _ => line_is_url(is_error_code, max_columns, line) || should_ignore(line),
236 }
237}
238
239#[derive(Clone, Copy)]
240enum Directive {
241 Deny,
243
244 Ignore(bool),
249}
250
251fn contains_ignore_directives<const N: usize>(
254 path_str: &str,
255 can_contain: bool,
256 contents: &str,
257 checks: [&str; N],
258) -> [Directive; N] {
259 let always_ignore_linelength = path_str.contains("rustdoc-json");
262
263 if !can_contain && !always_ignore_linelength {
264 return [Directive::Deny; N];
265 }
266
267 checks.map(|check| {
268 if check == LINELENGTH_CHECK && always_ignore_linelength {
269 return Directive::Ignore(false);
270 }
271
272 if contents.contains(&format!("// ignore-tidy-{check}"))
274 || contents.contains(&format!("# ignore-tidy-{check}"))
275 || contents.contains(&format!("/* ignore-tidy-{check} */"))
276 || contents.contains(&format!("<!-- ignore-tidy-{check} -->"))
277 {
278 Directive::Ignore(false)
279 } else {
280 Directive::Deny
281 }
282 })
283}
284
285macro_rules! suppressible_tidy_err {
286 ($err:ident, $skip:ident, $msg:literal) => {
287 if let Directive::Deny = $skip {
288 $err(&format!($msg));
289 } else {
290 $skip = Directive::Ignore(true);
291 }
292 };
293}
294
295pub fn is_in(full_path: &Path, parent_folder_to_find: &str, folder_to_find: &str) -> bool {
296 if let Some(parent) = full_path.parent() {
297 if parent.file_name().map_or_else(
298 || false,
299 |f| {
300 f == folder_to_find
301 && parent
302 .parent()
303 .and_then(|f| f.file_name())
304 .map_or_else(|| false, |f| f == parent_folder_to_find)
305 },
306 ) {
307 true
308 } else {
309 is_in(parent, parent_folder_to_find, folder_to_find)
310 }
311 } else {
312 false
313 }
314}
315
316fn skip_markdown_path(path: &Path) -> bool {
317 const SKIP_MD: &[&str] = &[
319 "src/doc/edition-guide",
320 "src/doc/embedded-book",
321 "src/doc/nomicon",
322 "src/doc/reference",
323 "src/doc/rust-by-example",
324 "src/doc/rustc-dev-guide",
325 ];
326 SKIP_MD.iter().any(|p| path.ends_with(p))
327}
328
329fn is_unexplained_ignore(extension: &str, line: &str) -> bool {
330 if !line.ends_with("```ignore") && !line.ends_with("```rust,ignore") {
331 return false;
332 }
333 if extension == "md" && line.trim().starts_with("//") {
334 return false;
337 }
338 true
339}
340
341pub fn check(path: &Path, bad: &mut bool) {
342 fn skip(path: &Path, is_dir: bool) -> bool {
343 if path.file_name().is_some_and(|name| name.to_string_lossy().starts_with(".#")) {
344 return true;
346 }
347
348 if filter_dirs(path) || skip_markdown_path(path) {
349 return true;
350 }
351
352 if is_dir {
354 return false;
355 }
356
357 let extensions = ["rs", "py", "js", "sh", "c", "cpp", "h", "md", "css", "ftl", "goml"];
358
359 if path.extension().is_none_or(|ext| !extensions.iter().any(|e| ext == OsStr::new(e))) {
361 return true;
362 }
363
364 path.extension().is_some_and(|e| e == "css") && !is_in(path, "src", "librustdoc")
366 }
367
368 let problematic_regex = RegexSetBuilder::new(PROBLEMATIC_CONSTS_STRINGS.as_slice())
371 .case_insensitive(true)
372 .build()
373 .unwrap();
374
375 let this_file = Path::new(file!());
378
379 walk(path, skip, &mut |entry, contents| {
380 let file = entry.path();
381 let path_str = file.to_string_lossy();
382 let filename = file.file_name().unwrap().to_string_lossy();
383
384 let is_css_file = filename.ends_with(".css");
385 let under_rustfmt = filename.ends_with(".rs") &&
386 !file.ancestors().any(|a| {
389 (a.ends_with("tests") && a.join("COMPILER_TESTS.md").exists()) ||
390 a.ends_with("src/doc/book")
391 });
392
393 if contents.is_empty() {
394 tidy_error!(bad, "{}: empty file", file.display());
395 }
396
397 let extension = file.extension().unwrap().to_string_lossy();
398 let is_error_code = extension == "md" && is_in(file, "src", "error_codes");
399 let is_goml_code = extension == "goml";
400
401 let max_columns = if is_error_code {
402 ERROR_CODE_COLS
403 } else if is_goml_code {
404 GOML_COLS
405 } else {
406 COLS
407 };
408
409 let can_contain = contents.contains("// ignore-tidy-")
411 || contents.contains("# ignore-tidy-")
412 || contents.contains("/* ignore-tidy-")
413 || contents.contains("<!-- ignore-tidy-");
414 if filename.contains("ignore-tidy") {
417 return;
418 }
419 if let Some(p) = file.parent()
421 && p.ends_with(Path::new("src/etc/completions"))
422 {
423 return;
424 }
425 let [
426 mut skip_cr,
427 mut skip_undocumented_unsafe,
428 mut skip_tab,
429 mut skip_line_length,
430 mut skip_file_length,
431 mut skip_end_whitespace,
432 mut skip_trailing_newlines,
433 mut skip_leading_newlines,
434 mut skip_copyright,
435 mut skip_dbg,
436 mut skip_odd_backticks,
437 ] = contains_ignore_directives(&path_str, can_contain, contents, CONFIGURABLE_CHECKS);
438 let mut leading_new_lines = false;
439 let mut trailing_new_lines = 0;
440 let mut lines = 0;
441 let mut last_safety_comment = false;
442 let mut comment_block: Option<(usize, usize)> = None;
443 let is_test = file.components().any(|c| c.as_os_str() == "tests")
444 || file.file_stem().unwrap() == "tests";
445 let is_this_file = file.ends_with(this_file) || this_file.ends_with(file);
446 let is_test_for_this_file =
447 is_test && file.parent().unwrap().ends_with(this_file.with_extension(""));
448 let any_problematic_line =
451 !is_this_file && !is_test_for_this_file && problematic_regex.is_match(contents);
452 for (i, line) in contents.split('\n').enumerate() {
453 if line.is_empty() {
454 if i == 0 {
455 leading_new_lines = true;
456 }
457 trailing_new_lines += 1;
458 continue;
459 } else {
460 trailing_new_lines = 0;
461 }
462
463 let trimmed = line.trim();
464
465 if !trimmed.starts_with("//") {
466 lines += 1;
467 }
468
469 let mut err = |msg: &str| {
470 tidy_error!(bad, "{}:{}: {}", file.display(), i + 1, msg);
471 };
472
473 if trimmed.contains("dbg!")
474 && !trimmed.starts_with("//")
475 && !file.ancestors().any(|a| {
476 (a.ends_with("tests") && a.join("COMPILER_TESTS.md").exists())
477 || a.ends_with("library/alloctests")
478 })
479 && filename != "tests.rs"
480 {
481 suppressible_tidy_err!(
482 err,
483 skip_dbg,
484 "`dbg!` macro is intended as a debugging tool. It should not be in version control."
485 )
486 }
487
488 if !under_rustfmt
489 && line.chars().count() > max_columns
490 && !long_line_is_ok(&extension, is_error_code, max_columns, line)
491 {
492 suppressible_tidy_err!(
493 err,
494 skip_line_length,
495 "line longer than {max_columns} chars"
496 );
497 }
498 if !is_css_file && line.contains('\t') {
499 suppressible_tidy_err!(err, skip_tab, "tab character");
500 }
501 if line.ends_with(' ') || line.ends_with('\t') {
502 suppressible_tidy_err!(err, skip_end_whitespace, "trailing whitespace");
503 }
504 if is_css_file && line.starts_with(' ') {
505 err("CSS files use tabs for indent");
506 }
507 if line.contains('\r') {
508 suppressible_tidy_err!(err, skip_cr, "CR character");
509 }
510 if !is_this_file {
511 let directive_line_starts = ["// ", "# ", "/* ", "<!-- "];
512 let possible_line_start =
513 directive_line_starts.into_iter().any(|s| line.starts_with(s));
514 let contains_potential_directive =
515 possible_line_start && (line.contains("-tidy") || line.contains("tidy-"));
516 let has_recognized_ignore_directive =
517 contains_ignore_directives(&path_str, can_contain, line, CONFIGURABLE_CHECKS)
518 .into_iter()
519 .any(|directive| matches!(directive, Directive::Ignore(_)));
520 let has_alphabetical_directive = line.contains("tidy-alphabetical-start")
521 || line.contains("tidy-alphabetical-end");
522 let has_other_tidy_ignore_directive =
523 line.contains("ignore-tidy-target-specific-tests");
524 let has_recognized_directive = has_recognized_ignore_directive
525 || has_alphabetical_directive
526 || has_other_tidy_ignore_directive;
527 if contains_potential_directive && (!has_recognized_directive) {
528 err("Unrecognized tidy directive")
529 }
530 if trimmed.contains("TODO") && !trimmed.contains("ignore-tidy-todo") {
533 err(
534 "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",
535 )
536 }
537 if trimmed.contains("//") && trimmed.contains(" XXX") {
538 err("Instead of XXX use FIXME")
539 }
540 if any_problematic_line && contains_problematic_const(trimmed) {
541 err("Don't use magic numbers that spell things (consider 0x12345678)");
542 }
543 }
544 if trimmed.contains("unsafe {")
546 && !trimmed.starts_with("//")
547 && !last_safety_comment
548 && file.components().any(|c| c.as_os_str() == "core")
549 && !is_test
550 {
551 suppressible_tidy_err!(err, skip_undocumented_unsafe, "undocumented unsafe");
552 }
553 if trimmed.contains("// SAFETY:") {
554 last_safety_comment = true;
555 } else if trimmed.starts_with("//") || trimmed.is_empty() {
556 } else {
558 last_safety_comment = false;
559 }
560 if (line.starts_with("// Copyright")
561 || line.starts_with("# Copyright")
562 || line.starts_with("Copyright"))
563 && (trimmed.contains("Rust Developers")
564 || trimmed.contains("Rust Project Developers"))
565 {
566 suppressible_tidy_err!(
567 err,
568 skip_copyright,
569 "copyright notices attributed to the Rust Project Developers are deprecated"
570 );
571 }
572 if !file.components().any(|c| c.as_os_str() == "rustc_baked_icu_data")
573 && is_unexplained_ignore(&extension, line)
574 {
575 err(UNEXPLAINED_IGNORE_DOCTEST_INFO);
576 }
577
578 if filename.ends_with(".cpp") && line.contains("llvm_unreachable") {
579 err(LLVM_UNREACHABLE_INFO);
580 }
581
582 let is_compiler = || file.components().any(|c| c.as_os_str() == "compiler");
584
585 if is_compiler() {
586 if line.contains("//")
587 && line
588 .chars()
589 .collect::<Vec<_>>()
590 .windows(4)
591 .any(|cs| matches!(cs, ['.', ' ', ' ', last] if last.is_alphabetic()))
592 {
593 err(DOUBLE_SPACE_AFTER_DOT)
594 }
595
596 if filename.ends_with(".ftl") {
597 let line_backticks = trimmed.chars().filter(|ch| *ch == '`').count();
598 if line_backticks % 2 == 1 {
599 suppressible_tidy_err!(err, skip_odd_backticks, "odd number of backticks");
600 }
601 } else if trimmed.contains("//") {
602 let (start_line, mut backtick_count) = comment_block.unwrap_or((i + 1, 0));
603 let line_backticks = trimmed.chars().filter(|ch| *ch == '`').count();
604 let comment_text = trimmed.split("//").nth(1).unwrap();
605 if line_backticks % 2 == 1 {
607 backtick_count += comment_text.chars().filter(|ch| *ch == '`').count();
608 }
609 comment_block = Some((start_line, backtick_count));
610 } else if let Some((start_line, backtick_count)) = comment_block.take()
611 && backtick_count % 2 == 1
612 {
613 let mut err = |msg: &str| {
614 tidy_error!(bad, "{}:{start_line}: {msg}", file.display());
615 };
616 let block_len = (i + 1) - start_line;
617 if block_len == 1 {
618 suppressible_tidy_err!(
619 err,
620 skip_odd_backticks,
621 "comment with odd number of backticks"
622 );
623 } else {
624 suppressible_tidy_err!(
625 err,
626 skip_odd_backticks,
627 "{block_len}-line comment block with odd number of backticks"
628 );
629 }
630 }
631 }
632 }
633 if leading_new_lines {
634 let mut err = |_| {
635 tidy_error!(bad, "{}: leading newline", file.display());
636 };
637 suppressible_tidy_err!(err, skip_leading_newlines, "missing leading newline");
638 }
639 let mut err = |msg: &str| {
640 tidy_error!(bad, "{}: {}", file.display(), msg);
641 };
642 match trailing_new_lines {
643 0 => suppressible_tidy_err!(err, skip_trailing_newlines, "missing trailing newline"),
644 1 => {}
645 n => suppressible_tidy_err!(
646 err,
647 skip_trailing_newlines,
648 "too many trailing newlines ({n})"
649 ),
650 };
651 if lines > LINES {
652 let mut err = |_| {
653 tidy_error!(
654 bad,
655 "{}: too many lines ({}) (add `// \
656 ignore-tidy-filelength` to the file to suppress this error)",
657 file.display(),
658 lines
659 );
660 };
661 suppressible_tidy_err!(err, skip_file_length, "");
662 }
663
664 if let Directive::Ignore(false) = skip_cr {
665 tidy_error!(bad, "{}: ignoring CR characters unnecessarily", file.display());
666 }
667 if let Directive::Ignore(false) = skip_tab {
668 tidy_error!(bad, "{}: ignoring tab characters unnecessarily", file.display());
669 }
670 if let Directive::Ignore(false) = skip_end_whitespace {
671 tidy_error!(bad, "{}: ignoring trailing whitespace unnecessarily", file.display());
672 }
673 if let Directive::Ignore(false) = skip_trailing_newlines {
674 tidy_error!(bad, "{}: ignoring trailing newlines unnecessarily", file.display());
675 }
676 if let Directive::Ignore(false) = skip_leading_newlines {
677 tidy_error!(bad, "{}: ignoring leading newlines unnecessarily", file.display());
678 }
679 if let Directive::Ignore(false) = skip_copyright {
680 tidy_error!(bad, "{}: ignoring copyright unnecessarily", file.display());
681 }
682 let _unused = skip_line_length;
685 let _unused = skip_file_length;
686 })
687}