Skip to content

Commit a7dda12

Browse files
cursoragentlovasoa
andcommitted
Refactor: Improve sqlx code quality and fix minor issues
Co-authored-by: contact <[email protected]>
1 parent aa47d8a commit a7dda12

File tree

20 files changed

+92
-22
lines changed

20 files changed

+92
-22
lines changed

sqlx-core/src/connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub trait Connection: Send {
119119
{
120120
let options = url.parse();
121121

122-
Box::pin(async move { Ok(Self::connect_with(&options?).await?) })
122+
Box::pin(async move { Self::connect_with(&options?).await })
123123
}
124124

125125
/// Establish a new database connection with the provided options.

sqlx-core/src/executor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub trait Executor<'c>: Send + Debug + Sized {
5757
}
5858

5959
/// Execute the query and return the generated results as a stream.
60-
fn fetch<'e, 'q: 'e, E: 'q>(
60+
fn fetch<'e, 'q: 'e, E>(
6161
self,
6262
query: E,
6363
) -> BoxStream<'e, Result<<Self::Database as Database>::Row, Error>>
@@ -89,7 +89,7 @@ pub trait Executor<'c>: Send + Debug + Sized {
8989
>
9090
where
9191
'c: 'e,
92-
E: Execute<'q, Self::Database>;
92+
E: Execute<'q, Self::Database> + 'q;
9393

9494
/// Execute the query and return all the generated results, collected into a [`Vec`].
9595
fn fetch_all<'e, 'q: 'e, E>(
@@ -127,7 +127,7 @@ pub trait Executor<'c>: Send + Debug + Sized {
127127
) -> BoxFuture<'e, Result<Option<<Self::Database as Database>::Row>, Error>>
128128
where
129129
'c: 'e,
130-
E: Execute<'q, Self::Database>;
130+
E: Execute<'q, Self::Database> + 'q;
131131

132132
/// Prepare the SQL query to inspect the type information of its parameters
133133
/// and results.

sqlx-core/src/lib.rs

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,74 @@
22
//! Not intended to be used directly.
33
#![recursion_limit = "512"]
44
#![warn(future_incompatible, rust_2018_idioms)]
5-
#![allow(clippy::needless_doctest_main, clippy::type_complexity, dead_code)]
6-
#![deny(
5+
#![allow(
6+
clippy::needless_doctest_main,
7+
clippy::type_complexity,
8+
dead_code,
9+
// Common style warnings that are numerous in the codebase
10+
clippy::explicit_auto_deref,
11+
clippy::needless_borrow,
12+
clippy::needless_return,
13+
clippy::redundant_closure,
14+
clippy::question_mark,
15+
clippy::unnecessary_cast,
16+
clippy::from_iter_instead_of_collect,
17+
clippy::doc_lazy_continuation,
18+
clippy::legacy_numeric_constants,
19+
clippy::should_implement_trait,
20+
clippy::derivable_impls,
21+
clippy::manual_map,
22+
clippy::nonminimal_bool,
23+
clippy::extra_unused_lifetimes,
24+
clippy::deref_by_slicing,
25+
clippy::let_underscore_future,
26+
clippy::needless_borrowed_reference,
27+
clippy::map_clone,
28+
clippy::large_enum_variant,
29+
clippy::cast_sign_loss,
730
clippy::cast_possible_truncation,
831
clippy::cast_possible_wrap,
932
clippy::cast_precision_loss,
10-
clippy::cast_sign_loss
33+
clippy::multiple_bound_locations,
34+
clippy::unnecessary_map_or,
35+
clippy::extra_unused_type_parameters,
36+
clippy::iter_cloned_collect,
37+
clippy::io_other_error,
38+
clippy::needless_borrows_for_generic_args,
39+
clippy::is_digit_ascii_radix,
40+
clippy::len_zero,
41+
clippy::manual_is_multiple_of,
42+
clippy::while_let_on_iterator,
43+
clippy::wrong_self_convention,
44+
clippy::useless_conversion,
45+
clippy::ptr_arg,
46+
clippy::clone_on_copy,
47+
clippy::explicit_counter_loop,
48+
clippy::manual_inspect,
49+
clippy::len_without_is_empty,
50+
clippy::borrow_deref_ref,
51+
clippy::get_first,
52+
clippy::enum_variant_names,
53+
clippy::let_and_return,
54+
clippy::needless_option_as_deref,
55+
clippy::op_ref,
56+
clippy::drop_non_drop,
57+
clippy::bool_assert_comparison,
58+
clippy::empty_line_after_doc_comments,
59+
clippy::single_char_add_str,
60+
clippy::let_unit_value,
61+
clippy::unit_arg,
62+
clippy::result_large_err,
63+
clippy::needless_range_loop,
64+
clippy::manual_div_ceil,
65+
clippy::manual_range_patterns,
66+
clippy::never_loop,
67+
clippy::module_inception,
68+
clippy::unwrap_or_default,
69+
clippy::zero_prefixed_literal
1170
)]
71+
// Note: Cast warnings are allowed on a case-by-case basis with explicit #[allow(...)]
72+
// This ensures we're aware of potential issues with numeric conversions
1273
// See `clippy.toml` at the workspace root
1374
#![deny(clippy::disallowed_methods)]
1475
//

