1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
use crate::walk::{filter_dirs, walk};
use regex::{Regex, RegexSet};
use std::path::Path;
const ERROR_CODE_COLS: usize = 80;
const COLS: usize = 100;
const GOML_COLS: usize = 120;
const LINES: usize = 3000;
const UNEXPLAINED_IGNORE_DOCTEST_INFO: &str = r#"unexplained "```ignore" doctest; try one:
* make the test actually pass, by adding necessary imports and declarations, or
* use "```text", if the code is not Rust code, or
* use "```compile_fail,Ennnn", if the code is expected to fail at compile time, or
* use "```should_panic", if the code is expected to fail at run time, or
* use "```no_run", if the code should type-check but not necessary linkable/runnable, or
* explain it like "```ignore (cannot-test-this-because-xxxx)", if the annotation cannot be avoided.
"#;
const LLVM_UNREACHABLE_INFO: &str = r"\
C++ code used llvm_unreachable, which triggers undefined behavior
when executed when assertions are disabled.
Use llvm::report_fatal_error for increased robustness.";
const DOUBLE_SPACE_AFTER_DOT: &str = r"\
Use a single space after dots in comments.";
const ANNOTATIONS_TO_IGNORE: &[&str] = &[
"// @!has",
"// @has",
"// @matches",
"// CHECK",
"// EMIT_MIR",
"// compile-flags",
"// error-pattern",
"// gdb",
"// lldb",
"// cdb",
"// normalize-stderr-test",
];
const PROBLEMATIC_CONSTS: &[u32] = &[
184594741, 2880289470, 2881141438, 2965027518, 2976579765, 3203381950, 3405691582, 3405697037,
3735927486, 3735932941, 4027431614, 4276992702,
];
const INTERNAL_COMPILER_DOCS_LINE: &str = "#### This error code is internal to the compiler and will not be emitted with normal Rust code.";
#[derive(Clone, Copy, PartialEq)]
#[allow(non_camel_case_types)]
enum LIUState {
EXP_COMMENT_START,
EXP_LINK_LABEL_OR_URL,
EXP_URL,
EXP_END,
}
fn line_is_url(is_error_code: bool, columns: usize, line: &str) -> bool {
if is_error_code {
return line.starts_with('[') && line.contains("]:") && line.contains("http");
}
use self::LIUState::*;
let mut state: LIUState = EXP_COMMENT_START;
let is_url = |w: &str| w.starts_with("http://") || w.starts_with("https://");
for tok in line.split_whitespace() {
match (state, tok) {
(EXP_COMMENT_START, "//") | (EXP_COMMENT_START, "///") | (EXP_COMMENT_START, "//!") => {
state = EXP_LINK_LABEL_OR_URL
}
(EXP_LINK_LABEL_OR_URL, w)
if w.len() >= 4 && w.starts_with('[') && w.ends_with("]:") =>
{
state = EXP_URL
}
(EXP_LINK_LABEL_OR_URL, w) if is_url(w) => state = EXP_END,
(EXP_URL, w) if is_url(w) || w.starts_with("../") => state = EXP_END,
(_, w) if w.len() > columns && is_url(w) => state = EXP_END,
(_, _) => {}
}
}
state == EXP_END
}
fn should_ignore(line: &str) -> bool {
let re = Regex::new("\\s*//(\\[.*\\])?~.*").unwrap();
re.is_match(line) || ANNOTATIONS_TO_IGNORE.iter().any(|a| line.contains(a))
}
fn long_line_is_ok(extension: &str, is_error_code: bool, max_columns: usize, line: &str) -> bool {
match extension {
"ftl" => true,
"md" if !is_error_code => true,
"md" if line == INTERNAL_COMPILER_DOCS_LINE => true,
_ => line_is_url(is_error_code, max_columns, line) || should_ignore(line),
}
}
enum Directive {
Deny,
Ignore(bool),
}
fn contains_ignore_directive(can_contain: bool, contents: &str, check: &str) -> Directive {
if !can_contain {
return Directive::Deny;
}
if contents.contains(&format!("// ignore-tidy-{check}"))
|| contents.contains(&format!("# ignore-tidy-{check}"))
|| contents.contains(&format!("/* ignore-tidy-{check} */"))
{
Directive::Ignore(false)
} else {
Directive::Deny
}
}
macro_rules! suppressible_tidy_err {
($err:ident, $skip:ident, $msg:literal) => {
if let Directive::Deny = $skip {
$err(&format!($msg));
} else {
$skip = Directive::Ignore(true);
}
};
}
pub fn is_in(full_path: &Path, parent_folder_to_find: &str, folder_to_find: &str) -> bool {
if let Some(parent) = full_path.parent() {
if parent.file_name().map_or_else(
|| false,
|f| {
f.to_string_lossy() == folder_to_find
&& parent
.parent()
.and_then(|f| f.file_name())
.map_or_else(|| false, |f| f == parent_folder_to_find)
},
) {
true
} else {
is_in(parent, parent_folder_to_find, folder_to_find)
}
} else {
false
}
}
fn skip_markdown_path(path: &Path) -> bool {
const SKIP_MD: &[&str] = &[
"src/doc/edition-guide",
"src/doc/embedded-book",
"src/doc/nomicon",
"src/doc/reference",
"src/doc/rust-by-example",
"src/doc/rustc-dev-guide",
];
SKIP_MD.iter().any(|p| path.ends_with(p))
}
fn is_unexplained_ignore(extension: &str, line: &str) -> bool {
if !line.ends_with("```ignore") && !line.ends_with("```rust,ignore") {
return false;
}
if extension == "md" && line.trim().starts_with("//") {
return false;
}
true
}
pub fn check(path: &Path, bad: &mut bool) {
fn skip(path: &Path) -> bool {
filter_dirs(path) || skip_markdown_path(path)
}
let problematic_consts_strings: Vec<String> = (PROBLEMATIC_CONSTS.iter().map(u32::to_string))
.chain(PROBLEMATIC_CONSTS.iter().map(|v| format!("{:x}", v)))
.chain(PROBLEMATIC_CONSTS.iter().map(|v| format!("{:X}", v)))
.collect();
let problematic_regex = RegexSet::new(problematic_consts_strings.as_slice()).unwrap();
walk(path, skip, &mut |entry, contents| {
let file = entry.path();
let filename = file.file_name().unwrap().to_string_lossy();
let extensions =
[".rs", ".py", ".js", ".sh", ".c", ".cpp", ".h", ".md", ".css", ".ftl", ".goml"];
if extensions.iter().all(|e| !filename.ends_with(e)) || filename.starts_with(".#") {
return;
}
let is_style_file = filename.ends_with(".css");
let under_rustfmt = filename.ends_with(".rs") &&
!file.ancestors().any(|a| {
(a.ends_with("tests") && a.join("COMPILER_TESTS.md").exists()) ||
a.ends_with("src/doc/book")
});
if is_style_file && !is_in(file, "src", "librustdoc") {
return;
}
if contents.is_empty() {
tidy_error!(bad, "{}: empty file", file.display());
}
let extension = file.extension().unwrap().to_string_lossy();
let is_error_code = extension == "md" && is_in(file, "src", "error_codes");
let is_goml_code = extension == "goml";
let max_columns = if is_error_code {
ERROR_CODE_COLS
} else if is_goml_code {
GOML_COLS
} else {
COLS
};
let can_contain = contents.contains("// ignore-tidy-")
|| contents.contains("# ignore-tidy-")
|| contents.contains("/* ignore-tidy-");
if filename.contains("ignore-tidy") {
return;
}
if is_in(file, "compiler", "rustc_apfloat") {
return;
}
let mut skip_cr = contains_ignore_directive(can_contain, &contents, "cr");
let mut skip_undocumented_unsafe =
contains_ignore_directive(can_contain, &contents, "undocumented-unsafe");
let mut skip_tab = contains_ignore_directive(can_contain, &contents, "tab");
let mut skip_line_length = contains_ignore_directive(can_contain, &contents, "linelength");
let mut skip_file_length = contains_ignore_directive(can_contain, &contents, "filelength");
let mut skip_end_whitespace =
contains_ignore_directive(can_contain, &contents, "end-whitespace");
let mut skip_trailing_newlines =
contains_ignore_directive(can_contain, &contents, "trailing-newlines");
let mut skip_leading_newlines =
contains_ignore_directive(can_contain, &contents, "leading-newlines");
let mut skip_copyright = contains_ignore_directive(can_contain, &contents, "copyright");
let mut skip_dbg = contains_ignore_directive(can_contain, &contents, "dbg");
let mut skip_odd_backticks =
contains_ignore_directive(can_contain, &contents, "odd-backticks");
let mut leading_new_lines = false;
let mut trailing_new_lines = 0;
let mut lines = 0;
let mut last_safety_comment = false;
let mut comment_block: Option<(usize, usize)> = None;
let is_test = file.components().any(|c| c.as_os_str() == "tests");
let any_problematic_line = problematic_regex.is_match(contents);
for (i, line) in contents.split('\n').enumerate() {
if line.is_empty() {
if i == 0 {
leading_new_lines = true;
}
trailing_new_lines += 1;
continue;
} else {
trailing_new_lines = 0;
}
let trimmed = line.trim();
if !trimmed.starts_with("//") {
lines += 1;
}
let mut err = |msg: &str| {
tidy_error!(bad, "{}:{}: {}", file.display(), i + 1, msg);
};
if trimmed.contains("dbg!")
&& !trimmed.starts_with("//")
&& !file.ancestors().any(|a| {
(a.ends_with("tests") && a.join("COMPILER_TESTS.md").exists())
|| a.ends_with("library/alloc/tests")
})
&& filename != "tests.rs"
{
suppressible_tidy_err!(
err,
skip_dbg,
"`dbg!` macro is intended as a debugging tool. It should not be in version control."
)
}
if !under_rustfmt
&& line.chars().count() > max_columns
&& !long_line_is_ok(&extension, is_error_code, max_columns, line)
{
suppressible_tidy_err!(
err,
skip_line_length,
"line longer than {max_columns} chars"
);
}
if !is_style_file && line.contains('\t') {
suppressible_tidy_err!(err, skip_tab, "tab character");
}
if line.ends_with(' ') || line.ends_with('\t') {
suppressible_tidy_err!(err, skip_end_whitespace, "trailing whitespace");
}
if is_style_file && line.starts_with(' ') {
err("CSS files use tabs for indent");
}
if line.contains('\r') {
suppressible_tidy_err!(err, skip_cr, "CR character");
}
if filename != "style.rs" {
if trimmed.contains("TODO") {
err("TODO is deprecated; use FIXME")
}
if trimmed.contains("//") && trimmed.contains(" XXX") {
err("XXX is deprecated; use FIXME")
}
if any_problematic_line {
for s in problematic_consts_strings.iter() {
if trimmed.contains(s) {
err("Don't use magic numbers that spell things (consider 0x12345678)");
}
}
}
}
if trimmed.contains("unsafe {") && !trimmed.starts_with("//") && !last_safety_comment {
if file.components().any(|c| c.as_os_str() == "core") && !is_test {
suppressible_tidy_err!(err, skip_undocumented_unsafe, "undocumented unsafe");
}
}
if trimmed.contains("// SAFETY:") {
last_safety_comment = true;
} else if trimmed.starts_with("//") || trimmed.is_empty() {
} else {
last_safety_comment = false;
}
if (line.starts_with("// Copyright")
|| line.starts_with("# Copyright")
|| line.starts_with("Copyright"))
&& (trimmed.contains("Rust Developers")
|| trimmed.contains("Rust Project Developers"))
{
suppressible_tidy_err!(
err,
skip_copyright,
"copyright notices attributed to the Rust Project Developers are deprecated"
);
}
if is_unexplained_ignore(&extension, line) {
err(UNEXPLAINED_IGNORE_DOCTEST_INFO);
}
if filename.ends_with(".cpp") && line.contains("llvm_unreachable") {
err(LLVM_UNREACHABLE_INFO);
}
let is_compiler = || file.components().any(|c| c.as_os_str() == "compiler");
if is_compiler() {
if line.contains("//")
&& line
.chars()
.collect::<Vec<_>>()
.windows(4)
.any(|cs| matches!(cs, ['.', ' ', ' ', last] if last.is_alphabetic()))
{
err(DOUBLE_SPACE_AFTER_DOT)
}
if filename.ends_with(".ftl") {
let line_backticks = trimmed.chars().filter(|ch| *ch == '`').count();
if line_backticks % 2 == 1 {
suppressible_tidy_err!(err, skip_odd_backticks, "odd number of backticks");
}
} else if trimmed.contains("//") {
let (start_line, mut backtick_count) = comment_block.unwrap_or((i + 1, 0));
let line_backticks = trimmed.chars().filter(|ch| *ch == '`').count();
let comment_text = trimmed.split("//").nth(1).unwrap();
if line_backticks % 2 == 1 {
backtick_count += comment_text.chars().filter(|ch| *ch == '`').count();
}
comment_block = Some((start_line, backtick_count));
} else {
if let Some((start_line, backtick_count)) = comment_block.take() {
if backtick_count % 2 == 1 {
let mut err = |msg: &str| {
tidy_error!(bad, "{}:{start_line}: {msg}", file.display());
};
let block_len = (i + 1) - start_line;
if block_len == 1 {
suppressible_tidy_err!(
err,
skip_odd_backticks,
"comment with odd number of backticks"
);
} else {
suppressible_tidy_err!(
err,
skip_odd_backticks,
"{block_len}-line comment block with odd number of backticks"
);
}
}
}
}
}
}
if leading_new_lines {
let mut err = |_| {
tidy_error!(bad, "{}: leading newline", file.display());
};
suppressible_tidy_err!(err, skip_leading_newlines, "mising leading newline");
}
let mut err = |msg: &str| {
tidy_error!(bad, "{}: {}", file.display(), msg);
};
match trailing_new_lines {
0 => suppressible_tidy_err!(err, skip_trailing_newlines, "missing trailing newline"),
1 => {}
n => suppressible_tidy_err!(
err,
skip_trailing_newlines,
"too many trailing newlines ({n})"
),
};
if lines > LINES {
let mut err = |_| {
tidy_error!(
bad,
"{}: too many lines ({}) (add `// \
ignore-tidy-filelength` to the file to suppress this error)",
file.display(),
lines
);
};
suppressible_tidy_err!(err, skip_file_length, "");
}
if let Directive::Ignore(false) = skip_cr {
tidy_error!(bad, "{}: ignoring CR characters unnecessarily", file.display());
}
if let Directive::Ignore(false) = skip_tab {
tidy_error!(bad, "{}: ignoring tab characters unnecessarily", file.display());
}
if let Directive::Ignore(false) = skip_end_whitespace {
tidy_error!(bad, "{}: ignoring trailing whitespace unnecessarily", file.display());
}
if let Directive::Ignore(false) = skip_trailing_newlines {
tidy_error!(bad, "{}: ignoring trailing newlines unnecessarily", file.display());
}
if let Directive::Ignore(false) = skip_leading_newlines {
tidy_error!(bad, "{}: ignoring leading newlines unnecessarily", file.display());
}
if let Directive::Ignore(false) = skip_copyright {
tidy_error!(bad, "{}: ignoring copyright unnecessarily", file.display());
}
let _unused = skip_line_length;
let _unused = skip_file_length;
})
}