Skip to content

Commit 35d0f07

Browse files
committed
fix(Diagnostic): remove usage of unsafe code from Infallible impl
1 parent a2bea99 commit 35d0f07

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/diagnostic_impls.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,41 @@
22
Default trait implementations for [`Diagnostic`].
33
*/
44

5-
use std::{convert::Infallible, fmt::Display, hint::unreachable_unchecked};
5+
use std::{convert::Infallible, fmt::Display};
66

77
use crate::{Diagnostic, LabeledSpan, Severity, SourceCode};
88

9-
// Since all trait methods for [`Diagnostic`] take a reference to `self`, there must be an instance of `Self`.
10-
// However, since an instance of [`Infallible`] can never be constructed, these methods can never be called.
11-
// Therefore, these methods are unreachable, and can be safely optimized away by the compiler.
12-
#[allow(clippy::undocumented_unsafe_blocks)]
139
impl Diagnostic for Infallible {
1410
fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
15-
unsafe { unreachable_unchecked() }
11+
match *self {}
1612
}
1713

1814
fn severity(&self) -> Option<Severity> {
19-
unsafe { unreachable_unchecked() }
15+
match *self {}
2016
}
2117

2218
fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
23-
unsafe { unreachable_unchecked() }
19+
match *self {}
2420
}
2521

2622
fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
27-
unsafe { unreachable_unchecked() }
23+
match *self {}
2824
}
2925

3026
fn source_code(&self) -> Option<&dyn SourceCode> {
31-
unsafe { unreachable_unchecked() }
27+
match *self {}
3228
}
3329

3430
fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
35-
unsafe { unreachable_unchecked() }
31+
match *self {}
3632
}
3733

3834
fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
39-
unsafe { unreachable_unchecked() }
35+
match *self {}
4036
}
4137

4238
fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
43-
unsafe { unreachable_unchecked() }
39+
match *self {}
4440
}
4541
}
4642

0 commit comments

Comments
 (0)