Skip to content

Commit e972486

Browse files
committed
Replace remaining unreachable unwraps with expects
Document the internal invariants behind the remaining non-lock unwrap sites so panic paths explain why they should be unreachable, while keeping the known reachable cases explicit for later handling. Co-Authored-By: HAL 9000
1 parent 8ebaab1 commit e972486

6 files changed

Lines changed: 47 additions & 18 deletions

File tree

build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77

88
fn main() {
99
#[cfg(feature = "uniffi")]
10-
uniffi::generate_scaffolding("bindings/ldk_node.udl").unwrap();
10+
uniffi::generate_scaffolding("bindings/ldk_node.udl")
11+
.expect("the checked-in UniFFI UDL should always generate scaffolding");
1112
}

src/balance.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ impl LightningBalance {
232232
inbound_htlc_rounded_msat,
233233
} => {
234234
// unwrap safety: confirmed_balance_candidate_index is guaranteed to index into balance_candidates
235-
let balance = balance_candidates.get(confirmed_balance_candidate_index).unwrap();
235+
let balance = balance_candidates
236+
.get(confirmed_balance_candidate_index)
237+
.expect("LDK should provide a valid confirmed balance candidate index");
236238

237239
Self::ClaimableOnChannelClose {
238240
channel_id,

src/ffi/types.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,9 @@ uniffi::custom_type!(PaymentHash, String, {
917917
}
918918
},
919919
lower: |obj| {
920-
Sha256::from_slice(&obj.0).unwrap().to_string()
920+
Sha256::from_slice(&obj.0)
921+
.expect("PaymentHash should always contain exactly 32 bytes")
922+
.to_string()
921923
},
922924
});
923925

src/lnurl_auth.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ fn linking_key_path(hashing_key: &[u8; 32], domain_name: &str) -> Vec<ChildNumbe
182182
result
183183
.chunks_exact(4)
184184
.take(4)
185-
.map(|i| u32::from_be_bytes(i.try_into().unwrap()))
185+
.map(|i| {
186+
u32::from_be_bytes(i.try_into().expect("chunks_exact(4) should yield 4-byte slices"))
187+
})
186188
.map(ChildNumber::from)
187189
.collect()
188190
}

src/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ pub struct ChannelDetails {
561561
}
562562

563563
impl From<LdkChannelDetails> for ChannelDetails {
564+
#[allow(clippy::unwrap_used)]
564565
fn from(value: LdkChannelDetails) -> Self {
565566
ChannelDetails {
566567
channel_id: value.channel_id,

src/wallet/ser.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ impl Readable for ChangeSetDeserWrapper<BdkLocalChainChangeSet> {
9494
decode_tlv_stream!(reader, {
9595
(0, blocks, required),
9696
});
97-
Ok(Self(BdkLocalChainChangeSet { blocks: blocks.0.unwrap() }))
97+
Ok(Self(BdkLocalChainChangeSet {
98+
blocks: blocks.0.expect("required blocks TLV field should be present"),
99+
}))
98100
}
99101
}
100102

@@ -141,10 +143,10 @@ impl Readable for ChangeSetDeserWrapper<BdkTxGraphChangeSet<ConfirmationBlockTim
141143
});
142144

