Skip to content

Commit 67be8c2

Browse files
tidy: handle #[cfg_attr(bootstrap, doc = "...")] in compiler/ comments
For the unbalanced backtick check. This fix is arguably a hack, however the original tidy check implementation is already based on heuristics and so are likewise fuzzy. This is probably fine in practice. Co-authored-by: Waffle Lapkin <waffle.lapkin@gmail.com>
1 parent 540f43a commit 67be8c2

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

src/tools/tidy/src/style.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,11 +602,34 @@ pub fn check(path: &Path, tidy_ctx: TidyCtx) {
602602
err(DOUBLE_SPACE_AFTER_DOT)
603603
}
604604

605-
if trimmed.contains("//") {
605+
// Heuristics for matching unbalanced backticks by trying to find comments and
606+
// comment blocks. Technically, this can have false negatives (or false positives),
607+
// but as a heuristic this is fine.
608+
let likely_comment = |trimmed: &str| {
609+
// Line comments, doc comments
610+
trimmed.contains("//")
611+
// Also account for `#[cfg_attr(bootstrap, doc = "")]` cases.
612+
|| (trimmed.contains("cfg_attr") && trimmed.contains("doc"))
613+
};
614+
615+
if likely_comment(trimmed) {
606616
let (start_line, mut backtick_count) = comment_block.unwrap_or((i + 1, 0));
607617
let line_backticks = trimmed.chars().filter(|ch| *ch == '`').count();
608-
let comment_text = trimmed.split("//").nth(1).unwrap();
609-
// This check ensures that we don't lint for code that has `//` in a string literal
618+
619+
// Try to split `//`-like comments or `#[cfg_attr(bootstrap), doc = ""]`-like
620+
// doc attributes. Fuzzy, but probably good enough.
621+
let comment_text = match trimmed.split("//").nth(1) {
622+
Some(text) => text,
623+
None => {
624+
// Fallback to try look for RHS of doc attr bits.
625+
let (_doc, rest) =
626+
trimmed.split_once("doc").expect("failed to find `doc` attribute");
627+
rest
628+
}
629+
};
630+
631+
// If backticks on a given comment line is not balanced, add to backtick count.
632+
// This is to account for wrapped backticks and code blocks.
610633
if line_backticks % 2 == 1 {
611634
backtick_count += comment_text.chars().filter(|ch| *ch == '`').count();
612635
}

0 commit comments

Comments
 (0)