Correctly handle closing parens in missing_backticks doc lint

This commit is contained in:
Guillaume Gomez 2024-05-16 16:24:58 +02:00
parent 0b1bf37722
commit 4f98cc6d55
4 changed files with 86 additions and 0 deletions

View file

@ -30,6 +30,7 @@ pub fn check(
word = tmp_word;
}
let original_len = word.len();
word = word.trim_start_matches(trim_pattern);
// Remove leading or trailing single `:` which may be part of a sentence.
@ -44,6 +45,25 @@ pub fn check(
continue;
}
// Ensure that all reachable matching closing parens are included as well.
let size_diff = original_len - word.len();
let mut open_parens = 0;
let mut close_parens = 0;
for c in word.chars() {
if c == '(' {
open_parens += 1;
} else if c == ')' {
close_parens += 1;
}
}
while close_parens < open_parens
&& let Some(tmp_word) = orig_word.get(size_diff..=(word.len() + size_diff))
&& tmp_word.ends_with(')')
{
word = tmp_word;
close_parens += 1;
}
// Adjust for the current word
let offset = word.as_ptr() as usize - text.as_ptr() as usize;
let span = Span::new(