From 3fcd4cd80e035bb1514f8aad34ce497dba26c9ca Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Sat, 30 May 2020 18:28:55 -0700 Subject: [PATCH] style(core): apply more clippy suggestions --- sqlx-core/src/executor.rs | 1 - sqlx-core/src/lib.rs | 1 + sqlx-core/src/mysql/arguments.rs | 2 +- sqlx-core/src/mysql/connection/executor.rs | 1 + sqlx-core/src/mysql/io/buf.rs | 4 ++-- sqlx-core/src/mysql/options.rs | 6 ++++++ sqlx-core/src/mysql/protocol/response/err.rs | 4 ++-- sqlx-core/src/mysql/protocol/rsa.rs | 2 ++ sqlx-core/src/postgres/connection/describe.rs | 2 +- sqlx-core/src/postgres/connection/mod.rs | 10 +++------- sqlx-core/src/postgres/connection/sasl.rs | 10 +++++----- sqlx-core/src/postgres/connection/stream.rs | 2 +- sqlx-core/src/postgres/listener.rs | 2 +- .../src/postgres/message/authentication.rs | 8 ++------ sqlx-core/src/postgres/options.rs | 20 +++++++++++-------- sqlx-core/src/postgres/type_info.rs | 2 +- sqlx-core/src/postgres/types/bytes.rs | 2 +- sqlx-core/src/postgres/types/ipnetwork.rs | 2 +- sqlx-core/src/postgres/types/str.rs | 2 +- sqlx-core/src/query.rs | 2 +- sqlx-core/src/sqlite/connection/executor.rs | 2 +- sqlx-core/src/sqlite/options.rs | 6 ++++++ 22 files changed, 52 insertions(+), 41 deletions(-) diff --git a/sqlx-core/src/executor.rs b/sqlx-core/src/executor.rs index ae16edcb..9d9ad0ff 100644 --- a/sqlx-core/src/executor.rs +++ b/sqlx-core/src/executor.rs @@ -75,7 +75,6 @@ pub trait Executor<'c>: Send + Debug + Sized { /// Execute multiple queries and return the generated results as a stream /// from each query, in a stream. - #[allow(clippy::type_complexity)] fn fetch_many<'e, 'q: 'e, E: 'q>( self, query: E, diff --git a/sqlx-core/src/lib.rs b/sqlx-core/src/lib.rs index 8b1b1313..315ab346 100644 --- a/sqlx-core/src/lib.rs +++ b/sqlx-core/src/lib.rs @@ -2,6 +2,7 @@ //! Not intended to be used directly. #![recursion_limit = "512"] #![warn(future_incompatible, rust_2018_idioms)] +#![allow(clippy::needless_doctest_main, clippy::type_complexity)] // // Allows an API be documented as only available in some specific platforms. // diff --git a/sqlx-core/src/mysql/arguments.rs b/sqlx-core/src/mysql/arguments.rs index 11ddfe0e..47c7edea 100644 --- a/sqlx-core/src/mysql/arguments.rs +++ b/sqlx-core/src/mysql/arguments.rs @@ -31,7 +31,7 @@ impl<'q> Arguments<'q> for MySqlArguments { self.null_bitmap.resize((index / 8) + 1, 0); if let IsNull::Yes = value.encode(self) { - self.null_bitmap[index / 8] |= (1 << index % 8) as u8; + self.null_bitmap[index / 8] |= (1 << (index % 8)) as u8; } } } diff --git a/sqlx-core/src/mysql/connection/executor.rs b/sqlx-core/src/mysql/connection/executor.rs index 405493fa..b0042062 100644 --- a/sqlx-core/src/mysql/connection/executor.rs +++ b/sqlx-core/src/mysql/connection/executor.rs @@ -104,6 +104,7 @@ impl MySqlConnection { Ok(()) } + #[allow(clippy::needless_lifetimes)] async fn run<'c>( &'c mut self, query: &str, diff --git a/sqlx-core/src/mysql/io/buf.rs b/sqlx-core/src/mysql/io/buf.rs index d533c088..9ccb62e0 100644 --- a/sqlx-core/src/mysql/io/buf.rs +++ b/sqlx-core/src/mysql/io/buf.rs @@ -21,8 +21,8 @@ impl MySqlBufExt for Bytes { fn get_uint_lenenc(&mut self) -> u64 { match self.get_u8() { 0xfc => u64::from(self.get_u16_le()), - 0xfd => u64::from(self.get_uint_le(3)), - 0xfe => u64::from(self.get_u64_le()), + 0xfd => self.get_uint_le(3), + 0xfe => self.get_u64_le(), v => u64::from(v), } diff --git a/sqlx-core/src/mysql/options.rs b/sqlx-core/src/mysql/options.rs index 90ece06a..a3259f76 100644 --- a/sqlx-core/src/mysql/options.rs +++ b/sqlx-core/src/mysql/options.rs @@ -102,6 +102,12 @@ pub struct MySqlConnectOptions { pub(crate) ssl_ca: Option, } +impl Default for MySqlConnectOptions { + fn default() -> Self { + Self::new() + } +} + impl MySqlConnectOptions { /// Creates a new, default set of options ready for configuration pub fn new() -> Self { diff --git a/sqlx-core/src/mysql/protocol/response/err.rs b/sqlx-core/src/mysql/protocol/response/err.rs index 7cc2d8d0..e365a8e0 100644 --- a/sqlx-core/src/mysql/protocol/response/err.rs +++ b/sqlx-core/src/mysql/protocol/response/err.rs @@ -32,11 +32,11 @@ impl Decode<'_, Capabilities> for ErrPacket { // If the next byte is '#' then we have a SQL STATE if buf.get(0) == Some(&0x23) { buf.advance(1); - sql_state = Some(buf.get_str(5)?.to_owned()); + sql_state = Some(buf.get_str(5)?); } } - let error_message = buf.get_str(buf.len())?.to_owned(); + let error_message = buf.get_str(buf.len())?; Ok(Self { error_code, diff --git a/sqlx-core/src/mysql/protocol/rsa.rs b/sqlx-core/src/mysql/protocol/rsa.rs index 21c7b883..9ed44074 100644 --- a/sqlx-core/src/mysql/protocol/rsa.rs +++ b/sqlx-core/src/mysql/protocol/rsa.rs @@ -1,3 +1,5 @@ +#![allow(clippy::all)] + use digest::Digest; use num_bigint::BigUint; use rand::{thread_rng, Rng}; diff --git a/sqlx-core/src/postgres/connection/describe.rs b/sqlx-core/src/postgres/connection/describe.rs index 9b895dcc..0ab74756 100644 --- a/sqlx-core/src/postgres/connection/describe.rs +++ b/sqlx-core/src/postgres/connection/describe.rs @@ -244,7 +244,7 @@ ORDER BY attnum query_as_with::<_, (i32, Option), _>(&query, args) .fetch(self) - .zip(stream::iter(columns.into_iter().enumerate())) + .zip(stream::iter(columns.iter().enumerate())) .map(|(row, (field_idx, column))| -> Result, Error> { let (idx, not_null) = row?; diff --git a/sqlx-core/src/postgres/connection/mod.rs b/sqlx-core/src/postgres/connection/mod.rs index def2c0fd..343de9df 100644 --- a/sqlx-core/src/postgres/connection/mod.rs +++ b/sqlx-core/src/postgres/connection/mod.rs @@ -74,13 +74,9 @@ impl PgConnection { loop { let message = self.stream.recv().await?; - match message.format { - MessageFormat::ReadyForQuery => { - self.handle_ready_for_query(message)?; - break; - } - - _ => {} + if let MessageFormat::ReadyForQuery = message.format { + self.handle_ready_for_query(message)?; + break; } } } diff --git a/sqlx-core/src/postgres/connection/sasl.rs b/sqlx-core/src/postgres/connection/sasl.rs index 4540e904..14cfb8b6 100644 --- a/sqlx-core/src/postgres/connection/sasl.rs +++ b/sqlx-core/src/postgres/connection/sasl.rs @@ -9,11 +9,11 @@ use rand::Rng; use sha2::digest::Digest; use sha2::Sha256; -const GS2_HEADER: &'static str = "n,,"; -const CHANNEL_ATTR: &'static str = "c"; -const USERNAME_ATTR: &'static str = "n"; -const CLIENT_PROOF_ATTR: &'static str = "p"; -const NONCE_ATTR: &'static str = "r"; +const GS2_HEADER: &str = "n,,"; +const CHANNEL_ATTR: &str = "c"; +const USERNAME_ATTR: &str = "n"; +const CLIENT_PROOF_ATTR: &str = "p"; +const NONCE_ATTR: &str = "r"; pub(crate) async fn authenticate( stream: &mut PgStream, diff --git a/sqlx-core/src/postgres/connection/stream.rs b/sqlx-core/src/postgres/connection/stream.rs index 6b671627..52fc3835 100644 --- a/sqlx-core/src/postgres/connection/stream.rs +++ b/sqlx-core/src/postgres/connection/stream.rs @@ -77,7 +77,7 @@ impl PgStream { let contents = self.inner.read(size).await?; - return Ok(Message { format, contents }); + Ok(Message { format, contents }) } // Get the next message from the server diff --git a/sqlx-core/src/postgres/listener.rs b/sqlx-core/src/postgres/listener.rs index 554068f2..61437fc8 100644 --- a/sqlx-core/src/postgres/listener.rs +++ b/sqlx-core/src/postgres/listener.rs @@ -115,7 +115,7 @@ impl PgListener { #[inline] async fn connect_if_needed(&mut self) -> Result<(), Error> { - if let None = self.connection { + if self.connection.is_none() { let mut connection = self.pool.acquire().await?; connection.stream.notifications = self.buffer_tx.take(); diff --git a/sqlx-core/src/postgres/message/authentication.rs b/sqlx-core/src/postgres/message/authentication.rs index e2ca3627..47625acf 100644 --- a/sqlx-core/src/postgres/message/authentication.rs +++ b/sqlx-core/src/postgres/message/authentication.rs @@ -179,12 +179,8 @@ impl Decode<'_> for AuthenticationSaslFinal { let key = item[0]; let value = &item[2..]; - match key { - b'v' => { - verifier = base64::decode(value).map_err(Error::protocol)?; - } - - _ => {} + if let b'v' = key { + verifier = base64::decode(value).map_err(Error::protocol)?; } } diff --git a/sqlx-core/src/postgres/options.rs b/sqlx-core/src/postgres/options.rs index e31bab80..c77396fa 100644 --- a/sqlx-core/src/postgres/options.rs +++ b/sqlx-core/src/postgres/options.rs @@ -116,6 +116,12 @@ pub struct PgConnectOptions { pub(crate) ssl_root_cert: Option, } +impl Default for PgConnectOptions { + fn default() -> Self { + Self::new() + } +} + impl PgConnectOptions { /// Creates a new, default set of options ready for configuration. /// @@ -289,16 +295,14 @@ fn default_host(port: u16) -> String { "/tmp", // Default ]; - 'outer: loop { - for candidate in &candidates { - if Path::new(candidate).join(&socket).exists() { - break 'outer candidate.to_string(); - } + for candidate in &candidates { + if Path::new(candidate).join(&socket).exists() { + return candidate.to_string(); } - - // fallback to localhost if no socket was found - break "localhost".to_owned(); } + + // fallback to localhost if no socket was found + "localhost".to_owned() } impl FromStr for PgConnectOptions { diff --git a/sqlx-core/src/postgres/type_info.rs b/sqlx-core/src/postgres/type_info.rs index 63257a54..7722931b 100644 --- a/sqlx-core/src/postgres/type_info.rs +++ b/sqlx-core/src/postgres/type_info.rs @@ -603,7 +603,7 @@ impl TypeInfo for PgTypeInfo {} impl PartialEq for PgCustomType { fn eq(&self, other: &PgCustomType) -> bool { - return other.oid == self.oid; + other.oid == self.oid } } diff --git a/sqlx-core/src/postgres/types/bytes.rs b/sqlx-core/src/postgres/types/bytes.rs index 6fd5497e..37d158ad 100644 --- a/sqlx-core/src/postgres/types/bytes.rs +++ b/sqlx-core/src/postgres/types/bytes.rs @@ -59,7 +59,7 @@ impl<'r> Decode<'r, Postgres> for &'r [u8] { match value.format() { PgValueFormat::Binary => value.as_bytes(), PgValueFormat::Text => { - return Err("unsupported decode to `&[u8]` of BYTEA in a simple query; use a prepared query or decode to `Vec`".into()); + Err("unsupported decode to `&[u8]` of BYTEA in a simple query; use a prepared query or decode to `Vec`".into()) } } } diff --git a/sqlx-core/src/postgres/types/ipnetwork.rs b/sqlx-core/src/postgres/types/ipnetwork.rs index a4eb8f44..cc1c3864 100644 --- a/sqlx-core/src/postgres/types/ipnetwork.rs +++ b/sqlx-core/src/postgres/types/ipnetwork.rs @@ -128,6 +128,6 @@ impl Decode<'_, Postgres> for IpNetwork { } } - return Err(format!("invalid data received when expecting an INET").into()); + Err("invalid data received when expecting an INET".into()) } } diff --git a/sqlx-core/src/postgres/types/str.rs b/sqlx-core/src/postgres/types/str.rs index a890b926..1a6e7d16 100644 --- a/sqlx-core/src/postgres/types/str.rs +++ b/sqlx-core/src/postgres/types/str.rs @@ -32,7 +32,7 @@ impl Encode<'_, Postgres> for &'_ str { impl Encode<'_, Postgres> for String { fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull { - <&str as Encode>::encode(&mut &**self, buf) + <&str as Encode>::encode(&**self, buf) } } diff --git a/sqlx-core/src/query.rs b/sqlx-core/src/query.rs index fa5eaca8..98876593 100644 --- a/sqlx-core/src/query.rs +++ b/sqlx-core/src/query.rs @@ -302,7 +302,7 @@ where /// Make a SQL query. #[inline] -pub fn query<'q, DB>(sql: &'q str) -> Query<'q, DB, >::Arguments> +pub fn query(sql: &str) -> Query<'_, DB, >::Arguments> where DB: Database, { diff --git a/sqlx-core/src/sqlite/connection/executor.rs b/sqlx-core/src/sqlite/connection/executor.rs index ba174ff7..479ce51f 100644 --- a/sqlx-core/src/sqlite/connection/executor.rs +++ b/sqlx-core/src/sqlite/connection/executor.rs @@ -191,7 +191,7 @@ impl<'c> Executor<'c> for &'c mut SqliteConnection { columns.reserve(num_columns); for i in 0..num_columns { - let name = statement.column_name(i).to_owned().into(); + let name = statement.column_name(i).to_owned(); let type_info = statement.column_decltype(i); let not_null = statement.column_not_null(i)?; diff --git a/sqlx-core/src/sqlite/options.rs b/sqlx-core/src/sqlite/options.rs index c0623c23..5f925b6b 100644 --- a/sqlx-core/src/sqlite/options.rs +++ b/sqlx-core/src/sqlite/options.rs @@ -12,6 +12,12 @@ pub struct SqliteConnectOptions { pub(crate) in_memory: bool, } +impl Default for SqliteConnectOptions { + fn default() -> Self { + Self::new() + } +} + impl SqliteConnectOptions { pub fn new() -> Self { Self {