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`.
21 /// Error code markdown is restricted to 80 columns because they can be
22 /// displayed on the console with --example.
23 const ERROR_CODE_COLS
: usize = 80;
24 const COLS
: usize = 100;
26 const LINES
: usize = 3000;
28 const UNEXPLAINED_IGNORE_DOCTEST_INFO
: &str = r
#"unexplained "```ignore" doctest; try one:
30 * make the test actually pass, by adding necessary imports and declarations, or
31 * use "```text", if the code is not Rust code, or
32 * use "```compile_fail,Ennnn", if the code is expected to fail at compile time, or
33 * use "```should_panic", if the code is expected to fail at run time, or
34 * use "```no_run", if the code should type-check but not necessary linkable/runnable, or
35 * explain it like "```ignore (cannot-test-this-because-xxxx)", if the annotation cannot be avoided.
39 const LLVM_UNREACHABLE_INFO
: &str = r
"\
40 C++ code used llvm_unreachable, which triggers undefined behavior
41 when executed when assertions are disabled.
42 Use llvm::report_fatal_error for increased robustness.";
44 /// Parser states for `line_is_url`.
45 #[derive(Clone, Copy, PartialEq)]
46 #[allow(non_camel_case_types)]
49 EXP_LINK_LABEL_OR_URL
,
54 /// Returns `true` if `line` appears to be a line comment containing an URL,
55 /// possibly with a Markdown link label in front, and nothing else.
56 /// The Markdown link label, if present, may not contain whitespace.
57 /// Lines of this form are allowed to be overlength, because Markdown
58 /// offers no way to split a line in the middle of a URL, and the lengths
59 /// of URLs to external references are beyond our control.
60 fn line_is_url(is_error_code
: bool
, columns
: usize, line
: &str) -> bool
{
61 // more basic check for markdown, to avoid complexity in implementing two state machines
63 return line
.starts_with('
['
) && line
.contains("]:") && line
.contains("http");
66 use self::LIUState
::*;
67 let mut state
: LIUState
= EXP_COMMENT_START
;
68 let is_url
= |w
: &str| w
.starts_with("http://") || w
.starts_with("https://");
70 for tok
in line
.split_whitespace() {
72 (EXP_COMMENT_START
, "//") | (EXP_COMMENT_START
, "///") | (EXP_COMMENT_START
, "//!") => {
73 state
= EXP_LINK_LABEL_OR_URL
76 (EXP_LINK_LABEL_OR_URL
, w
)
77 if w
.len() >= 4 && w
.starts_with('
['
) && w
.ends_with("]:") =>
82 (EXP_LINK_LABEL_OR_URL
, w
) if is_url(w
) => state
= EXP_END
,
84 (EXP_URL
, w
) if is_url(w
) || w
.starts_with("../") => state
= EXP_END
,
86 (_
, w
) if w
.len() > columns
&& is_url(w
) => state
= EXP_END
,
95 /// Returns `true` if `line` is allowed to be longer than the normal limit.
96 /// Currently there is only one exception, for long URLs, but more
97 /// may be added in the future.
98 fn long_line_is_ok(extension
: &str, is_error_code
: bool
, max_columns
: usize, line
: &str) -> bool
{
99 if extension
!= "md" || is_error_code
{
100 if line_is_url(is_error_code
, max_columns
, line
) {
103 } else if extension
== "md" {
104 // non-error code markdown is allowed to be any length
112 /// By default, tidy always warns against style issues.
115 /// `Ignore(false)` means that an `ignore-tidy-*` directive
116 /// has been provided, but is unnecessary. `Ignore(true)`
117 /// means that it is necessary (i.e. a warning would be
118 /// produced if `ignore-tidy-*` was not present).
122 fn contains_ignore_directive(can_contain
: bool
, contents
: &str, check
: &str) -> Directive
{
124 return Directive
::Deny
;
126 // Update `can_contain` when changing this
127 if contents
.contains(&format
!("// ignore-tidy-{}", check
))
128 || contents
.contains(&format
!("# ignore-tidy-{}", check
))
129 || contents
.contains(&format
!("/* ignore-tidy-{} */", check
))
131 Directive
::Ignore(false)
137 macro_rules
! suppressible_tidy_err
{
138 ($err
:ident
, $skip
:ident
, $msg
:expr
) => {
139 if let Directive
::Deny
= $skip
{
142 $skip
= Directive
::Ignore(true);
147 pub fn is_in(full_path
: &Path
, parent_folder_to_find
: &str, folder_to_find
: &str) -> bool
{
148 if let Some(parent
) = full_path
.parent() {
149 if parent
.file_name().map_or_else(
152 f
.to_string_lossy() == folder_to_find
155 .and_then(|f
| f
.file_name())
156 .map_or_else(|| false, |f
| f
== parent_folder_to_find
)
161 is_in(parent
, parent_folder_to_find
, folder_to_find
)
168 fn skip_markdown_path(path
: &Path
) -> bool
{
169 // These aren't ready for tidy.
170 const SKIP_MD
: &[&str] = &[
171 "src/doc/edition-guide",
172 "src/doc/embedded-book",
175 "src/doc/rust-by-example",
176 "src/doc/rustc-dev-guide",
178 SKIP_MD
.iter().any(|p
| path
.ends_with(p
))
181 fn is_unexplained_ignore(extension
: &str, line
: &str) -> bool
{
182 if !line
.ends_with("```ignore") && !line
.ends_with("```rust,ignore") {
185 if extension
== "md" && line
.trim().starts_with("//") {
186 // Markdown examples may include doc comments with ignore inside a
193 pub fn check(path
: &Path
, bad
: &mut bool
) {
194 fn skip(path
: &Path
) -> bool
{
195 super::filter_dirs(path
) || skip_markdown_path(path
)
197 super::walk(path
, &mut skip
, &mut |entry
, contents
| {
198 let file
= entry
.path();
199 let filename
= file
.file_name().unwrap().to_string_lossy();
200 let extensions
= [".rs", ".py", ".js", ".sh", ".c", ".cpp", ".h", ".md", ".css"];
201 if extensions
.iter().all(|e
| !filename
.ends_with(e
)) || filename
.starts_with(".#") {
205 let is_style_file
= filename
.ends_with(".css");
206 let under_rustfmt
= filename
.ends_with(".rs") &&
207 // This list should ideally be sourced from rustfmt.toml but we don't want to add a toml
209 !file
.ancestors().any(|a
| {
210 a
.ends_with("src/test") ||
211 a
.ends_with("src/doc/book")
214 if is_style_file
&& !is_in(file
, "src", "librustdoc") {
215 // We only check CSS files in rustdoc.
219 if contents
.is_empty() {
220 tidy_error
!(bad
, "{}: empty file", file
.display());
223 let extension
= file
.extension().unwrap().to_string_lossy();
224 let is_error_code
= extension
== "md" && is_in(file
, "src", "error_codes");
226 let max_columns
= if is_error_code { ERROR_CODE_COLS }
else { COLS }
;
228 let can_contain
= contents
.contains("// ignore-tidy-")
229 || contents
.contains("# ignore-tidy-")
230 || contents
.contains("/* ignore-tidy-");
231 // Enable testing ICE's that require specific (untidy)
232 // file formats easily eg. `issue-1234-ignore-tidy.rs`
233 if filename
.contains("ignore-tidy") {
236 let mut skip_cr
= contains_ignore_directive(can_contain
, &contents
, "cr");
237 let mut skip_undocumented_unsafe
=
238 contains_ignore_directive(can_contain
, &contents
, "undocumented-unsafe");
239 let mut skip_tab
= contains_ignore_directive(can_contain
, &contents
, "tab");
240 let mut skip_line_length
= contains_ignore_directive(can_contain
, &contents
, "linelength");
241 let mut skip_file_length
= contains_ignore_directive(can_contain
, &contents
, "filelength");
242 let mut skip_end_whitespace
=
243 contains_ignore_directive(can_contain
, &contents
, "end-whitespace");
244 let mut skip_trailing_newlines
=
245 contains_ignore_directive(can_contain
, &contents
, "trailing-newlines");
246 let mut skip_copyright
= contains_ignore_directive(can_contain
, &contents
, "copyright");
247 let mut leading_new_lines
= false;
248 let mut trailing_new_lines
= 0;
250 let mut last_safety_comment
= false;
251 for (i
, line
) in contents
.split('
\n'
).enumerate() {
252 let mut err
= |msg
: &str| {
253 tidy_error
!(bad
, "{}:{}: {}", file
.display(), i
+ 1, msg
);
256 && line
.chars().count() > max_columns
257 && !long_line_is_ok(&extension
, is_error_code
, max_columns
, line
)
259 suppressible_tidy_err
!(
262 &format
!("line longer than {} chars", max_columns
)
265 if !is_style_file
&& line
.contains('
\t'
) {
266 suppressible_tidy_err
!(err
, skip_tab
, "tab character");
268 if line
.ends_with(' '
) || line
.ends_with('
\t'
) {
269 suppressible_tidy_err
!(err
, skip_end_whitespace
, "trailing whitespace");
271 if is_style_file
&& line
.starts_with(' '
) {
272 err("CSS files use tabs for indent");
274 if line
.contains('
\r'
) {
275 suppressible_tidy_err
!(err
, skip_cr
, "CR character");
277 if filename
!= "style.rs" {
278 if line
.contains("TODO") {
279 err("TODO is deprecated; use FIXME")
281 if line
.contains("//") && line
.contains(" XXX") {
282 err("XXX is deprecated; use FIXME")
285 let is_test
= || file
.components().any(|c
| c
.as_os_str() == "tests");
286 // for now we just check libcore
287 if line
.contains("unsafe {") && !line
.trim().starts_with("//") && !last_safety_comment
{
288 if file
.components().any(|c
| c
.as_os_str() == "core") && !is_test() {
289 suppressible_tidy_err
!(err
, skip_undocumented_unsafe
, "undocumented unsafe");
292 if line
.contains("// SAFETY:") {
293 last_safety_comment
= true;
294 } else if line
.trim().starts_with("//") || line
.trim().is_empty() {
295 // keep previous value
297 last_safety_comment
= false;
299 if (line
.starts_with("// Copyright")
300 || line
.starts_with("# Copyright")
301 || line
.starts_with("Copyright"))
302 && (line
.contains("Rust Developers") || line
.contains("Rust Project Developers"))
304 suppressible_tidy_err
!(
307 "copyright notices attributed to the Rust Project Developers are deprecated"
310 if is_unexplained_ignore(&extension
, line
) {
311 err(UNEXPLAINED_IGNORE_DOCTEST_INFO
);
313 if filename
.ends_with(".cpp") && line
.contains("llvm_unreachable") {
314 err(LLVM_UNREACHABLE_INFO
);
318 leading_new_lines
= true;
320 trailing_new_lines
+= 1;
322 trailing_new_lines
= 0;
326 if leading_new_lines
{
327 tidy_error
!(bad
, "{}: leading newline", file
.display());
329 let mut err
= |msg
: &str| {
330 tidy_error
!(bad
, "{}: {}", file
.display(), msg
);
332 match trailing_new_lines
{
333 0 => suppressible_tidy_err
!(err
, skip_trailing_newlines
, "missing trailing newline"),
335 n
=> suppressible_tidy_err
!(
337 skip_trailing_newlines
,
338 &format
!("too many trailing newlines ({})", n
)
345 "{}: too many lines ({}) (add `// \
346 ignore-tidy-filelength` to the file to suppress this error)",
351 suppressible_tidy_err
!(err
, skip_file_length
, "");
354 if let Directive
::Ignore(false) = skip_cr
{
355 tidy_error
!(bad
, "{}: ignoring CR characters unnecessarily", file
.display());
357 if let Directive
::Ignore(false) = skip_tab
{
358 tidy_error
!(bad
, "{}: ignoring tab characters unnecessarily", file
.display());
360 if let Directive
::Ignore(false) = skip_line_length
{
361 tidy_error
!(bad
, "{}: ignoring line length unnecessarily", file
.display());
363 if let Directive
::Ignore(false) = skip_file_length
{
364 tidy_error
!(bad
, "{}: ignoring file length unnecessarily", file
.display());
366 if let Directive
::Ignore(false) = skip_end_whitespace
{
367 tidy_error
!(bad
, "{}: ignoring trailing whitespace unnecessarily", file
.display());
369 if let Directive
::Ignore(false) = skip_trailing_newlines
{
370 tidy_error
!(bad
, "{}: ignoring trailing newlines unnecessarily", file
.display());
372 if let Directive
::Ignore(false) = skip_copyright
{
373 tidy_error
!(bad
, "{}: ignoring copyright unnecessarily", file
.display());