Skip to content

Commit 086a7c9

Browse files
authored
Fix needless_doctest_main panic when doctest is invalid (#15052)
Closes #8244 Closes #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
2 parents 506411d + 7d708a4 commit 086a7c9

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

clippy_lints/src/doc/needless_doctest_main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ pub fn check(
105105
},
106106
Ok(None) => break,
107107
Err(e) => {
108-
e.cancel();
108+
// See issue #15041. When calling `.cancel()` on the `Diag`, Clippy will unexpectedly panic
109+
// when the `Diag` is unwinded. Meanwhile, we can just call `.emit()`, since the `DiagCtxt`
110+
// is just a sink, nothing will be printed.
111+
e.emit();
109112
return (false, test_attr_spans);
110113
},
111114
}

tests/ui/doc/needless_doctest_main.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,19 @@
2020
fn foo() {}
2121

2222
fn main() {}
23+
24+
fn issue8244() -> Result<(), ()> {
25+
//! ```compile_fail
26+
//! fn test() -> Result< {}
27+
//! ```
28+
Ok(())
29+
}
30+
31+
/// # Examples
32+
///
33+
/// ```
34+
/// use std::error::Error;
35+
/// fn main() -> Result<(), Box<dyn Error>/* > */ {
36+
/// }
37+
/// ```
38+
fn issue15041() {}

0 commit comments

Comments
 (0)