about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-05-25 12:03:07 +0000
committerbors <bors@rust-lang.org>2024-05-25 12:03:07 +0000
commit5d10538fb4e3294cdb8d7b95fb0b3e5239598e3f (patch)
treedb0e1612b84f27e45f9582e54f209b4725c4974b
parent674c641ecf2f5ac7b915d9e3896708e92c0e7434 (diff)
parent4f98cc6d55aa82612fac031a5828f5fc9deee4bc (diff)
downloadrust-5d10538fb4e3294cdb8d7b95fb0b3e5239598e3f.tar.gz
rust-5d10538fb4e3294cdb8d7b95fb0b3e5239598e3f.zip
Auto merge of #12809 - GuillaumeGomez:missing-backticks-fix, r=y21
Correctly handle closing parens in `missing_backticks` doc lint

Fixes #12795.

changelog: Correctly handle closing parens in `doc_markdown` lint
-rw-r--r--clippy_lints/src/doc/markdown.rs20
-rw-r--r--tests/ui/doc/issue_12795.fixed9
-rw-r--r--tests/ui/doc/issue_12795.rs9
-rw-r--r--tests/ui/doc/issue_12795.stderr48
4 files changed, 86 insertions, 0 deletions
diff --git a/clippy_lints/src/doc/markdown.rs b/clippy_lints/src/doc/markdown.rs
index 8a497b5ab0d..41c0bcd55ad 100644
--- a/clippy_lints/src/doc/markdown.rs
+++ b/clippy_lints/src/doc/markdown.rs
@@ -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(
diff --git a/tests/ui/doc/issue_12795.fixed b/tests/ui/doc/issue_12795.fixed
new file mode 100644
index 00000000000..ade23bf975c
--- /dev/null
+++ b/tests/ui/doc/issue_12795.fixed
@@ -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() {}
diff --git a/tests/ui/doc/issue_12795.rs b/tests/ui/doc/issue_12795.rs
new file mode 100644
index 00000000000..6d94a07e303
--- /dev/null
+++ b/tests/ui/doc/issue_12795.rs
@@ -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() {}
diff --git a/tests/ui/doc/issue_12795.stderr b/tests/ui/doc/issue_12795.stderr
new file mode 100644
index 00000000000..5700145ec8f
--- /dev/null
+++ b/tests/ui/doc/issue_12795.stderr
@@ -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
+