Skip to content

Commit 706523d

Browse files
committed
sqlite/mysql: fix clippy warnings (needless borrows, ptr_arg, casts, loops, conversions)
1 parent 3dbabb1 commit 706523d

File tree

19 files changed

+64
-42
lines changed

19 files changed

+64
-42
lines changed

sqlx-core/src/mysql/connection/auth.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn scramble_sha1(password: &str, nonce: &Chain<Bytes, Bytes>) -> Vec<u8> {
7676

7777
let mut pw_hash = ctx.finalize_reset();
7878

79-
ctx.update(&pw_hash);
79+
ctx.update(pw_hash);
8080

8181
let pw_hash_hash = ctx.finalize_reset();
8282

@@ -100,7 +100,7 @@ fn scramble_sha256(password: &str, nonce: &Chain<Bytes, Bytes>) -> Vec<u8> {
100100

101101
let mut pw_hash = ctx.finalize_reset();
102102

103-
ctx.update(&pw_hash);
103+
ctx.update(pw_hash);
104104

105105
let pw_hash_hash = ctx.finalize_reset();
106106

sqlx-core/src/mysql/connection/executor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use futures_util::{pin_mut, TryStreamExt};
2525
use std::{borrow::Cow, sync::Arc};
2626

2727
impl MySqlConnection {
28-
async fn get_or_prepare<'c>(
28+
async fn get_or_prepare(
2929
&mut self,
3030
sql: &str,
3131
persistent: bool,
@@ -155,7 +155,7 @@ impl MySqlConnection {
155155
// otherwise, this first packet is the start of the result-set metadata,
156156
*self.stream.waiting.front_mut().unwrap() = Waiting::Row;
157157

158-
let num_columns = packet.get_uint_lenenc() as usize; // column count
158+
let num_columns = usize::try_from(packet.get_uint_lenenc()).expect("column count should fit in usize"); // column count
159159

160160
if needs_metadata {
161161
column_names = Arc::new(recv_result_metadata(&mut self.stream, num_columns, Arc::make_mut(&mut columns)).await?);
@@ -289,7 +289,7 @@ impl<'c> Executor<'c> for &'c mut MySqlConnection {
289289

290290
let (_, metadata) = self.get_or_prepare(sql, false).await?;
291291

292-
let columns = (&*metadata.columns).clone();
292+
let columns = (*metadata.columns).clone();
293293

294294
let nullable = columns
295295
.iter()
@@ -335,7 +335,7 @@ fn recv_next_result_column(def: &ColumnDefinition, ordinal: usize) -> Result<MyS
335335
(name, _) => UStr::new(name),
336336
};
337337

338-
let type_info = MySqlTypeInfo::from_column(&def);
338+
let type_info = MySqlTypeInfo::from_column(def);
339339

340340
Ok(MySqlColumn {
341341
name,

sqlx-core/src/mysql/io/buf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ impl MySqlBufExt for Bytes {
3131

3232
fn get_str_lenenc(&mut self) -> Result<String, Error> {
3333
let size = self.get_uint_lenenc();
34-
self.get_str(size as usize)
34+
self.get_str(usize::try_from(size).expect("string length should fit in usize"))
3535
}
3636

3737
fn get_bytes_lenenc(&mut self) -> Bytes {
3838
let size = self.get_uint_lenenc();
39-
self.split_to(size as usize)
39+
self.split_to(usize::try_from(size).expect("split size should fit in usize"))
4040
}
4141
}

sqlx-core/src/mysql/io/buf_mut.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ impl MySqlBufMutExt for Vec<u8> {
1414
// https://mariadb.com/kb/en/library/protocol-data-types/#length-encoded-integers
1515

1616
if v < 251 {
17-
self.push(v as u8);
17+
self.push(u8::try_from(v).expect("value < 251 should fit in u8"));
1818
} else if v < 0x1_00_00 {
1919
self.push(0xfc);
20-
self.extend(&(v as u16).to_le_bytes());
20+
self.extend(&u16::try_from(v).expect("value < 0x1_00_00").to_le_bytes());
2121
} else if v < 0x1_00_00_00 {
2222
self.push(0xfd);
23-
self.extend(&(v as u32).to_le_bytes()[..3]);
23+
self.extend(
24+
&u32::try_from(v)
25+
.expect("value < 0x1_00_00_00")
26+
.to_le_bytes()[..3],
27+
);
2428
} else {
2529
self.push(0xfe);
2630
self.extend(&v.to_le_bytes());

sqlx-core/src/mysql/migrate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@ CREATE TABLE IF NOT EXISTS _sqlx_migrations (
192192
.map_err(MigrateError::AccessMigrationMetadata)?;
193193

194194
if let Some(checksum) = checksum {
195-
return if checksum == &*migration.checksum {
195+
if checksum == &*migration.checksum {
196196
Ok(())
197197
} else {
198198
Err(MigrateError::VersionMismatch(migration.version))
199-
};
199+
}
200200
} else {
201201
Err(MigrateError::VersionMissing(migration.version))
202202
}
@@ -264,7 +264,7 @@ CREATE TABLE IF NOT EXISTS _sqlx_migrations (
264264
WHERE version = ?
265265
"#,
266266
)
267-
.bind(elapsed.as_nanos() as i64)
267+
.bind(i64::try_from(elapsed.as_nanos()).expect("elapsed nanos should fit in i64"))
268268
.bind(migration.version)
269269
.execute(self)
270270
.await?;

sqlx-core/src/mysql/protocol/connect/handshake.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ impl Decode<'_> for Handshake {
6161
}
6262

6363
let auth_plugin_data_2 = if capabilities.contains(Capabilities::SECURE_CONNECTION) {
64-
let len = core::cmp::max((auth_plugin_data_len as isize) - 9, 12) as usize;
64+
let len = usize::try_from(core::cmp::max((auth_plugin_data_len as isize) - 9, 12))
65+
.expect("auth plugin data length should be non-negative");
6566
let v = buf.get_bytes(len);
6667
buf.advance(1); // NUL-terminator
6768

sqlx-core/src/mysql/protocol/connect/handshake_response.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl Encode<'_, Capabilities> for HandshakeResponse<'_> {
4848
} else if capabilities.contains(Capabilities::SECURE_CONNECTION) {
4949
let response = self.auth_response.unwrap_or_default();
5050

51-
buf.push(response.len() as u8);
51+
buf.push(u8::try_from(response.len()).expect("auth response length should fit in u8"));
5252
buf.extend(response);
5353
} else {
5454
buf.push(0);

sqlx-core/src/mysql/protocol/connect/ssl_request.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ pub struct SslRequest {
1212

1313
impl Encode<'_, Capabilities> for SslRequest {
1414
fn encode_with(&self, buf: &mut Vec<u8>, capabilities: Capabilities) {
15-
buf.extend(&(capabilities.bits() as u32).to_le_bytes());
15+
buf.extend(
16+
&u32::try_from(capabilities.bits())
17+
.expect("capabilities bits should fit in u32")
18+
.to_le_bytes(),
19+
);
1620
buf.extend(&self.max_packet_size.to_le_bytes());
1721
buf.push(self.collation);
1822

@@ -24,7 +28,11 @@ impl Encode<'_, Capabilities> for SslRequest {
2428
buf.extend(&[0_u8; 4]);
2529
} else {
2630
// extended client capabilities (MariaDB-specified): int<4>
27-
buf.extend(&((capabilities.bits() >> 32) as u32).to_le_bytes());
31+
buf.extend(
32+
&u32::try_from(capabilities.bits() >> 32)
33+
.expect("capabilities hi bits should fit in u32")
34+
.to_le_bytes(),
35+
);
2836
}
2937
}
3038
}

sqlx-core/src/mysql/protocol/packet.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ where
3434
// FIXME: Support larger packets
3535
assert!(len < 0xFF_FF_FF);
3636

37-
header[..4].copy_from_slice(&(len as u32).to_le_bytes());
37+
header[..4].copy_from_slice(
38+
&u32::try_from(len)
39+
.expect("packet length should fit in u32")
40+
.to_le_bytes(),
41+
);
3842
header[3] = *sequence_id;
3943

4044
*sequence_id = sequence_id.wrapping_add(1);

sqlx-core/src/mysql/protocol/row.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ impl Row {
1212
pub(crate) fn get(&self, index: usize) -> Option<&[u8]> {
1313
self.values[index]
1414
.as_ref()
15-
.map(|col| &self.storage[(col.start as usize)..(col.end as usize)])
15+
.map(|col| &self.storage[(col.start)..(col.end)])
1616
}
1717
}

0 commit comments

Comments
 (0)