Skip to content

Commit 93237ec

Browse files
cursoragentlovasoa
andcommitted
Fix: Replace unwrap with expect for error handling
Co-authored-by: contact <[email protected]>
1 parent 9ca44d6 commit 93237ec

File tree

5 files changed

+45
-26
lines changed

5 files changed

+45
-26
lines changed

sqlx-core/src/mssql/types/bigdecimal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl Encode<'_, Mssql> for BigDecimal {
4040
fn produces(&self) -> Option<MssqlTypeInfo> {
4141
let mut info = <Self as Type<Mssql>>::type_info();
4242
let (_biging, exponent) = self.as_bigint_and_exponent();
43-
info.0.scale = u8::try_from(exponent).unwrap_or(0);
43+
info.0.scale = u8::try_from(exponent).expect("exponent should fit in u8");
4444
Some(info)
4545
}
4646

sqlx-core/src/mssql/types/chrono.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ where
162162
let mut encoded_offset: [u8; 2] = [0, 0];
163163
LittleEndian::write_i16(
164164
&mut encoded_offset,
165-
i16::try_from(seconds_from_utc / 60).unwrap_or(i16::MAX),
165+
i16::try_from(seconds_from_utc / 60).expect("timezone minutes offset out of range"),
166166
);
167167
buf.extend_from_slice(&encoded_offset);
168168
IsNull::No

