about summary refs log tree commit diff
diff options
context:
space:
mode:
authordswij <dharmasw@outlook.com>2025-06-14 15:58:39 +0000
committerGitHub <noreply@github.com>2025-06-14 15:58:39 +0000
commit086a7c978063e08af2e9e6899616880986c5de68 (patch)
treed105c65975441f3b3f357bd6dd142e5d5ece71b7
parent506411d9d1c45fc65a87a7bd2d5edebf249234a0 (diff)
parent7d708a45afad8a692ac6b57a3d332c07f81c2c57 (diff)
downloadrust-086a7c978063e08af2e9e6899616880986c5de68.tar.gz
rust-086a7c978063e08af2e9e6899616880986c5de68.zip
Fix `needless_doctest_main` panic when doctest is invalid (#15052)
Closes rust-lang/rust-clippy#8244
Closes rust-lang/rust-clippy#15041

This feels like a bug with the compiler, because the panic happens when
`Diag` is getting unwinded. However, `drop()` is already called in
`.cancel()` so this should not happen.

In this PR, I find a workaround to just call `emit()`, since the
`DiagCtxt` here is just a `io::sink`, nothing will happen and the panic
just goes away.

changelog: [`needless_doctest_main`] fix panic when doctest is invalid
-rw-r--r--clippy_lints/src/doc/needless_doctest_main.rs5
-rw-r--r--tests/ui/doc/needless_doctest_main.rs16
2 files changed, 20 insertions, 1 deletions
diff --git a/clippy_lints/src/doc/needless_doctest_main.rs b/clippy_lints/src/doc/needless_doctest_main.rs
index ec4538039a9..587ea630d95 100644
--- a/clippy_lints/src/doc/needless_doctest_main.rs
+++ b/clippy_lints/src/doc/needless_doctest_main.rs
@@ -105,7 +105,10 @@ pub fn check(
                         },
                         Ok(None) => break,
                         Err(e) => {
-                            e.cancel();
+                            // See issue #15041. When calling `.cancel()` on the `Diag`, Clippy will unexpectedly panic
+                            // when the `Diag` is unwinded. Meanwhile, we can just call `.emit()`, since the `DiagCtxt`
+                            // is just a sink, nothing will be printed.
+                            e.emit();
                             return (false, test_attr_spans);
                         },
                     }
diff --git a/tests/ui/doc/needless_doctest_main.rs b/tests/ui/doc/needless_doctest_main.rs
index 633a435ca5e..21396cbc8d3 100644
--- a/tests/ui/doc/needless_doctest_main.rs
+++ b/tests/ui/doc/needless_doctest_main.rs
@@ -20,3 +20,19 @@
 fn foo() {}
 
 fn main() {}
+
+fn issue8244() -> Result<(), ()> {
+    //! ```compile_fail
+    //! fn test() -> Result< {}
+    //! ```
+    Ok(())
+}
+
+/// # Examples
+///
+/// ```
+/// use std::error::Error;
+/// fn main() -> Result<(), Box<dyn Error>/* > */ {
+/// }
+/// ```
+fn issue15041() {}