143145
Ok(Self(BdkTxGraphChangeSet {
144-
txs: txs.0.unwrap().0,
145-
txouts: txouts.0.unwrap(),
146-
anchors: anchors.0.unwrap().0,
147-
last_seen: last_seen.0.unwrap(),
146+
txs: txs.0.expect("required txs TLV field should be present").0,
147+
txouts: txouts.0.expect("required txouts TLV field should be present"),
148+
anchors: anchors.0.expect("required anchors TLV field should be present").0,
149+
last_seen: last_seen.0.expect("required last_seen TLV field should be present"),
148150
first_seen: first_seen.unwrap_or_default(),
149151
last_evicted: last_evicted.unwrap_or_default(),
150152
}))
@@ -177,7 +179,10 @@ impl Readable for ChangeSetDeserWrapper<BTreeSet<(ConfirmationBlockTime, Txid)>>
177179
(0, time, required),
178180
(2, txid, required),
179181
});
180-
set.insert((time.0.unwrap().0, txid.0.unwrap()));
182+
set.insert((
183+
time.0.expect("required confirmation time TLV field should be present").0,
184+
txid.0.expect("required txid TLV field should be present"),
185+
));
181186
}
182187
Ok(Self(set))
183188
}
@@ -205,7 +210,7 @@ impl Readable for ChangeSetDeserWrapper<BTreeSet<Arc<Transaction>>> {
205210
read_tlv_fields!(reader, {
206211
(0, tx, required),
207212
});
208-
set.insert(Arc::new(tx.0.unwrap()));
213+
set.insert(Arc::new(tx.0.expect("required transaction TLV field should be present")));
209214
}
210215
Ok(Self(set))
211216
}
@@ -232,8 +237,10 @@ impl Readable for ChangeSetDeserWrapper<ConfirmationBlockTime> {
232237
});
233238

234239
Ok(Self(ConfirmationBlockTime {
235-
block_id: block_id.0.unwrap().0,
236-
confirmation_time: confirmation_time.0.unwrap(),
240+
block_id: block_id.0.expect("required block_id TLV field should be present").0,
241+
confirmation_time: confirmation_time
242+
.0
243+
.expect("required confirmation_time TLV field should be present"),
237244
}))
238245
}
239246
}
@@ -257,7 +264,10 @@ impl Readable for ChangeSetDeserWrapper<BlockId> {
257264
(2, hash, required),
258265
});
259266

260-
Ok(Self(BlockId { height: height.0.unwrap(), hash: hash.0.unwrap() }))
267+
Ok(Self(BlockId {
268+
height: height.0.expect("required height TLV field should be present"),
269+
hash: hash.0.expect("required hash TLV field should be present"),
270+
}))
261271
}
262272
}
263273

@@ -285,7 +295,10 @@ impl Readable for ChangeSetDeserWrapper<BdkIndexerChangeSet> {
285295
decode_tlv_stream!(reader, { (0, last_revealed, required) });
286296

287297
Ok(Self(BdkIndexerChangeSet {
288-
last_revealed: last_revealed.0.unwrap().0,
298+
last_revealed: last_revealed
299+
.0
300+
.expect("required last_revealed TLV field should be present")
301+
.0,
289302
spk_cache: Default::default(),
290303
}))
291304
}
@@ -317,7 +330,10 @@ impl Readable for ChangeSetDeserWrapper<BTreeMap<DescriptorId, u32>> {
317330
(0, descriptor_id, required),
318331
(2, last_index, required),
319332
});
320-
set.insert(descriptor_id.0.unwrap().0, last_index.0.unwrap());
333+
set.insert(
334+
descriptor_id.0.expect("required descriptor_id TLV field should be present").0,
335+
last_index.0.expect("required last_index TLV field should be present"),
336+
);
321337
}
322338
Ok(Self(set))
323339
}
@@ -336,7 +352,9 @@ impl Readable for ChangeSetDeserWrapper<DescriptorId> {
336352

337353
decode_tlv_stream!(reader, { (0, hash, required) });
338354

339-
Ok(Self(DescriptorId(hash.0.unwrap().0)))
355+
Ok(Self(DescriptorId(
356+
hash.0.expect("required descriptor hash TLV field should be present").0,
357+
)))
340358
}
341359
}
342360

@@ -351,6 +369,9 @@ impl Readable for ChangeSetDeserWrapper<Sha256Hash> {
351369
use bitcoin::hashes::Hash;
352370

353371
let buf: [u8; 32] = Readable::read(reader)?;
354-
Ok(Self(Sha256Hash::from_slice(&buf[..]).unwrap()))
372+
Ok(Self(
373+
Sha256Hash::from_slice(&buf[..])
374+
.expect("a 32-byte buffer should decode into a sha256 hash"),
375+
))
355376
}
356377
}

0 commit comments

Comments
 (0)