1 //! Tidy check to enforce various stylistic guidelines on the Rust codebase.
3 //! Example checks are:
5 //! * No lines over 100 characters (in non-Rust files).
6 //! * No files with over 3000 lines (in non-Rust files).
8 //! * No trailing whitespace.
9 //! * No CR characters.
10 //! * No `TODO` or `XXX` directives.
11 //! * No unexplained ` ```ignore ` or ` ```rust,ignore ` doc tests.
13 //! Note that some of these rules are excluded from Rust files because we enforce rustfmt. It is
14 //! preferable to be formatted rather than tidy-clean.
16 //! A number of these checks can be opted-out of with various directives of the form:
17 //! `// ignore-tidy-CHECK-NAME`.
20 use crate::walk::{filter_dirs, walk};
21 use regex::{Regex, RegexSet};
24 /// Error code markdown is restricted to 80 columns because they can be
25 /// displayed on the console with --example.
26 const ERROR_CODE_COLS: usize = 80;
27 const COLS: usize = 100;
28 const GOML_COLS: usize = 120;
30 const LINES: usize = 3000;
32 const UNEXPLAINED_IGNORE_DOCTEST_INFO: &str = r#"unexplained "```ignore" doctest; try one:
34 * make the test actually pass, by adding necessary imports and declarations, or
35 * use "```text", if the code is not Rust code, or
36 * use "```compile_fail,Ennnn", if the code is expected to fail at compile time, or
37 * use "```should_panic", if the code is expected to fail at run time, or
38 * use "```no_run", if the code should type-check but not necessary linkable/runnable, or
39 * explain it like "```ignore (cannot-test-this-because-xxxx)", if the annotation cannot be avoided.
43 const LLVM_UNREACHABLE_INFO: &str = r"\
44 C++ code used llvm_unreachable, which triggers undefined behavior
45 when executed when assertions are disabled.
46 Use llvm::report_fatal_error for increased robustness.";
48 const ANNOTATIONS_TO_IGNORE: &[&str] = &[
59 "// normalize-stderr-test",
62 // Intentionally written in decimal rather than hex
63 const PROBLEMATIC_CONSTS: &[u32] = &[
64 184594741, 2880289470, 2881141438, 2965027518, 2976579765, 3203381950, 3405691582, 3405697037,
65 3735927486, 3735932941, 4027431614, 4276992702,
68 const INTERNAL_COMPILER_DOCS_LINE: &str = "#### This error code is internal to the compiler and will not be emitted with normal Rust code.";
70 /// Parser states for `line_is_url`.
71 #[derive(Clone, Copy, PartialEq)]
72 #[allow(non_camel_case_types)]
75 EXP_LINK_LABEL_OR_URL,
80 /// Returns `true` if `line` appears to be a line comment containing a URL,
81 /// possibly with a Markdown link label in front, and nothing else.
82 /// The Markdown link label, if present, may not contain whitespace.
83 /// Lines of this form are allowed to be overlength, because Markdown
84 /// offers no way to split a line in the middle of a URL, and the lengths
85 /// of URLs to external references are beyond our control.
86 fn line_is_url(is_error_code: bool, columns: usize, line: &str) -> bool {
87 // more basic check for markdown, to avoid complexity in implementing two state machines
89 return line.starts_with('[') && line.contains("]:") && line.contains("http");
92 use self::LIUState::*;
93 let mut state: LIUState = EXP_COMMENT_START;
94 let is_url = |w: &str| w.starts_with("http://") || w.starts_with("https://");
96 for tok in line.split_whitespace() {
98 (EXP_COMMENT_START, "//") | (EXP_COMMENT_START, "///") | (EXP_COMMENT_START, "//!") => {
99 state = EXP_LINK_LABEL_OR_URL
102 (EXP_LINK_LABEL_OR_URL, w)
103 if w.len() >= 4 && w.starts_with('[') && w.ends_with("]:") =>
108 (EXP_LINK_LABEL_OR_URL, w) if is_url(w) => state = EXP_END,
110 (EXP_URL, w) if is_url(w) || w.starts_with("../") => state = EXP_END,
112 (_, w) if w.len() > columns && is_url(w) => state = EXP_END,
121 /// Returns `true` if `line` can be ignored. This is the case when it contains
122 /// an annotation that is explicitly ignored.
123 fn should_ignore(line: &str) -> bool {
124 // Matches test annotations like `//~ ERROR text`.
125 // This mirrors the regex in src/tools/compiletest/src/runtest.rs, please
126 // update both if either are changed.
127 let re = Regex::new("\\s*//(\\[.*\\])?~.*").unwrap();
128 re.is_match(line) || ANNOTATIONS_TO_IGNORE.iter().any(|a| line.contains(a))
131 /// Returns `true` if `line` is allowed to be longer than the normal limit.
132 fn long_line_is_ok(extension: &str, is_error_code: bool, max_columns: usize, line: &str) -> bool {
134 // fluent files are allowed to be any length
136 // non-error code markdown is allowed to be any length
137 "md" if !is_error_code => true,
138 // HACK(Ezrashaw): there is no way to split a markdown header over multiple lines
139 "md" if line == INTERNAL_COMPILER_DOCS_LINE => true,
140 _ => line_is_url(is_error_code, max_columns, line) || should_ignore(line),
145 /// By default, tidy always warns against style issues.
148 /// `Ignore(false)` means that an `ignore-tidy-*` directive
149 /// has been provided, but is unnecessary. `Ignore(true)`
150 /// means that it is necessary (i.e. a warning would be
151 /// produced if `ignore-tidy-*` was not present).
155 fn contains_ignore_directive(can_contain: bool, contents: &str, check: &str) -> Directive {
157 return Directive::Deny;
159 // Update `can_contain` when changing this
160 if contents.contains(&format!("// ignore-tidy-{check}"))
161 || contents.contains(&format!("# ignore-tidy-{check}"))
162 || contents.contains(&format!("/* ignore-tidy-{check} */"))
164 Directive::Ignore(false)
170 macro_rules! suppressible_tidy_err {
171 ($err:ident, $skip:ident, $msg:expr) => {
172 if let Directive::Deny = $skip {
175 $skip = Directive::Ignore(true);
180 pub fn is_in(full_path: &Path, parent_folder_to_find: &str, folder_to_find: &str) -> bool {
181 if let Some(parent) = full_path.parent() {
182 if parent.file_name().map_or_else(
185 f.to_string_lossy() == folder_to_find
188 .and_then(|f| f.file_name())
189 .map_or_else(|| false, |f| f == parent_folder_to_find)
194 is_in(parent, parent_folder_to_find, folder_to_find)
201 fn skip_markdown_path(path: &Path) -> bool {
202 // These aren't ready for tidy.
203 const SKIP_MD: &[&str] = &[
204 "src/doc/edition-guide",
205 "src/doc/embedded-book",
208 "src/doc/rust-by-example",
209 "src/doc/rustc-dev-guide",
211 SKIP_MD.iter().any(|p| path.ends_with(p))
214 fn is_unexplained_ignore(extension: &str, line: &str) -> bool {
215 if !line.ends_with("```ignore") && !line.ends_with("```rust,ignore") {
218 if extension == "md" && line.trim().starts_with("//") {
219 // Markdown examples may include doc comments with ignore inside a
226 pub fn check(path: &Path, bad: &mut bool) {
227 fn skip(path: &Path) -> bool {
228 filter_dirs(path) || skip_markdown_path(path)
230 let problematic_consts_strings: Vec<String> = (PROBLEMATIC_CONSTS.iter().map(u32::to_string))
231 .chain(PROBLEMATIC_CONSTS.iter().map(|v| format!("{:x}", v)))
232 .chain(PROBLEMATIC_CONSTS.iter().map(|v| format!("{:X}", v)))
234 let problematic_regex = RegexSet::new(problematic_consts_strings.as_slice()).unwrap();
235 walk(path, &mut skip, &mut |entry, contents| {
236 let file = entry.path();
237 let filename = file.file_name().unwrap().to_string_lossy();
239 [".rs", ".py", ".js", ".sh", ".c", ".cpp", ".h", ".md", ".css", ".ftl", ".goml"];
240 if extensions.iter().all(|e| !filename.ends_with(e)) || filename.starts_with(".#") {
244 let is_style_file = filename.ends_with(".css");
245 let under_rustfmt = filename.ends_with(".rs") &&
246 // This list should ideally be sourced from rustfmt.toml but we don't want to add a toml
248 !file.ancestors().any(|a| {
249 a.ends_with("src/test") ||
250 a.ends_with("src/doc/book")
253 if is_style_file && !is_in(file, "src", "librustdoc") {
254 // We only check CSS files in rustdoc.
258 if contents.is_empty() {
259 tidy_error!(bad, "{}: empty file", file.display());
262 let extension = file.extension().unwrap().to_string_lossy();
263 let is_error_code = extension == "md" && is_in(file, "src", "error_codes");
264 let is_goml_code = extension == "goml";
266 let max_columns = if is_error_code {
268 } else if is_goml_code {
274 let can_contain = contents.contains("// ignore-tidy-")
275 || contents.contains("# ignore-tidy-")
276 || contents.contains("/* ignore-tidy-");
277 // Enable testing ICE's that require specific (untidy)
278 // file formats easily eg. `issue-1234-ignore-tidy.rs`
279 if filename.contains("ignore-tidy") {
282 let mut skip_cr = contains_ignore_directive(can_contain, &contents, "cr");
283 let mut skip_undocumented_unsafe =
284 contains_ignore_directive(can_contain, &contents, "undocumented-unsafe");
285 let mut skip_tab = contains_ignore_directive(can_contain, &contents, "tab");
286 let mut skip_line_length = contains_ignore_directive(can_contain, &contents, "linelength");
287 let mut skip_file_length = contains_ignore_directive(can_contain, &contents, "filelength");
288 let mut skip_end_whitespace =
289 contains_ignore_directive(can_contain, &contents, "end-whitespace");
290 let mut skip_trailing_newlines =
291 contains_ignore_directive(can_contain, &contents, "trailing-newlines");
292 let mut skip_leading_newlines =
293 contains_ignore_directive(can_contain, &contents, "leading-newlines");
294 let mut skip_copyright = contains_ignore_directive(can_contain, &contents, "copyright");
295 let mut skip_dbg = contains_ignore_directive(can_contain, &contents, "dbg");
296 let mut leading_new_lines = false;
297 let mut trailing_new_lines = 0;
299 let mut last_safety_comment = false;
300 let is_test = file.components().any(|c| c.as_os_str() == "tests");
301 // scanning the whole file for multiple needles at once is more efficient than
302 // executing lines times needles separate searches.
303 let any_problematic_line = problematic_regex.is_match(contents);
304 for (i, line) in contents.split('\n').enumerate() {
307 leading_new_lines = true;
309 trailing_new_lines += 1;
312 trailing_new_lines = 0;
315 let trimmed = line.trim();
317 if !trimmed.starts_with("//") {
321 let mut err = |msg: &str| {
322 tidy_error!(bad, "{}:{}: {}", file.display(), i + 1, msg);
325 if trimmed.contains("dbg!")
326 && !trimmed.starts_with("//")
329 .any(|a| a.ends_with("src/test") || a.ends_with("library/alloc/tests"))
330 && filename != "tests.rs"
332 suppressible_tidy_err!(
335 "`dbg!` macro is intended as a debugging tool. It should not be in version control."
340 && line.chars().count() > max_columns
341 && !long_line_is_ok(&extension, is_error_code, max_columns, line)
343 suppressible_tidy_err!(
346 &format!("line longer than {max_columns} chars")
349 if !is_style_file && line.contains('\t') {
350 suppressible_tidy_err!(err, skip_tab, "tab character");
352 if line.ends_with(' ') || line.ends_with('\t') {
353 suppressible_tidy_err!(err, skip_end_whitespace, "trailing whitespace");
355 if is_style_file && line.starts_with(' ') {
356 err("CSS files use tabs for indent");
358 if line.contains('\r') {
359 suppressible_tidy_err!(err, skip_cr, "CR character");
361 if filename != "style.rs" {
362 if trimmed.contains("TODO") {
363 err("TODO is deprecated; use FIXME")
365 if trimmed.contains("//") && trimmed.contains(" XXX") {
366 err("XXX is deprecated; use FIXME")
368 if any_problematic_line {
369 for s in problematic_consts_strings.iter() {
370 if trimmed.contains(s) {
371 err("Don't use magic numbers that spell things (consider 0x12345678)");
376 // for now we just check libcore
377 if trimmed.contains("unsafe {") && !trimmed.starts_with("//") && !last_safety_comment {
378 if file.components().any(|c| c.as_os_str() == "core") && !is_test {
379 suppressible_tidy_err!(err, skip_undocumented_unsafe, "undocumented unsafe");
382 if trimmed.contains("// SAFETY:") {
383 last_safety_comment = true;
384 } else if trimmed.starts_with("//") || trimmed.is_empty() {
385 // keep previous value
387 last_safety_comment = false;
389 if (line.starts_with("// Copyright")
390 || line.starts_with("# Copyright")
391 || line.starts_with("Copyright"))
392 && (trimmed.contains("Rust Developers")
393 || trimmed.contains("Rust Project Developers"))
395 suppressible_tidy_err!(
398 "copyright notices attributed to the Rust Project Developers are deprecated"
401 if is_unexplained_ignore(&extension, line) {
402 err(UNEXPLAINED_IGNORE_DOCTEST_INFO);
404 if filename.ends_with(".cpp") && line.contains("llvm_unreachable") {
405 err(LLVM_UNREACHABLE_INFO);
408 if leading_new_lines {
410 tidy_error!(bad, "{}: leading newline", file.display());
412 suppressible_tidy_err!(err, skip_leading_newlines, "mising leading newline");
414 let mut err = |msg: &str| {
415 tidy_error!(bad, "{}: {}", file.display(), msg);
417 match trailing_new_lines {
418 0 => suppressible_tidy_err!(err, skip_trailing_newlines, "missing trailing newline"),
420 n => suppressible_tidy_err!(
422 skip_trailing_newlines,
423 &format!("too many trailing newlines ({n})")
430 "{}: too many lines ({}) (add `// \
431 ignore-tidy-filelength` to the file to suppress this error)",
436 suppressible_tidy_err!(err, skip_file_length, "");
439 if let Directive::Ignore(false) = skip_cr {
440 tidy_error!(bad, "{}: ignoring CR characters unnecessarily", file.display());
442 if let Directive::Ignore(false) = skip_tab {
443 tidy_error!(bad, "{}: ignoring tab characters unnecessarily", file.display());
445 if let Directive::Ignore(false) = skip_end_whitespace {
446 tidy_error!(bad, "{}: ignoring trailing whitespace unnecessarily", file.display());
448 if let Directive::Ignore(false) = skip_trailing_newlines {
449 tidy_error!(bad, "{}: ignoring trailing newlines unnecessarily", file.display());
451 if let Directive::Ignore(false) = skip_leading_newlines {
452 tidy_error!(bad, "{}: ignoring leading newlines unnecessarily", file.display());
454 if let Directive::Ignore(false) = skip_copyright {
455 tidy_error!(bad, "{}: ignoring copyright unnecessarily", file.display());
457 // We deliberately do not warn about these being unnecessary,
458 // that would just lead to annoying churn.
459 let _unused = skip_line_length;
460 let _unused = skip_file_length;