Skip to content

Commit c0686ef

Browse files
cursoragentlovasoa
andcommitted
Refactor: Improve type conversions and clippy lints
This commit makes several improvements including: - Using `try_into` for safer type conversions from `i32` to `usize` and `c_int`. - Replacing `&*` with direct dereferencing for string slices. - Adding a clippy workflow to catch potential issues. - Minor refactoring in various modules for clarity and consistency. Co-authored-by: contact <[email protected]>
1 parent ef520b9 commit c0686ef

File tree

22 files changed

+153
-55
lines changed

22 files changed

+153
-55
lines changed

.github/workflows/clippy.yml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Clippy
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
permissions:
8+
contents: read
9+
checks: write
10+
11+
env:
12+
CARGO_TERM_COLOR: always
13+
14+
jobs:
15+
clippy-rt:
16+
name: clippy (sqlx-rt-oldapi) • ${{ matrix.rt }}
17+
runs-on: ubuntu-latest
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
rt:
22+
- runtime-tokio-native-tls
23+
- runtime-tokio-rustls
24+
- runtime-async-std-native-tls
25+
- runtime-async-std-rustls
26+
steps:
27+
- uses: actions/checkout@v4
28+
- name: Install system deps
29+
run: sudo apt-get update -y && sudo apt-get install -y pkg-config libssl-dev
30+
- uses: dtolnay/rust-toolchain@nightly
31+
with:
32+
components: clippy
33+
- uses: Swatinem/rust-cache@v2
34+
with:
35+
cache-all-crates: true
36+
- name: Run clippy (rt)
37+
run: |
38+
cargo clippy -p sqlx-rt-oldapi --no-default-features --features "${{ matrix.rt }}" --all-targets -- -D warnings
39+
40+
clippy-core:
41+
name: clippy (sqlx-core-oldapi) • ${{ matrix.db }} • ${{ matrix.rt }}
42+
runs-on: ubuntu-latest
43+
strategy:
44+
fail-fast: false
45+
matrix:
46+
db: [postgres, mysql, sqlite, mssql, any]
47+
rt: [runtime-tokio-rustls]
48+
steps:
49+
- uses: actions/checkout@v4
50+
- name: Install system deps
51+
run: sudo apt-get update -y && sudo apt-get install -y pkg-config libssl-dev
52+
- uses: dtolnay/rust-toolchain@nightly
53+
with:
54+
components: clippy
55+
- uses: Swatinem/rust-cache@v2
56+
with:
57+
cache-all-crates: true
58+
- name: Run clippy (core)
59+
run: |
60+
cargo clippy -p sqlx-core-oldapi --no-default-features --features "${{ matrix.rt }},${{ matrix.db }}" --all-targets -- -D warnings
61+
62+
clippy-root:
63+
name: clippy (sqlx-oldapi) • ${{ matrix.combo }}
64+
runs-on: ubuntu-latest
65+
strategy:
66+
fail-fast: false
67+
matrix:
68+
combo:
69+
- "runtime-tokio-rustls,postgres,macros"
70+
- "runtime-tokio-rustls,sqlite,macros"
71+
- "runtime-tokio-rustls,mysql,macros"
72+
- "runtime-async-std-rustls,postgres,macros"
73+
steps:
74+
- uses: actions/checkout@v4
75+
- name: Install system deps
76+
run: sudo apt-get update -y && sudo apt-get install -y pkg-config libssl-dev
77+
- uses: dtolnay/rust-toolchain@nightly
78+
with:
79+
components: clippy
80+
- uses: Swatinem/rust-cache@v2
81+
with:
82+
cache-all-crates: true
83+
- name: Run clippy (root)
84+
run: |
85+
cargo clippy -p sqlx-oldapi --no-default-features --features "${{ matrix.combo }}" --all-targets -- -D warnings
86+

sqlx-core/src/any/statement.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<'q> Statement<'q> for AnyStatement<'q> {
3333

3434
fn parameters(&self) -> Option<Either<&[AnyTypeInfo], usize>> {
3535
match &self.parameters {
36-
Some(Either::Left(types)) => Some(Either::Left(&types)),
36+
Some(Either::Left(types)) => Some(Either::Left(types)),
3737
Some(Either::Right(count)) => Some(Either::Right(*count)),
3838
None => None,
3939
}
@@ -55,6 +55,6 @@ where
5555
.column_names
5656
.get(*self)
5757
.ok_or_else(|| Error::ColumnNotFound((*self).into()))
58-
.map(|v| *v)
58+
.copied()
5959
}
6060
}

sqlx-core/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
clippy::cast_precision_loss,
1010
clippy::cast_sign_loss
1111
)]
12+
#![allow(
13+
clippy::multiple_bound_locations,
14+
clippy::empty_line_after_doc_comments
15+
)]
1216
// See `clippy.toml` at the workspace root
1317
#![deny(clippy::disallowed_methods)]
1418
//

