Skip to content

Commit

Permalink
fix: clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
abonander committed Jun 11, 2024
1 parent 99ddc48 commit b000ce9
Show file tree
Hide file tree
Showing 110 changed files with 510 additions and 477 deletions.
1 change: 1 addition & 0 deletions .github/workflows/sqlx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
rustup update
rustup component add clippy
rustup toolchain install beta
rustup component add --toolchain beta clippy
- run: >
cargo clippy
Expand Down
18 changes: 13 additions & 5 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
disallowed-methods = [
# It is *much* too easy to misread `x.min(y)` as "x should be *at least* y" when in fact it
# means the *exact* opposite, and same with `x.max(y)`; use `cmp::{min, max}` instead.
"core::cmp::Ord::min", "core::cmp::Ord::max"
]
[[disallowed-methods]]
path = "core::cmp::Ord::min"
reason = '''
too easy to misread `x.min(y)` as "let the minimum value of `x` be `y`" when it actually means the exact opposite;
use `std::cmp::min` instead.
'''

[[disallowed-methods]]
path = "core::cmp::Ord::max"
reason = '''
too easy to misread `x.max(y)` as "let the maximum value of `x` be `y`" when it actually means the exact opposite;
use `std::cmp::max` instead.
'''
7 changes: 3 additions & 4 deletions sqlx-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,15 @@ where
.build(),
|| {
connect(db_url).map_err(|e| -> backoff::Error<anyhow::Error> {
match e {
sqlx::Error::Io(ref ioe) => match ioe.kind() {
if let sqlx::Error::Io(ref ioe) = e {
match ioe.kind() {
io::ErrorKind::ConnectionRefused
| io::ErrorKind::ConnectionReset
| io::ErrorKind::ConnectionAborted => {
return backoff::Error::transient(e.into());
}
_ => (),
},
_ => (),
}
}

backoff::Error::permanent(e.into())
Expand Down
2 changes: 1 addition & 1 deletion sqlx-cli/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Metadata {
self.packages.get(id)
}

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

Expand Down
31 changes: 14 additions & 17 deletions sqlx-cli/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn create_file(
use std::path::PathBuf;

let mut file_name = file_prefix.to_string();
file_name.push_str("_");
file_name.push('_');
file_name.push_str(&description.replace(' ', "_"));
file_name.push_str(migration_type.suffix());

Expand Down Expand Up @@ -120,20 +120,20 @@ pub async fn add(
if migration_type.is_reversible() {
create_file(
migration_source,
&file_prefix,
file_prefix,
description,
MigrationType::ReversibleUp,
)?;
create_file(
migration_source,
&file_prefix,
file_prefix,
description,
MigrationType::ReversibleDown,
)?;
} else {
create_file(
migration_source,
&file_prefix,
file_prefix,
description,
MigrationType::Simple,
)?;
Expand Down Expand Up @@ -194,7 +194,7 @@ fn short_checksum(checksum: &[u8]) -> String {

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

conn.ensure_migrations_table().await?;

Expand Down Expand Up @@ -300,7 +300,7 @@ pub async fn run(
let latest_version = applied_migrations
.iter()
.max_by(|x, y| x.version.cmp(&y.version))
.and_then(|migration| Some(migration.version))
.map(|migration| migration.version)
.unwrap_or(0);
if let Some(target_version) = target_version {
if target_version < latest_version {
Expand All @@ -326,10 +326,8 @@ pub async fn run(
}
}
None => {
let skip = match target_version {
Some(target_version) if migration.version > target_version => true,
_ => false,
};
let skip =
target_version.is_some_and(|target_version| migration.version > target_version);

let elapsed = if dry_run || skip {
Duration::new(0, 0)
Expand Down Expand Up @@ -380,7 +378,7 @@ pub async fn revert(
}
}

let mut conn = crate::connect(&connect_opts).await?;
let mut conn = crate::connect(connect_opts).await?;

conn.ensure_migrations_table().await?;

Expand All @@ -395,7 +393,7 @@ pub async fn revert(
let latest_version = applied_migrations
.iter()
.max_by(|x, y| x.version.cmp(&y.version))
.and_then(|migration| Some(migration.version))
.map(|migration| migration.version)
.unwrap_or(0);
if let Some(target_version) = target_version {
if target_version > latest_version {
Expand All @@ -417,10 +415,9 @@ pub async fn revert(
}

if applied_migrations.contains_key(&migration.version) {
let skip = match target_version {
Some(target_version) if migration.version <= target_version => true,
_ => false,
};
let skip =
target_version.is_some_and(|target_version| migration.version <= target_version);

let elapsed = if dry_run || skip {
Duration::new(0, 0)
} else {
Expand All @@ -447,7 +444,7 @@ pub async fn revert(

// Only a single migration will be reverted at a time if no target
// version is supplied, so we break.
if let None = target_version {
if target_version.is_none() {
break;
}
}
Expand Down
8 changes: 4 additions & 4 deletions sqlx-core/src/any/connection/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ use std::future;
impl<'c> Executor<'c> for &'c mut AnyConnection {
type Database = Any;

fn fetch_many<'e, 'q: 'e, E: 'q>(
fn fetch_many<'e, 'q: 'e, E>(
self,
mut query: E,
) -> BoxStream<'e, Result<Either<AnyQueryResult, AnyRow>, Error>>
where
'c: 'e,
E: Execute<'q, Any>,
E: 'q + Execute<'q, Any>,
{
let arguments = match query.take_arguments().map_err(Error::Encode) {
Ok(arguments) => arguments,
Expand All @@ -26,13 +26,13 @@ impl<'c> Executor<'c> for &'c mut AnyConnection {
self.backend.fetch_many(query.sql(), arguments)
}

fn fetch_optional<'e, 'q: 'e, E: 'q>(
fn fetch_optional<'e, 'q: 'e, E>(
self,
mut query: E,
) -> BoxFuture<'e, Result<Option<AnyRow>, Error>>
where
'c: 'e,
E: Execute<'q, Self::Database>,
E: 'q + Execute<'q, Self::Database>,
{
let arguments = match query.take_arguments().map_err(Error::Encode) {
Ok(arguments) => arguments,
Expand Down
4 changes: 2 additions & 2 deletions sqlx-core/src/any/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<'q> Statement<'q> for AnyStatement<'q> {

fn parameters(&self) -> Option<Either<&[AnyTypeInfo], usize>> {
match &self.parameters {
Some(Either::Left(types)) => Some(Either::Left(&types)),
Some(Either::Left(types)) => Some(Either::Left(types)),
Some(Either::Right(count)) => Some(Either::Right(*count)),
None => None,
}
Expand All @@ -57,7 +57,7 @@ impl<'i> ColumnIndex<AnyStatement<'_>> for &'i str {
.column_names
.get(*self)
.ok_or_else(|| Error::ColumnNotFound((*self).into()))
.map(|v| *v)
.copied()
}
}

Expand Down
2 changes: 2 additions & 0 deletions sqlx-core/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::types::Type;
use std::fmt::{self, Write};

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

Expand Down
4 changes: 4 additions & 0 deletions sqlx-core/src/common/statement_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ impl<T> StatementCache<T> {
self.inner.len()
}

pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}

/// Removes the least recently used item from the cache.
pub fn remove_lru(&mut self) -> Option<T> {
self.inner.remove_lru().map(|(_, v)| v)
Expand Down
2 changes: 1 addition & 1 deletion sqlx-core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub trait Connection: Send {
{
let options = url.parse();

Box::pin(async move { Ok(Self::connect_with(&options?).await?) })
Box::pin(async move { Self::connect_with(&options?).await })
}

/// Establish a new database connection with the provided options.
Expand Down
28 changes: 14 additions & 14 deletions sqlx-core/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ pub trait Executor<'c>: Send + Debug + Sized {
type Database: Database;

/// Execute the query and return the total number of rows affected.
fn execute<'e, 'q: 'e, E: 'q>(
fn execute<'e, 'q: 'e, E>(
self,
query: E,
) -> BoxFuture<'e, Result<<Self::Database as Database>::QueryResult, Error>>
where
'c: 'e,
E: Execute<'q, Self::Database>,
E: 'q + Execute<'q, Self::Database>,
{
self.execute_many(query).try_collect().boxed()
}

/// Execute multiple queries and return the rows affected from each query, in a stream.
fn execute_many<'e, 'q: 'e, E: 'q>(
fn execute_many<'e, 'q: 'e, E>(
self,
query: E,
) -> BoxStream<'e, Result<<Self::Database as Database>::QueryResult, Error>>
where
'c: 'e,
E: Execute<'q, Self::Database>,
E: 'q + Execute<'q, Self::Database>,
{
self.fetch_many(query)
.try_filter_map(|step| async move {
Expand All @@ -65,13 +65,13 @@ pub trait Executor<'c>: Send + Debug + Sized {
}

/// Execute the query and return the generated results as a stream.
fn fetch<'e, 'q: 'e, E: 'q>(
fn fetch<'e, 'q: 'e, E>(
self,
query: E,
) -> BoxStream<'e, Result<<Self::Database as Database>::Row, Error>>
where
'c: 'e,
E: Execute<'q, Self::Database>,
E: 'q + Execute<'q, Self::Database>,
{
self.fetch_many(query)
.try_filter_map(|step| async move {
Expand All @@ -85,7 +85,7 @@ pub trait Executor<'c>: Send + Debug + Sized {

/// Execute multiple queries and return the generated results as a stream
/// from each query, in a stream.
fn fetch_many<'e, 'q: 'e, E: 'q>(
fn fetch_many<'e, 'q: 'e, E>(
self,
query: E,
) -> BoxStream<
Expand All @@ -97,28 +97,28 @@ pub trait Executor<'c>: Send + Debug + Sized {
>
where
'c: 'e,
E: Execute<'q, Self::Database>;
E: 'q + Execute<'q, Self::Database>;

/// Execute the query and return all the generated results, collected into a [`Vec`].
fn fetch_all<'e, 'q: 'e, E: 'q>(
fn fetch_all<'e, 'q: 'e, E>(
self,
query: E,
) -> BoxFuture<'e, Result<Vec<<Self::Database as Database>::Row>, Error>>
where
'c: 'e,
E: Execute<'q, Self::Database>,
E: 'q + Execute<'q, Self::Database>,
{
self.fetch(query).try_collect().boxed()
}

/// Execute the query and returns exactly one row.
fn fetch_one<'e, 'q: 'e, E: 'q>(
fn fetch_one<'e, 'q: 'e, E>(
self,
query: E,
) -> BoxFuture<'e, Result<<Self::Database as Database>::Row, Error>>
where
'c: 'e,
E: Execute<'q, Self::Database>,
E: 'q + Execute<'q, Self::Database>,
{
self.fetch_optional(query)
.and_then(|row| match row {
Expand All @@ -129,13 +129,13 @@ pub trait Executor<'c>: Send + Debug + Sized {
}

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

/// Prepare the SQL query to inspect the type information of its parameters
/// and results.
Expand Down
4 changes: 2 additions & 2 deletions sqlx-core/src/ext/async_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ impl<'a, T> Stream for TryAsyncStream<'a, T> {
#[macro_export]
macro_rules! try_stream {
($($block:tt)*) => {
crate::ext::async_stream::TryAsyncStream::new(move |yielder| async move {
$crate::ext::async_stream::TryAsyncStream::new(move |yielder| async move {
// Anti-footgun: effectively pins `yielder` to this future to prevent any accidental
// move to another task, which could deadlock.
let ref yielder = yielder;
let yielder = &yielder;

macro_rules! r#yield {
($v:expr) => {{
Expand Down
6 changes: 3 additions & 3 deletions sqlx-core/src/ext/ustr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ impl Hash for UStr {
fn hash<H: Hasher>(&self, state: &mut H) {
// Forward the hash to the string representation of this
// A derive(Hash) encodes the enum discriminant
(&**self).hash(state);
(**self).hash(state);
}
}

impl Borrow<str> for UStr {
#[inline]
fn borrow(&self) -> &str {
&**self
self
}
}

Expand Down Expand Up @@ -102,6 +102,6 @@ impl serde::Serialize for UStr {
where
S: serde::Serializer,
{
serializer.serialize_str(&self)
serializer.serialize_str(self)
}
}
Loading

0 comments on commit b000ce9

Please sign in to comment.