]> git.proxmox.com Git - rustc.git/blob - src/tools/tidy/src/style.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / src / tools / tidy / src / style.rs
1 //! Tidy check to enforce various stylistic guidelines on the Rust codebase.
2 //!
3 //! Example checks are:
4 //!
5 //! * No lines over 100 characters (in non-Rust files).
6 //! * No files with over 3000 lines (in non-Rust files).
7 //! * No tabs.
8 //! * No trailing whitespace.
9 //! * No CR characters.
10 //! * No `TODO` or `XXX` directives.
11 //! * No unexplained ` ```ignore ` or ` ```rust,ignore ` doc tests.
12 //!
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.
15 //!
16 //! A number of these checks can be opted-out of with various directives of the form:
17 //! `// ignore-tidy-CHECK-NAME`.
18
19 use regex::Regex;
20 use std::path::Path;
21
22 /// Error code markdown is restricted to 80 columns because they can be
23 /// displayed on the console with --example.
24 const ERROR_CODE_COLS: usize = 80;
25 const COLS: usize = 100;
26
27 const LINES: usize = 3000;
28
29 const UNEXPLAINED_IGNORE_DOCTEST_INFO: &str = r#"unexplained "```ignore" doctest; try one:
30
31 * make the test actually pass, by adding necessary imports and declarations, or
32 * use "```text", if the code is not Rust code, or
33 * use "```compile_fail,Ennnn", if the code is expected to fail at compile time, or
34 * use "```should_panic", if the code is expected to fail at run time, or
35 * use "```no_run", if the code should type-check but not necessary linkable/runnable, or
36 * explain it like "```ignore (cannot-test-this-because-xxxx)", if the annotation cannot be avoided.
37
38 "#;
39
40 const LLVM_UNREACHABLE_INFO: &str = r"\
41 C++ code used llvm_unreachable, which triggers undefined behavior
42 when executed when assertions are disabled.
43 Use llvm::report_fatal_error for increased robustness.";
44
45 const ANNOTATIONS_TO_IGNORE: &[&str] = &[
46 "// @!has",
47 "// @has",
48 "// @matches",
49 "// CHECK",
50 "// EMIT_MIR",
51 "// compile-flags",
52 "// error-pattern",
53 "// gdb",
54 "// lldb",
55 "// normalize-stderr-test",
56 ];
57
58 /// Parser states for `line_is_url`.
59 #[derive(Clone, Copy, PartialEq)]
60 #[allow(non_camel_case_types)]
61 enum LIUState {
62 EXP_COMMENT_START,
63 EXP_LINK_LABEL_OR_URL,
64 EXP_URL,
65 EXP_END,
66 }
67
68 /// Returns `true` if `line` appears to be a line comment containing an URL,
69 /// possibly with a Markdown link label in front, and nothing else.
70 /// The Markdown link label, if present, may not contain whitespace.
71 /// Lines of this form are allowed to be overlength, because Markdown
72 /// offers no way to split a line in the middle of a URL, and the lengths
73 /// of URLs to external references are beyond our control.
74 fn line_is_url(is_error_code: bool, columns: usize, line: &str) -> bool {
75 // more basic check for markdown, to avoid complexity in implementing two state machines
76 if is_error_code {
77 return line.starts_with('[') && line.contains("]:") && line.contains("http");
78 }
79
80 use self::LIUState::*;
81 let mut state: LIUState = EXP_COMMENT_START;
82 let is_url = |w: &str| w.starts_with("http://") || w.starts_with("https://");
83
84 for tok in line.split_whitespace() {
85 match (state, tok) {
86 (EXP_COMMENT_START, "//") | (EXP_COMMENT_START, "///") | (EXP_COMMENT_START, "//!") => {
87 state = EXP_LINK_LABEL_OR_URL
88 }
89
90 (EXP_LINK_LABEL_OR_URL, w)
91 if w.len() >= 4 && w.starts_with('[') && w.ends_with("]:") =>
92 {
93 state = EXP_URL
94 }
95
96 (EXP_LINK_LABEL_OR_URL, w) if is_url(w) => state = EXP_END,
97
98 (EXP_URL, w) if is_url(w) || w.starts_with("../") => state = EXP_END,
99
100 (_, w) if w.len() > columns && is_url(w) => state = EXP_END,
101
102 (_, _) => {}
103 }
104 }
105
106 state == EXP_END
107 }
108
109 /// Returns `true` if `line` can be ignored. This is the case when it contains
110 /// an annotation that is explicitly ignored.
111 fn should_ignore(line: &str) -> bool {
112 // Matches test annotations like `//~ ERROR text`.
113 // This mirrors the regex in src/tools/compiletest/src/runtest.rs, please
114 // update both if either are changed.
115 let re = Regex::new("\\s*//(\\[.*\\])?~.*").unwrap();
116 re.is_match(line) || ANNOTATIONS_TO_IGNORE.iter().any(|a| line.contains(a))
117 }
118
119 /// Returns `true` if `line` is allowed to be longer than the normal limit.
120 fn long_line_is_ok(extension: &str, is_error_code: bool, max_columns: usize, line: &str) -> bool {
121 if extension != "md" || is_error_code {
122 if line_is_url(is_error_code, max_columns, line) || should_ignore(line) {
123 return true;
124 }
125 } else if extension == "md" {
126 // non-error code markdown is allowed to be any length
127 return true;
128 }
129
130 false
131 }
132
133 enum Directive {
134 /// By default, tidy always warns against style issues.
135 Deny,
136
137 /// `Ignore(false)` means that an `ignore-tidy-*` directive
138 /// has been provided, but is unnecessary. `Ignore(true)`
139 /// means that it is necessary (i.e. a warning would be
140 /// produced if `ignore-tidy-*` was not present).
141 Ignore(bool),
142 }
143
144 fn contains_ignore_directive(can_contain: bool, contents: &str, check: &str) -> Directive {
145 if !can_contain {
146 return Directive::Deny;
147 }
148 // Update `can_contain` when changing this
149 if contents.contains(&format!("// ignore-tidy-{}", check))
150 || contents.contains(&format!("# ignore-tidy-{}", check))
151 || contents.contains(&format!("/* ignore-tidy-{} */", check))
152 {
153 Directive::Ignore(false)
154 } else {
155 Directive::Deny
156 }
157 }
158
159 macro_rules! suppressible_tidy_err {
160 ($err:ident, $skip:ident, $msg:expr) => {
161 if let Directive::Deny = $skip {
162 $err($msg);
163 } else {
164 $skip = Directive::Ignore(true);
165 }
166 };
167 }
168
169 pub fn is_in(full_path: &Path, parent_folder_to_find: &str, folder_to_find: &str) -> bool {
170 if let Some(parent) = full_path.parent() {
171 if parent.file_name().map_or_else(
172 || false,
173 |f| {
174 f.to_string_lossy() == folder_to_find
175 && parent
176 .parent()
177 .and_then(|f| f.file_name())
178 .map_or_else(|| false, |f| f == parent_folder_to_find)
179 },
180 ) {
181 true
182 } else {
183 is_in(parent, parent_folder_to_find, folder_to_find)
184 }
185 } else {
186 false
187 }
188 }
189
190 fn skip_markdown_path(path: &Path) -> bool {
191 // These aren't ready for tidy.
192 const SKIP_MD: &[&str] = &[
193 "src/doc/edition-guide",
194 "src/doc/embedded-book",
195 "src/doc/nomicon",
196 "src/doc/reference",
197 "src/doc/rust-by-example",
198 "src/doc/rustc-dev-guide",
199 ];
200 SKIP_MD.iter().any(|p| path.ends_with(p))
201 }
202
203 fn is_unexplained_ignore(extension: &str, line: &str) -> bool {
204 if !line.ends_with("```ignore") && !line.ends_with("```rust,ignore") {
205 return false;
206 }
207 if extension == "md" && line.trim().starts_with("//") {
208 // Markdown examples may include doc comments with ignore inside a
209 // code block.
210 return false;
211 }
212 true
213 }
214
215 pub fn check(path: &Path, bad: &mut bool) {
216 fn skip(path: &Path) -> bool {
217 super::filter_dirs(path) || skip_markdown_path(path)
218 }
219 super::walk(path, &mut skip, &mut |entry, contents| {
220 let file = entry.path();
221 let filename = file.file_name().unwrap().to_string_lossy();
222 let extensions = [".rs", ".py", ".js", ".sh", ".c", ".cpp", ".h", ".md", ".css"];
223 if extensions.iter().all(|e| !filename.ends_with(e)) || filename.starts_with(".#") {
224 return;
225 }
226
227 let is_style_file = filename.ends_with(".css");
228 let under_rustfmt = filename.ends_with(".rs") &&
229 // This list should ideally be sourced from rustfmt.toml but we don't want to add a toml
230 // parser to tidy.
231 !file.ancestors().any(|a| {
232 a.ends_with("src/test") ||
233 a.ends_with("src/doc/book")
234 });
235
236 if is_style_file && !is_in(file, "src", "librustdoc") {
237 // We only check CSS files in rustdoc.
238 return;
239 }
240
241 if contents.is_empty() {
242 tidy_error!(bad, "{}: empty file", file.display());
243 }
244
245 let extension = file.extension().unwrap().to_string_lossy();
246 let is_error_code = extension == "md" && is_in(file, "src", "error_codes");
247
248 let max_columns = if is_error_code { ERROR_CODE_COLS } else { COLS };
249
250 let can_contain = contents.contains("// ignore-tidy-")
251 || contents.contains("# ignore-tidy-")
252 || contents.contains("/* ignore-tidy-");
253 // Enable testing ICE's that require specific (untidy)
254 // file formats easily eg. `issue-1234-ignore-tidy.rs`
255 if filename.contains("ignore-tidy") {
256 return;
257 }
258 let mut skip_cr = contains_ignore_directive(can_contain, &contents, "cr");
259 let mut skip_undocumented_unsafe =
260 contains_ignore_directive(can_contain, &contents, "undocumented-unsafe");
261 let mut skip_tab = contains_ignore_directive(can_contain, &contents, "tab");
262 let mut skip_line_length = contains_ignore_directive(can_contain, &contents, "linelength");
263 let mut skip_file_length = contains_ignore_directive(can_contain, &contents, "filelength");
264 let mut skip_end_whitespace =
265 contains_ignore_directive(can_contain, &contents, "end-whitespace");
266 let mut skip_trailing_newlines =
267 contains_ignore_directive(can_contain, &contents, "trailing-newlines");
268 let mut skip_copyright = contains_ignore_directive(can_contain, &contents, "copyright");
269 let mut leading_new_lines = false;
270 let mut trailing_new_lines = 0;
271 let mut lines = 0;
272 let mut last_safety_comment = false;
273 for (i, line) in contents.split('\n').enumerate() {
274 let mut err = |msg: &str| {
275 tidy_error!(bad, "{}:{}: {}", file.display(), i + 1, msg);
276 };
277 if !under_rustfmt
278 && line.chars().count() > max_columns
279 && !long_line_is_ok(&extension, is_error_code, max_columns, line)
280 {
281 suppressible_tidy_err!(
282 err,
283 skip_line_length,
284 &format!("line longer than {} chars", max_columns)
285 );
286 }
287 if !is_style_file && line.contains('\t') {
288 suppressible_tidy_err!(err, skip_tab, "tab character");
289 }
290 if line.ends_with(' ') || line.ends_with('\t') {
291 suppressible_tidy_err!(err, skip_end_whitespace, "trailing whitespace");
292 }
293 if is_style_file && line.starts_with(' ') {
294 err("CSS files use tabs for indent");
295 }
296 if line.contains('\r') {
297 suppressible_tidy_err!(err, skip_cr, "CR character");
298 }
299 if filename != "style.rs" {
300 if line.contains("TODO") {
301 err("TODO is deprecated; use FIXME")
302 }
303 if line.contains("//") && line.contains(" XXX") {
304 err("XXX is deprecated; use FIXME")
305 }
306 }
307 let is_test = || file.components().any(|c| c.as_os_str() == "tests");
308 // for now we just check libcore
309 if line.contains("unsafe {") && !line.trim().starts_with("//") && !last_safety_comment {
310 if file.components().any(|c| c.as_os_str() == "core") && !is_test() {
311 suppressible_tidy_err!(err, skip_undocumented_unsafe, "undocumented unsafe");
312 }
313 }
314 if line.contains("// SAFETY:") {
315 last_safety_comment = true;
316 } else if line.trim().starts_with("//") || line.trim().is_empty() {
317 // keep previous value
318 } else {
319 last_safety_comment = false;
320 }
321 if (line.starts_with("// Copyright")
322 || line.starts_with("# Copyright")
323 || line.starts_with("Copyright"))
324 && (line.contains("Rust Developers") || line.contains("Rust Project Developers"))
325 {
326 suppressible_tidy_err!(
327 err,
328 skip_copyright,
329 "copyright notices attributed to the Rust Project Developers are deprecated"
330 );
331 }
332 if is_unexplained_ignore(&extension, line) {
333 err(UNEXPLAINED_IGNORE_DOCTEST_INFO);
334 }
335 if filename.ends_with(".cpp") && line.contains("llvm_unreachable") {
336 err(LLVM_UNREACHABLE_INFO);
337 }
338 if line.is_empty() {
339 if i == 0 {
340 leading_new_lines = true;
341 }
342 trailing_new_lines += 1;
343 } else {
344 trailing_new_lines = 0;
345 }
346 lines = i;
347 }
348 if leading_new_lines {
349 tidy_error!(bad, "{}: leading newline", file.display());
350 }
351 let mut err = |msg: &str| {
352 tidy_error!(bad, "{}: {}", file.display(), msg);
353 };
354 match trailing_new_lines {
355 0 => suppressible_tidy_err!(err, skip_trailing_newlines, "missing trailing newline"),
356 1 => {}
357 n => suppressible_tidy_err!(
358 err,
359 skip_trailing_newlines,
360 &format!("too many trailing newlines ({})", n)
361 ),
362 };
363 if lines > LINES {
364 let mut err = |_| {
365 tidy_error!(
366 bad,
367 "{}: too many lines ({}) (add `// \
368 ignore-tidy-filelength` to the file to suppress this error)",
369 file.display(),
370 lines
371 );
372 };
373 suppressible_tidy_err!(err, skip_file_length, "");
374 }
375
376 if let Directive::Ignore(false) = skip_cr {
377 tidy_error!(bad, "{}: ignoring CR characters unnecessarily", file.display());
378 }
379 if let Directive::Ignore(false) = skip_tab {
380 tidy_error!(bad, "{}: ignoring tab characters unnecessarily", file.display());
381 }
382 if let Directive::Ignore(false) = skip_line_length {
383 tidy_error!(bad, "{}: ignoring line length unnecessarily", file.display());
384 }
385 if let Directive::Ignore(false) = skip_file_length {
386 tidy_error!(bad, "{}: ignoring file length unnecessarily", file.display());
387 }
388 if let Directive::Ignore(false) = skip_end_whitespace {
389 tidy_error!(bad, "{}: ignoring trailing whitespace unnecessarily", file.display());
390 }
391 if let Directive::Ignore(false) = skip_trailing_newlines {
392 tidy_error!(bad, "{}: ignoring trailing newlines unnecessarily", file.display());
393 }
394 if let Directive::Ignore(false) = skip_copyright {
395 tidy_error!(bad, "{}: ignoring copyright unnecessarily", file.display());
396 }
397 })
398 }