sqlx-core/src/mssql/column.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ impl Column for MssqlColumn {
3232
self.ordinal
3333
}
3434

35-
fn name(&self) -> &str {
36-
&*self.name
37-
}
35+
fn name(&self) -> &str { &self.name }
3836

3937
fn type_info(&self) -> &MssqlTypeInfo {
4038
&self.type_info

sqlx-core/src/mysql/arguments.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@ impl MySqlArguments {
2323
self.null_bitmap.resize((index / 8) + 1, 0);
2424

2525
if let IsNull::Yes = value.encode(&mut self.values) {
26-
self.null_bitmap[index / 8] |= (1 << (index % 8)) as u8;
26+
self.null_bitmap[index / 8] |= u8::try_from(1 << (index % 8)).unwrap_or(0);
2727
}
2828
}
2929

3030
#[doc(hidden)]
3131
pub fn len(&self) -> usize {
3232
self.types.len()
3333
}
34+
35+
#[doc(hidden)]
36+
pub fn is_empty(&self) -> bool {
37+
self.types.is_empty()
38+
}
3439
}
3540

3641
impl<'q> Arguments<'q> for MySqlArguments {

sqlx-core/src/mysql/column.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ impl Column for MySqlColumn {
2323
self.ordinal
2424
}
2525

26-
fn name(&self) -> &str {
27-
&*self.name
28-
}
26+
fn name(&self) -> &str { &self.name }
2927

3028
fn type_info(&self) -> &MySqlTypeInfo {
3129
&self.type_info

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ async fn encrypt_rsa<'s>(
141141

142142
let (a, b) = (nonce.first_ref(), nonce.last_ref());
143143
let mut nonce = Vec::with_capacity(a.len() + b.len());
144-
nonce.extend_from_slice(&*a);
145-
nonce.extend_from_slice(&*b);
144+
nonce.extend_from_slice(a);
145+
nonce.extend_from_slice(b);
146146

147-
xor_eq(&mut pass, &*nonce);
147+
xor_eq(&mut pass, &nonce);
148148

149149
// client sends an RSA encrypted password
150150
let pkey = parse_rsa_pub_key(rsa_pub_key)?;
@@ -179,5 +179,5 @@ fn parse_rsa_pub_key(key: &[u8]) -> Result<RsaPublicKey, Error> {
179179
// we are receiving a PKCS#8 RSA Public Key at all
180180
// times from MySQL
181181

182-
RsaPublicKey::from_public_key_pem(&pem).map_err(Error::protocol)
182+
RsaPublicKey::from_public_key_pem(pem).map_err(Error::protocol)
183183
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ impl MySqlStream {
138138

139139
let mut header: Bytes = self.stream.read(4).await?;
140140

141-
let packet_size = header.get_uint_le(3) as usize;
141+
let packet_size: usize = header
142+
.get_uint_le(3)
143+
.try_into()
144+
.expect("packet length should fit in usize");
142145
let sequence_id = header.get_u8();
143146

144147
self.sequence_id = sequence_id.wrapping_add(1);
@@ -149,7 +152,7 @@ impl MySqlStream {
149152
// TODO: packet joining
150153

151154
if payload
152-
.get(0)
155+
.first()
153156
.ok_or(err_protocol!("Packet empty"))?
154157
.eq(&0xff)
155158
{

sqlx-core/src/mysql/options/parse.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ impl FromStr for MySqlConnectOptions {
2222
let username = url.username();
2323
if !username.is_empty() {
2424
options = options.username(
25-
&*percent_decode_str(username)
25+
&percent_decode_str(username)
2626
.decode_utf8()
2727
.map_err(Error::config)?,
2828
);
2929
}
3030

3131
if let Some(password) = url.password() {
3232
options = options.password(
33-
&*percent_decode_str(password)
33+
&percent_decode_str(password)
3434
.decode_utf8()
3535
.map_err(Error::config)?,
3636
);
@@ -52,11 +52,11 @@ impl FromStr for MySqlConnectOptions {
5252
}
5353

5454
"charset" => {
55-
options = options.charset(&*value);
55+
options = options.charset(&value);
5656
}
5757

5858
"collation" => {
59-
options = options.collation(&*value);
59+
options = options.collation(&value);
6060
}
6161

6262
"statement-cache-capacity" => {

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

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

6363
let auth_plugin_data_2 = if capabilities.contains(Capabilities::SECURE_CONNECTION) {
64-
let len = ((auth_plugin_data_len as isize) - 9).max(12) as usize;
64+
let len = core::cmp::max((auth_plugin_data_len as isize) - 9, 12) as usize;
6565
let v = buf.get_bytes(len);
6666
buf.advance(1); // NUL-terminator
6767

0 commit comments

Comments
 (0)