sqlx-core/src/sqlite/connection/explain.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,14 @@ pub(super) fn explain(
364364
OP_INIT => {
365365
// start at <p2>
366366
state.visited[state.program_i] = true;
367-
state.program_i = usize::try_from(p2).unwrap_or(0);
367+
state.program_i = usize::try_from(p2).expect("opcode p2 should be non-negative");
368368
continue;
369369
}
370370

371371
OP_GOTO => {
372372
// goto <p2>
373373
state.visited[state.program_i] = true;
374-
state.program_i = usize::try_from(p2).unwrap_or(0);
374+
state.program_i = usize::try_from(p2).expect("opcode p2 should be non-negative");
375375
continue;
376376
}
377377

@@ -388,7 +388,7 @@ pub(super) fn explain(
388388
state.visited[state.program_i] = true;
389389

390390
let mut branch_state = state.clone();
391-
branch_state.program_i = usize::try_from(p2).unwrap_or(0);
391+
branch_state.program_i = usize::try_from(p2).expect("branch p2 should be non-negative");
392392
states.push(branch_state);
393393

394394
state.program_i += 1;
@@ -401,7 +401,7 @@ pub(super) fn explain(
401401
state.r.insert(p1, RegDataType::Int(p3));
402402

403403
if p2 != 0 {
404-
state.program_i = usize::try_from(p2).unwrap_or(0);
404+
state.program_i = usize::try_from(p2).expect("opcode p2 should be non-negative");
405405
} else {
406406
state.program_i += 1;
407407
}
@@ -413,10 +413,10 @@ pub(super) fn explain(
413413
state.visited[state.program_i] = true;
414414
if let Some(RegDataType::Int(yield_i)) = state.r.get(&p1) {
415415
if let Some((_, yield_op, _, yield_p2, _, _)) =
416-
program.get(usize::try_from(*yield_i).unwrap_or(0))
416+
program.get(usize::try_from(*yield_i).expect("yield index should be non-negative"))
417417
{
418418
if OP_YIELD == yield_op.as_str() {
419-
state.program_i = usize::try_from(*yield_p2).unwrap_or(0);
419+
state.program_i = usize::try_from(*yield_p2).expect("yield p2 should be non-negative");
420420
state.r.remove(&p1);
421421
continue;
422422
} else {
@@ -434,7 +434,7 @@ pub(super) fn explain(
434434
// jump to the instruction after the instruction pointed at by register p1
435435
state.visited[state.program_i] = true;
436436
if let Some(RegDataType::Int(return_i)) = state.r.get(&p1) {
437-
state.program_i = usize::try_from(*return_i + 1).unwrap_or(0);
437+
state.program_i = usize::try_from(*return_i + 1).expect("return index should be non-negative");
438438
state.r.remove(&p1);
439439
continue;
440440
} else {
@@ -450,15 +450,15 @@ pub(super) fn explain(
450450

451451
//if yielding to a yield operation, go to the NEXT instruction after that instruction
452452
if program
453-
.get(usize::try_from(*yield_i).unwrap_or(0))
453+
.get(usize::try_from(*yield_i).expect("yield index should be non-negative"))
454454
.map(|(_, yield_op, _, _, _, _)| yield_op.as_str())
455455
== Some(OP_YIELD)
456456
{
457-
state.program_i = usize::try_from(*yield_i + 1).unwrap_or(0);
457+
state.program_i = usize::try_from(*yield_i + 1).expect("yield+1 should be non-negative");
458458
*yield_i = program_i as i64;
459459
continue;
460460
} else {
461-
state.program_i = usize::try_from(*yield_i).unwrap_or(0);
461+
state.program_i = usize::try_from(*yield_i).expect("yield index should be non-negative");
462462
*yield_i = program_i as i64;
463463
continue;
464464
}
@@ -472,15 +472,15 @@ pub(super) fn explain(
472472
state.visited[state.program_i] = true;
473473

474474
let mut branch_state = state.clone();
475-
branch_state.program_i = usize::try_from(p1).unwrap_or(0);
475+
branch_state.program_i = usize::try_from(p1).expect("branch p1 should be non-negative");
476476
states.push(branch_state);
477477

478478
let mut branch_state = state.clone();
479-
branch_state.program_i = usize::try_from(p2).unwrap_or(0);
479+
branch_state.program_i = usize::try_from(p2).expect("branch p2 should be non-negative");
480480
states.push(branch_state);
481481

482482
let mut branch_state = state.clone();
483-
branch_state.program_i = usize::try_from(p3).unwrap_or(0);
483+
branch_state.program_i = usize::try_from(p3).expect("branch p3 should be non-negative");
484484
states.push(branch_state);
485485
}
486486

@@ -515,7 +515,7 @@ pub(super) fn explain(
515515

516516
OP_MAKE_RECORD => {
517517
// p3 = Record([p1 .. p1 + p2])
518-
let mut record = Vec::with_capacity(usize::try_from(p2).unwrap_or(0));
518+
let mut record = Vec::with_capacity(usize::try_from(p2).expect("record len should be non-negative"));
519519
for reg in p1..p1 + p2 {
520520
record.push(
521521
state
@@ -565,7 +565,9 @@ pub(super) fn explain(
565565
//Create a new pointer which is referenced by p1
566566
state.p.insert(
567567
p1,
568-
CursorDataType::from_dense_record(&vec![ColumnType::null(); usize::try_from(p2).unwrap_or(0)]),
568+
CursorDataType::from_dense_record(
569+
&vec![ColumnType::null(); usize::try_from(p2).expect("record len should be non-negative")],
570+
),
569571
);
570572
}
571573

sqlx-core/src/sqlite/statement/handle.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl StatementHandle {
7272
pub(crate) fn column_count(&self) -> usize {
7373
// https://sqlite.org/c3ref/column_count.html
7474
let count: i32 = unsafe { sqlite3_column_count(self.0.as_ptr()) };
75-
usize::try_from(count).unwrap_or(0)
75+
usize::try_from(count).expect("sqlite3_column_count returned a negative value")
7676
}
7777

7878
#[inline]
@@ -81,14 +81,17 @@ impl StatementHandle {
8181
// necessarily this statement.
8282
// https://sqlite.org/c3ref/changes.html
8383
let changes: i32 = unsafe { sqlite3_changes(self.db_handle()) };
84-
u64::try_from(changes).unwrap_or(0)
84+
u64::try_from(changes).expect("sqlite3_changes returned a negative value")
8585
}
8686

8787
#[inline]
8888
pub(crate) fn column_name(&self, index: usize) -> &str {
8989
// https://sqlite.org/c3ref/column_name.html
9090
unsafe {
91-
let name = sqlite3_column_name(self.0.as_ptr(), c_int::try_from(index).unwrap_or(c_int::MAX));
91+
let name = sqlite3_column_name(
92+
self.0.as_ptr(),
93+
c_int::try_from(index).expect("column_name index exceeds c_int")
94+
);
9295
debug_assert!(!name.is_null());
9396

9497
from_utf8_unchecked(CStr::from_ptr(name).to_bytes())
@@ -109,7 +112,10 @@ impl StatementHandle {
109112
#[inline]
110113
pub(crate) fn column_decltype(&self, index: usize) -> Option<SqliteTypeInfo> {
111114
unsafe {
112-
let decl = sqlite3_column_decltype(self.0.as_ptr(), c_int::try_from(index).unwrap_or(c_int::MAX));
115+
let decl = sqlite3_column_decltype(
116+
self.0.as_ptr(),
117+
c_int::try_from(index).expect("column_decltype index exceeds c_int")
118+
);
113119
if decl.is_null() {
114120
// If the Nth column of the result set is an expression or subquery,
115121
// then a NULL pointer is returned.
@@ -132,7 +138,7 @@ impl StatementHandle {
132138
// sqlite3_finalize() or until the statement is automatically reprepared by the
133139
// first call to sqlite3_step() for a particular run or until the same information
134140
// is requested again in a different encoding.
135-
let idx_cint = c_int::try_from(index).unwrap_or(c_int::MAX);
141+
let idx_cint = c_int::try_from(index).expect("index exceeds c_int");
136142
let db_name = sqlite3_column_database_name(self.0.as_ptr(), idx_cint);
137143
let table_name = sqlite3_column_table_name(self.0.as_ptr(), idx_cint);
138144
let origin_name = sqlite3_column_origin_name(self.0.as_ptr(), idx_cint);
@@ -178,7 +184,7 @@ impl StatementHandle {
178184
pub(crate) fn bind_parameter_count(&self) -> usize {
179185
// https://www.sqlite.org/c3ref/bind_parameter_count.html
180186
let count: i32 = unsafe { sqlite3_bind_parameter_count(self.0.as_ptr()) };
181-
usize::try_from(count).unwrap_or(0)
187+
usize::try_from(count).expect("sqlite3_bind_parameter_count returned negative")
182188
}
183189

184190
// Name Of A Host Parameter
@@ -187,7 +193,10 @@ impl StatementHandle {
187193
pub(crate) fn bind_parameter_name(&self, index: usize) -> Option<&str> {
188194
unsafe {
189195
// https://www.sqlite.org/c3ref/bind_parameter_name.html
190-
let name = sqlite3_bind_parameter_name(self.0.as_ptr(), c_int::try_from(index).unwrap_or(c_int::MAX));
196+
let name = sqlite3_bind_parameter_name(
197+
self.0.as_ptr(),
198+
c_int::try_from(index).expect("bind_parameter_name index exceeds c_int")
199+
);
191200
if name.is_null() {
192201
return None;
193202
}
@@ -243,7 +252,12 @@ impl StatementHandle {
243252

244253
#[inline]
245254
pub(crate) fn bind_null(&self, index: usize) -> c_int {
246-
unsafe { sqlite3_bind_null(self.0.as_ptr(), c_int::try_from(index).unwrap_or(c_int::MAX)) }
255+
unsafe {
256+
sqlite3_bind_null(
257+
self.0.as_ptr(),
258+
c_int::try_from(index).expect("bind_null index exceeds c_int"),
259+
)
260+
}
247261
}
248262

249263
// result values from the query

sqlx-core/src/sqlite/statement/unlock_notify.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ pub unsafe fn wait(conn: *mut sqlite3) -> Result<(), SqliteError> {
2727

2828
unsafe extern "C" fn unlock_notify_cb(ptr: *mut *mut c_void, len: c_int) {
2929
let ptr = ptr as *mut &Notify;
30-
let slice = slice::from_raw_parts(ptr, usize::try_from(len).unwrap_or(0));
30+
let slice = slice::from_raw_parts(
31+
ptr,
32+
usize::try_from(len).expect("unlock_notify callback length negative"),
33+
);
3134

3235
for notify in slice {
3336
notify.fire();

0 commit comments

Comments
 (0)