Skip to content

Commit 814662b

Browse files
committed
fix: clippy warnings
1 parent fc23b37 commit 814662b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+505
-476
lines changed

clippy.toml

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
disallowed-methods = [
2-
# It is *much* too easy to misread `x.min(y)` as "x should be *at least* y" when in fact it
3-
# means the *exact* opposite, and same with `x.max(y)`; use `cmp::{min, max}` instead.
4-
"core::cmp::Ord::min", "core::cmp::Ord::max"
5-
]
1+
[[disallowed-methods]]
2+
path = "core::cmp::Ord::min"
3+
reason = '''
4+
too easy to misread `x.min(y)` as "let the minimum value of `x` be `y`" when it actually means the exact opposite;
5+
use `std::cmp::min` instead.
6+
'''
7+
8+
[[disallowed-methods]]
9+
path = "core::cmp::Ord::max"
10+
reason = '''
11+
too easy to misread `x.max(y)` as "let the maximum value of `x` be `y`" when it actually means the exact opposite;
12+
use `std::cmp::max` instead.
13+
'''

sqlx-cli/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,15 @@ where
129129
.build(),
130130
|| {
131131
connect(db_url).map_err(|e| -> backoff::Error<anyhow::Error> {
132-
match e {
133-
sqlx::Error::Io(ref ioe) => match ioe.kind() {
132+
if let sqlx::Error::Io(ref ioe) = e {
133+
match ioe.kind() {
134134
io::ErrorKind::ConnectionRefused
135135
| io::ErrorKind::ConnectionReset
136136
| io::ErrorKind::ConnectionAborted => {
137137
return backoff::Error::transient(e.into());
138138
}
139139
_ => (),
140-
},
141-
_ => (),
140+
}
142141
}
143142

144143
backoff::Error::permanent(e.into())

sqlx-cli/src/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl Metadata {
8383
self.packages.get(id)
8484
}
8585

86-
pub fn entries<'this>(&'this self) -> btree_map::Iter<'this, MetadataId, Package> {
86+
pub fn entries(&self) -> btree_map::Iter<'_, MetadataId, Package> {
8787
self.packages.iter()
8888
}
8989

sqlx-cli/src/migrate.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn create_file(
2020
use std::path::PathBuf;
2121

2222
let mut file_name = file_prefix.to_string();
23-
file_name.push_str("_");
23+
file_name.push('_');
2424
file_name.push_str(&description.replace(' ', "_"));
2525
file_name.push_str(migration_type.suffix());
2626

@@ -120,20 +120,20 @@ pub async fn add(
120120
if migration_type.is_reversible() {
121121
create_file(
122122
migration_source,
123-
&file_prefix,
123+
file_prefix,
124124
description,
125125
MigrationType::ReversibleUp,
126126
)?;
127127
create_file(
128128
migration_source,
129-
&file_prefix,
129+
file_prefix,
130130
description,
131131
MigrationType::ReversibleDown,
132132
)?;
133133
} else {
134134
create_file(
135135
migration_source,
136-
&file_prefix,
136+
file_prefix,
137137
description,
138138
MigrationType::Simple,
139139
)?;
@@ -194,7 +194,7 @@ fn short_checksum(checksum: &[u8]) -> String {
194194

195195
pub async fn info(migration_source: &str, connect_opts: &ConnectOpts) -> anyhow::Result<()> {
196196
let migrator = Migrator::new(Path::new(migration_source)).await?;
197-
let mut conn = crate::connect(&connect_opts).await?;
197+
let mut conn = crate::connect(connect_opts).await?;
198198

199199
conn.ensure_migrations_table().await?;
200200

@@ -300,7 +300,7 @@ pub async fn run(
300300
let latest_version = applied_migrations
301301
.iter()
302302
.max_by(|x, y| x.version.cmp(&y.version))
303-
.and_then(|migration| Some(migration.version))
303+
.map(|migration| migration.version)
304304
.unwrap_or(0);
305305
if let Some(target_version) = target_version {
306306
if target_version < latest_version {
@@ -326,10 +326,8 @@ pub async fn run(
326326
}
327327
}
328328
None => {
329-
let skip = match target_version {
330-
Some(target_version) if migration.version > target_version => true,
331-
_ => false,
332-
};
329+
let skip =
330+
target_version.is_some_and(|target_version| migration.version > target_version);
333331

334332
let elapsed = if dry_run || skip {
335333
Duration::new(0, 0)
@@ -380,7 +378,7 @@ pub async fn revert(
380378
}
381379
}
382380

383-
let mut conn = crate::connect(&connect_opts).await?;
381+
let mut conn = crate::connect(connect_opts).await?;
384382

385383
conn.ensure_migrations_table().await?;
386384

@@ -395,7 +393,7 @@ pub async fn revert(
395393
let latest_version = applied_migrations
396394
.iter()
397395
.max_by(|x, y| x.version.cmp(&y.version))
398-
.and_then(|migration| Some(migration.version))
396+
.map(|migration| migration.version)
399397
.unwrap_or(0);
400398
if let Some(target_version) = target_version {
401399
if target_version > latest_version {
@@ -417,10 +415,9 @@ pub async fn revert(
417415
}
418416

419417
if applied_migrations.contains_key(&migration.version) {
420-
let skip = match target_version {
421-
Some(target_version) if migration.version <= target_version => true,
422-
_ => false,
423-
};
418+
let skip =
419+
target_version.is_some_and(|target_version| migration.version <= target_version);
420+
424421
let elapsed = if dry_run || skip {
425422
Duration::new(0, 0)
426423
} else {
@@ -447,7 +444,7 @@ pub async fn revert(
447444

448445
// Only a single migration will be reverted at a time if no target
449446
// version is supplied, so we break.
450-
if let None = target_version {
447+
if target_version.is_none() {
451448
break;
452449
}
453450
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ use std::future;
1111
impl<'c> Executor<'c> for &'c mut AnyConnection {
1212
type Database = Any;
1313

14-
fn fetch_many<'e, 'q: 'e, E: 'q>(
14+
fn fetch_many<'e, 'q: 'e, E>(
1515
self,
1616
mut query: E,
1717
) -> BoxStream<'e, Result<Either<AnyQueryResult, AnyRow>, Error>>
1818
where
1919
'c: 'e,
20-
E: Execute<'q, Any>,
20+
E: 'q + Execute<'q, Any>,
2121
{
2222
let arguments = match query.take_arguments().map_err(Error::Encode) {
2323
Ok(arguments) => arguments,
@@ -26,13 +26,13 @@ impl<'c> Executor<'c> for &'c mut AnyConnection {
2626
self.backend.fetch_many(query.sql(), arguments)
2727
}
2828

29-
fn fetch_optional<'e, 'q: 'e, E: 'q>(
29+
fn fetch_optional<'e, 'q: 'e, E>(
3030
self,
3131
mut query: E,
3232
) -> BoxFuture<'e, Result<Option<AnyRow>, Error>>
3333
where
3434
'c: 'e,
35-
E: Execute<'q, Self::Database>,
35+
E: 'q + Execute<'q, Self::Database>,
3636
{
3737
let arguments = match query.take_arguments().map_err(Error::Encode) {
3838
Ok(arguments) => arguments,

sqlx-core/src/any/statement.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<'q> Statement<'q> for AnyStatement<'q> {
3838

3939
fn parameters(&self) -> Option<Either<&[AnyTypeInfo], usize>> {
4040
match &self.parameters {
41-
Some(Either::Left(types)) => Some(Either::Left(&types)),
41+
Some(Either::Left(types)) => Some(Either::Left(types)),
4242
Some(Either::Right(count)) => Some(Either::Right(*count)),
4343
None => None,
4444
}
@@ -57,7 +57,7 @@ impl<'i> ColumnIndex<AnyStatement<'_>> for &'i str {
5757
.column_names
5858
.get(*self)
5959
.ok_or_else(|| Error::ColumnNotFound((*self).into()))
60-
.map(|v| *v)
60+
.copied()
6161
}
6262
}
6363

sqlx-core/src/arguments.rs

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use crate::types::Type;
77
use std::fmt::{self, Write};
88

99
/// A tuple of arguments to be sent to the database.
10+
// This lint is designed for general collections, but `Arguments` is not meant to be as such.
11+
#[allow(clippy::len_without_is_empty)]
1012
pub trait Arguments<'q>: Send + Sized + Default {
1113
type Database: Database;
1214

sqlx-core/src/common/statement_cache.rs

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ impl<T> StatementCache<T> {
4343
self.inner.len()
4444
}
4545

46+
pub fn is_empty(&self) -> bool {
47+
self.inner.is_empty()
48+
}
49+
4650
/// Removes the least recently used item from the cache.
4751
pub fn remove_lru(&mut self) -> Option<T> {
4852
self.inner.remove_lru().map(|(_, v)| v)

sqlx-core/src/connection.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub trait Connection: Send {
143143
{
144144
let options = url.parse();
145145

146-
Box::pin(async move { Ok(Self::connect_with(&options?).await?) })
146+
Box::pin(async move { Self::connect_with(&options?).await })
147147
}
148148

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

sqlx-core/src/executor.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,25 @@ pub trait Executor<'c>: Send + Debug + Sized {
3434
type Database: Database;
3535

3636
/// Execute the query and return the total number of rows affected.
37-
fn execute<'e, 'q: 'e, E: 'q>(
37+
fn execute<'e, 'q: 'e, E>(
3838
self,
3939
query: E,
4040
) -> BoxFuture<'e, Result<<Self::Database as Database>::QueryResult, Error>>
4141
where
4242
'c: 'e,
43-
E: Execute<'q, Self::Database>,
43+
E: 'q + Execute<'q, Self::Database>,
4444
{
4545
self.execute_many(query).try_collect().boxed()
4646
}
4747

4848
/// Execute multiple queries and return the rows affected from each query, in a stream.
49-
fn execute_many<'e, 'q: 'e, E: 'q>(
49+
fn execute_many<'e, 'q: 'e, E>(
5050
self,
5151
query: E,
5252
) -> BoxStream<'e, Result<<Self::Database as Database>::QueryResult, Error>>
5353
where
5454
'c: 'e,
55-
E: Execute<'q, Self::Database>,
55+
E: 'q + Execute<'q, Self::Database>,
5656
{
5757
self.fetch_many(query)
5858
.try_filter_map(|step| async move {
@@ -65,13 +65,13 @@ pub trait Executor<'c>: Send + Debug + Sized {
6565
}
6666

6767
/// Execute the query and return the generated results as a stream.
68-
fn fetch<'e, 'q: 'e, E: 'q>(
68+
fn fetch<'e, 'q: 'e, E>(
6969
self,
7070
query: E,
7171
) -> BoxStream<'e, Result<<Self::Database as Database>::Row, Error>>
7272
where
7373
'c: 'e,
74-
E: Execute<'q, Self::Database>,
74+
E: 'q + Execute<'q, Self::Database>,
7575
{
7676
self.fetch_many(query)
7777
.try_filter_map(|step| async move {
@@ -85,7 +85,7 @@ pub trait Executor<'c>: Send + Debug + Sized {
8585

8686
/// Execute multiple queries and return the generated results as a stream
8787
/// from each query, in a stream.
88-
fn fetch_many<'e, 'q: 'e, E: 'q>(
88+
fn fetch_many<'e, 'q: 'e, E>(
8989
self,
9090
query: E,
9191
) -> BoxStream<
@@ -97,28 +97,28 @@ pub trait Executor<'c>: Send + Debug + Sized {
9797
>
9898
where
9999
'c: 'e,
100-
E: Execute<'q, Self::Database>;
100+
E: 'q + Execute<'q, Self::Database>;
101101

102102
/// Execute the query and return all the generated results, collected into a [`Vec`].
103-
fn fetch_all<'e, 'q: 'e, E: 'q>(
103+
fn fetch_all<'e, 'q: 'e, E>(
104104
self,
105105
query: E,
106106
) -> BoxFuture<'e, Result<Vec<<Self::Database as Database>::Row>, Error>>
107107
where
108108
'c: 'e,
109-
E: Execute<'q, Self::Database>,
109+
E: 'q + Execute<'q, Self::Database>,
110110
{
111111
self.fetch(query).try_collect().boxed()
112112
}
113113

114114
/// Execute the query and returns exactly one row.
115-
fn fetch_one<'e, 'q: 'e, E: 'q>(
115+
fn fetch_one<'e, 'q: 'e, E>(
116116
self,
117117
query: E,
118118
) -> BoxFuture<'e, Result<<Self::Database as Database>::Row, Error>>
119119
where
120120
'c: 'e,
121-
E: Execute<'q, Self::Database>,
121+
E: 'q + Execute<'q, Self::Database>,
122122
{
123123
self.fetch_optional(query)
124124
.and_then(|row| match row {
@@ -129,13 +129,13 @@ pub trait Executor<'c>: Send + Debug + Sized {
129129
}
130130

131131
/// Execute the query and returns at most one row.
132-
fn fetch_optional<'e, 'q: 'e, E: 'q>(
132+
fn fetch_optional<'e, 'q: 'e, E>(
133133
self,
134134
query: E,
135135
) -> BoxFuture<'e, Result<Option<<Self::Database as Database>::Row>, Error>>
136136
where
137137
'c: 'e,
138-
E: Execute<'q, Self::Database>;
138+
E: 'q + Execute<'q, Self::Database>;
139139

140140
/// Prepare the SQL query to inspect the type information of its parameters
141141
/// and results.

sqlx-core/src/ext/async_stream.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ impl<'a, T> Stream for TryAsyncStream<'a, T> {
121121
#[macro_export]
122122
macro_rules! try_stream {
123123
($($block:tt)*) => {
124-
crate::ext::async_stream::TryAsyncStream::new(move |yielder| async move {
124+
$crate::ext::async_stream::TryAsyncStream::new(move |yielder| async move {
125125
// Anti-footgun: effectively pins `yielder` to this future to prevent any accidental
126126
// move to another task, which could deadlock.
127-
let ref yielder = yielder;
127+
let yielder = &yielder;
128128

129129
macro_rules! r#yield {
130130
($v:expr) => {{

sqlx-core/src/ext/ustr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ impl Hash for UStr {
3636
fn hash<H: Hasher>(&self, state: &mut H) {
3737
// Forward the hash to the string representation of this
3838
// A derive(Hash) encodes the enum discriminant
39-
(&**self).hash(state);
39+
(**self).hash(state);
4040
}
4141
}
4242

4343
impl Borrow<str> for UStr {
4444
#[inline]
4545
fn borrow(&self) -> &str {
46-
&**self
46+
self
4747
}
4848
}
4949

@@ -102,6 +102,6 @@ impl serde::Serialize for UStr {
102102
where
103103
S: serde::Serializer,
104104
{
105-
serializer.serialize_str(&self)
105+
serializer.serialize_str(self)
106106
}
107107
}

sqlx-core/src/io/buf.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub trait BufExt: Buf {
2222
impl BufExt for Bytes {
2323
fn get_bytes_nul(&mut self) -> Result<Bytes, Error> {
2424
let nul =
25-
memchr(b'\0', &self).ok_or_else(|| err_protocol!("expected NUL in byte sequence"))?;
25+
memchr(b'\0', self).ok_or_else(|| err_protocol!("expected NUL in byte sequence"))?;
2626

2727
let v = self.slice(0..nul);
2828

@@ -40,7 +40,7 @@ impl BufExt for Bytes {
4040

4141
fn get_str_nul(&mut self) -> Result<String, Error> {
4242
self.get_bytes_nul().and_then(|bytes| {
43-
from_utf8(&*bytes)
43+
from_utf8(&bytes)
4444
.map(ToOwned::to_owned)
4545
.map_err(|err| err_protocol!("{}", err))
4646
})

0 commit comments

Comments
 (0)