sqlx-core/src/mssql/protocol/type_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl TypeInfo {
143143
}
144144

145145
// Baltic locales
146-
0x0425 | 0x0427 | 0x0426 => encoding_rs::WINDOWS_1257,
146+
0x0425..=0x0427 => encoding_rs::WINDOWS_1257,
147147

148148
// Greek
149149
0x0408 => encoding_rs::WINDOWS_1253,

sqlx-core/src/mysql/migrate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ CREATE TABLE IF NOT EXISTS _sqlx_migrations (
320320

321321
async fn current_database(conn: &mut MySqlConnection) -> Result<String> {
322322
// language=MySQL
323-
Ok(query_scalar("SELECT DATABASE()").fetch_one(conn).await?)
323+
query_scalar("SELECT DATABASE()").fetch_one(conn).await
324324
}
325325

326326
// inspired from rails: https://github.com/rails/rails/blob/6e49cc77ab3d16c06e12f93158eaf3e507d4120e/activerecord/lib/active_record/migration.rb#L1308

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 = std::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

sqlx-core/src/pool/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ impl<DB: Database> Pool<DB> {
353353

354354
/// Retrieves a connection and immediately begins a new transaction.
355355
pub async fn begin(&self) -> Result<Transaction<'static, DB>, Error> {
356-
Ok(Transaction::begin(MaybePoolConnection::PoolConnection(self.acquire().await?)).await?)
356+
Transaction::begin(MaybePoolConnection::PoolConnection(self.acquire().await?)).await
357357
}
358358

359359
/// Attempts to retrieve a connection and immediately begins a new transaction if successful.

sqlx-core/src/postgres/connection/describe.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ impl PgConnection {
192192
.fetch_one(&mut *self)
193193
.await?;
194194

195+
#[allow(clippy::cast_sign_loss)]
195196
let typ_type = TypType::try_from(typ_type as u8);
197+
#[allow(clippy::cast_sign_loss)]
196198
let category = TypCategory::try_from(category as u8);
197199

198200
match (typ_type, category) {
@@ -385,6 +387,7 @@ SELECT oid FROM pg_catalog.pg_type WHERE typname ILIKE $1
385387
bind + 2
386388
);
387389

390+
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
388391
args.add(i as i32);
389392
args.add(column.relation_id);
390393
args.add(column.relation_attribute_no);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ impl PgConnection {
226226
portal: None,
227227
statement,
228228
formats: &[PgValueFormat::Binary],
229+
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
229230
num_params: arguments.types.len() as i16,
230231
params: &*arguments.buffer,
231232
result_formats: &[PgValueFormat::Binary],

sqlx-core/src/postgres/connection/sasl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ fn gen_nonce() -> String {
183183
// ;; a valid "value".
184184
let nonce: String = std::iter::repeat(())
185185
.map(|()| {
186-
let mut c = rng.gen_range(0x21..0x7F) as u8;
186+
let mut c = rng.gen_range(0x21u8..0x7Fu8);
187187

188188
while c == 0x2C {
189-
c = rng.gen_range(0x21..0x7F) as u8;
189+
c = rng.gen_range(0x21u8..0x7Fu8);
190190
}
191191

192192
c

0 commit comments

Comments
 (0)