Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle closing parens in missing_backticks doc lint #12809

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions clippy_lints/src/doc/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/doc/issue_12795.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![warn(clippy::doc_markdown)]

//! A comment with `a_b(x)` and `a_c` in it and (`a_b((c))` ) too and (maybe `a_b((c))`)
//~^ ERROR: item in documentation is missing backticks
//~| ERROR: item in documentation is missing backticks
//~| ERROR: item in documentation is missing backticks
//~| ERROR: item in documentation is missing backticks

pub fn main() {}
9 changes: 9 additions & 0 deletions tests/ui/doc/issue_12795.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![warn(clippy::doc_markdown)]

//! A comment with a_b(x) and a_c in it and (a_b((c)) ) too and (maybe a_b((c)))
//~^ ERROR: item in documentation is missing backticks
//~| ERROR: item in documentation is missing backticks
//~| ERROR: item in documentation is missing backticks
//~| ERROR: item in documentation is missing backticks

pub fn main() {}
48 changes: 48 additions & 0 deletions tests/ui/doc/issue_12795.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
error: item in documentation is missing backticks
--> tests/ui/doc/issue_12795.rs:3:20
|
LL | //! A comment with a_b(x) and a_c in it and (a_b((c)) ) too and (maybe a_b((c)))
| ^^^^^^
|
= note: `-D clippy::doc-markdown` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::doc_markdown)]`
help: try
|
LL | //! A comment with `a_b(x)` and a_c in it and (a_b((c)) ) too and (maybe a_b((c)))
| ~~~~~~~~

error: item in documentation is missing backticks
--> tests/ui/doc/issue_12795.rs:3:31
|
LL | //! A comment with a_b(x) and a_c in it and (a_b((c)) ) too and (maybe a_b((c)))
| ^^^
|
help: try
|
LL | //! A comment with a_b(x) and `a_c` in it and (a_b((c)) ) too and (maybe a_b((c)))
| ~~~~~

error: item in documentation is missing backticks
--> tests/ui/doc/issue_12795.rs:3:46
|
LL | //! A comment with a_b(x) and a_c in it and (a_b((c)) ) too and (maybe a_b((c)))
| ^^^^^^^^
|
help: try
|
LL | //! A comment with a_b(x) and a_c in it and (`a_b((c))` ) too and (maybe a_b((c)))
| ~~~~~~~~~~

error: item in documentation is missing backticks
--> tests/ui/doc/issue_12795.rs:3:72
|
LL | //! A comment with a_b(x) and a_c in it and (a_b((c)) ) too and (maybe a_b((c)))
| ^^^^^^^^
|
help: try
|
LL | //! A comment with a_b(x) and a_c in it and (a_b((c)) ) too and (maybe `a_b((c))`)
| ~~~~~~~~~~

error: aborting due to 4 previous errors