Skip to content

Commit

Permalink
tidy: Add ignore-rules for the line length check
Browse files Browse the repository at this point in the history
This is step 1 towards fixing #77548.

This commit includes the tidy change from #77675.
The "ignoring file length unnecessarily" check is temporarily
disabled to simplify landing the ignore-rules.
That check will be re-enabled in a follow-up PR.
  • Loading branch information
Anthuang authored and sjakobi committed Apr 3, 2021
1 parent cb17136 commit c35a36f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3650,6 +3650,8 @@ impl<'test> TestCx<'test> {

// Remove test annotations like `//~ ERROR text` from the output,
// since they duplicate actual errors and make the output hard to read.
// This mirrors the regex in src/tools/tidy/src/style.rs, please update
// both if either are changed.
normalized =
Regex::new("\\s*//(\\[.*\\])?~.*").unwrap().replace_all(&normalized, "").into_owned();

Expand Down
36 changes: 30 additions & 6 deletions src/tools/tidy/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//! A number of these checks can be opted-out of with various directives of the form:
//! `// ignore-tidy-CHECK-NAME`.

use regex::Regex;
use std::path::Path;

/// Error code markdown is restricted to 80 columns because they can be
Expand All @@ -41,6 +42,19 @@ C++ code used llvm_unreachable, which triggers undefined behavior
when executed when assertions are disabled.
Use llvm::report_fatal_error for increased robustness.";

const ANNOTATIONS_TO_IGNORE: &[&str] = &[
"// @!has",
"// @has",
"// @matches",
"// CHECK",
"// EMIT_MIR",
"// compile-flags",
"// error-pattern",
"// gdb",
"// lldb",
"// normalize-stderr-test",
];

/// Parser states for `line_is_url`.
#[derive(Clone, Copy, PartialEq)]
#[allow(non_camel_case_types)]
Expand Down Expand Up @@ -92,12 +106,20 @@ fn line_is_url(is_error_code: bool, columns: usize, line: &str) -> bool {
state == EXP_END
}

/// Returns `true` if `line` can be ignored. This is the case when it contains
/// an annotation that is explicitly ignored.
fn should_ignore(line: &str) -> bool {
// Matches test annotations like `//~ ERROR text`.
// This mirrors the regex in src/tools/compiletest/src/runtest.rs, please
// update both if either are changed.
let re = Regex::new("\\s*//(\\[.*\\])?~.*").unwrap();
re.is_match(line) || ANNOTATIONS_TO_IGNORE.iter().any(|a| line.contains(a))
}

/// Returns `true` if `line` is allowed to be longer than the normal limit.
/// Currently there is only one exception, for long URLs, but more
/// may be added in the future.
fn long_line_is_ok(extension: &str, is_error_code: bool, max_columns: usize, line: &str) -> bool {
if extension != "md" || is_error_code {
if line_is_url(is_error_code, max_columns, line) {
if line_is_url(is_error_code, max_columns, line) || should_ignore(line) {
return true;
}
} else if extension == "md" {
Expand Down Expand Up @@ -357,9 +379,11 @@ pub fn check(path: &Path, bad: &mut bool) {
if let Directive::Ignore(false) = skip_tab {
tidy_error!(bad, "{}: ignoring tab characters unnecessarily", file.display());
}
if let Directive::Ignore(false) = skip_line_length {
tidy_error!(bad, "{}: ignoring line length unnecessarily", file.display());
}
// FIXME: Temporarily disabled to simplify landing the ignore-rules for the line
// length check (https://github.com/rust-lang/rust/issues/77548):
//if let Directive::Ignore(false) = skip_line_length {
// tidy_error!(bad, "{}: ignoring line length unnecessarily", file.display());
//}
if let Directive::Ignore(false) = skip_file_length {
tidy_error!(bad, "{}: ignoring file length unnecessarily", file.display());
}
Expand Down

0 comments on commit c35a36f

Please sign in to comment.