Skip to content

Commit 30d2f5a

Browse files
authored
Rollup merge of #155195 - jieyouxu:tidy-compiler-doc-attr, r=WaffleLapkin
tidy: handle `#[cfg_attr(bootstrap, doc = "...")]` in `compiler/` comments For the unbalanced backtick check that Waffle ran into in #154887. This PR cherry-picks Waffle's tidy patch in that PR (and adds some explaining comments) even though I'd say this is somewhat hacky[^1]. But since the original tidy check implementation is already based on heuristics and so are likewise fuzzy, this is probably fine in practice for most cases. [^1]: There can be false positives/negatives like having both fragments `cfg_attr` and `doc` inside a string literal, but I would expect that to be very very niche, so it's "good enough". (I wanted to write a regression test, but this check needs some restructuring to make it more easy to test that I don't want to bundle in this PR.) r? wafflelapkin (or bootstrap/compiler)
2 parents 4691e61 + 67be8c2 commit 30d2f5a

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)