Skip to content

Commit 402ad19

Browse files
committed
Remove unsound use of unreachable_unchecked.
Return an error if Channel::channel() is invalid rather than causing undefined behaviour. The Channel trait is not sealed, so we can't rely on the implementation being correct.
1 parent f913ec5 commit 402ad19

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

nrf-hal-common/src/saadc.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ use crate::pac::{saadc_ns as saadc, SAADC_NS as SAADC};
3232
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
3333
use crate::pac::{saadc, SAADC};
3434

35-
use core::{
36-
hint::unreachable_unchecked,
37-
sync::atomic::{compiler_fence, Ordering::SeqCst},
38-
};
35+
use core::sync::atomic::{compiler_fence, Ordering::SeqCst};
3936

4037
pub use saadc::{
4138
ch::config::{GAIN_A as Gain, REFSEL_A as Reference, RESP_A as Resistor, TACQ_A as Time},
@@ -123,9 +120,9 @@ impl Saadc {
123120
8 => self.0.ch[0].pselp.write(|w| w.pselp().vdd()),
124121
#[cfg(any(feature = "52833", feature = "52840"))]
125122
13 => self.0.ch[0].pselp.write(|w| w.pselp().vddhdiv5()),
126-
// This can never happen the only analog pins have already been defined
127-
// PAY CLOSE ATTENTION TO ANY CHANGES TO THIS IMPL OR THE `channel_mappings!` MACRO
128-
_ => unsafe { unreachable_unchecked() },
123+
// This can never happen with the `Channel` implementations provided, as the only analog
124+
// pins have already been covered.
125+
_ => return Err(()),
129126
}
130127

131128
let mut val: i16 = 0;

0 commit comments

Comments
 (0)