Skip to content

Commit 198b125

Browse files
committed
postgres: fix clippy lints (casts, needless borrows, docs, len checks, parsing)
1 parent 706523d commit 198b125

File tree

10 files changed

+33
-29
lines changed

10 files changed

+33
-29
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ pub(crate) async fn authenticate(
126126

127127
// ClientSignature := HMAC(StoredKey, AuthMessage)
128128
let mut mac = Hmac::<Sha256>::new_from_slice(&stored_key).map_err(Error::protocol)?;
129-
mac.update(&auth_message.as_bytes());
129+
mac.update(auth_message.as_bytes());
130130

131131
let client_signature = mac.finalize().into_bytes();
132132

@@ -145,7 +145,7 @@ pub(crate) async fn authenticate(
145145

146146
// ServerSignature := HMAC(ServerKey, AuthMessage)
147147
let mut mac = Hmac::<Sha256>::new_from_slice(&server_key).map_err(Error::protocol)?;
148-
mac.update(&auth_message.as_bytes());
148+
mac.update(auth_message.as_bytes());
149149

150150
// client-final-message = client-final-message-without-proof "," proof
151151
let client_final_message = format!(
@@ -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: u8 = 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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl PgStream {
8484
let mut header: Bytes = self.inner.read(5).await?;
8585

8686
let format = MessageFormat::try_from_u8(header.get_u8())?;
87-
let size = (header.get_u32() - 4) as usize;
87+
let size = usize::try_from(header.get_u32() - 4).expect("message size should fit in usize");
8888

8989
let contents = self.inner.read(size).await?;
9090

sqlx-core/src/postgres/copy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl PgConnection {
4242
///
4343
/// 1. by closing the connection, or:
4444
/// 2. by using another connection to kill the server process that is sending the data as shown
45-
/// [in this StackOverflow answer](https://stackoverflow.com/a/35319598).
45+
/// [in this StackOverflow answer](https://stackoverflow.com/a/35319598).
4646
///
4747
/// If you don't read the stream to completion, the next time the connection is used it will
4848
/// need to read and discard all the remaining queued data, which could take some time.
@@ -149,7 +149,7 @@ impl<C: DerefMut<Target = PgConnection>> PgCopyIn<C> {
149149
/// Returns the number of columns expected in the input.
150150
pub fn num_columns(&self) -> usize {
151151
assert_eq!(
152-
self.response.num_columns as usize,
152+
usize::try_from(self.response.num_columns).expect("num_columns should be non-negative"),
153153
self.response.format_codes.len(),
154154
"num_columns does not match format_codes.len()"
155155
);

sqlx-core/src/postgres/io/buf_mut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl PgBufMutExt for Vec<u8> {
2525
f(self);
2626

2727
// now calculate the size of what we wrote and set the length value
28-
let size = (self.len() - offset) as i32;
28+
let size = i32::try_from(self.len() - offset).expect("message size should fit in i32");
2929
self[offset..(offset + 4)].copy_from_slice(&size.to_be_bytes());
3030
}
3131

sqlx-core/src/postgres/message/authentication.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ impl Decode<'_> for AuthenticationSaslContinue {
162162
Ok(Self {
163163
iterations,
164164
salt,
165-
nonce: from_utf8(&*nonce).map_err(Error::protocol)?.to_owned(),
166-
message: from_utf8(&*buf).map_err(Error::protocol)?.to_owned(),
165+
nonce: from_utf8(&nonce).map_err(Error::protocol)?.to_owned(),
166+
message: from_utf8(&buf).map_err(Error::protocol)?.to_owned(),
167167
})
168168
}
169169
}

sqlx-core/src/postgres/message/bind.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ impl Encode<'_> for Bind<'_> {
4242

4343
buf.put_statement_name(self.statement);
4444

45-
buf.extend(&(self.formats.len() as i16).to_be_bytes());
45+
buf.extend(
46+
&i16::try_from(self.formats.len())
47+
.expect("formats length should fit in i16")
48+
.to_be_bytes(),
49+
);
4650

4751
for &format in self.formats {
4852
buf.extend(&(format as i16).to_be_bytes());

sqlx-core/src/postgres/message/parse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::i16;
1+
// remove legacy numeric constants import
22

33
use crate::io::{BufMutExt, Encode};
44
use crate::postgres::io::PgBufMutExt;

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl FromStr for PgConnectOptions {
1616
if let Some(host) = url.host_str() {
1717
let host_decoded = percent_decode_str(host);
1818
options = match host_decoded.clone().next() {
19-
Some(b'/') => options.socket(&*host_decoded.decode_utf8().map_err(Error::config)?),
19+
Some(b'/') => options.socket(&host_decoded.decode_utf8().map_err(Error::config)?),
2020
_ => options.host(host),
2121
}
2222
}
@@ -28,15 +28,15 @@ impl FromStr for PgConnectOptions {
2828
let username = url.username();
2929
if !username.is_empty() {
3030
options = options.username(
31-
&*percent_decode_str(username)
31+
&percent_decode_str(username)
3232
.decode_utf8()
3333
.map_err(Error::config)?,
3434
);
3535
}
3636

3737
if let Some(password) = url.password() {
3838
options = options.password(
39-
&*percent_decode_str(password)
39+
&percent_decode_str(password)
4040
.decode_utf8()
4141
.map_err(Error::config)?,
4242
);
@@ -54,12 +54,12 @@ impl FromStr for PgConnectOptions {
5454
}
5555

5656
"sslrootcert" | "ssl-root-cert" | "ssl-ca" => {
57-
options = options.ssl_root_cert(&*value);
57+
options = options.ssl_root_cert(&value);
5858
}
5959

60-
"sslcert" | "ssl-cert" => options = options.ssl_client_cert(&*value),
60+
"sslcert" | "ssl-cert" => options = options.ssl_client_cert(&value),
6161

62-
"sslkey" | "ssl-key" => options = options.ssl_client_key(&*value),
62+
"sslkey" | "ssl-key" => options = options.ssl_client_key(&value),
6363

6464
"statement-cache-capacity" => {
6565
options =
@@ -68,39 +68,39 @@ impl FromStr for PgConnectOptions {
6868

6969
"host" => {
7070
if value.starts_with("/") {
71-
options = options.socket(&*value);
71+
options = options.socket(&value);
7272
} else {
73-
options = options.host(&*value);
73+
options = options.host(&value);
7474
}
7575
}
7676

7777
"hostaddr" => {
7878
value.parse::<IpAddr>().map_err(Error::config)?;
79-
options = options.host(&*value)
79+
options = options.host(&value)
8080
}
8181

8282
"port" => options = options.port(value.parse().map_err(Error::config)?),
8383

84-
"dbname" => options = options.database(&*value),
84+
"dbname" => options = options.database(&value),
8585

86-
"user" => options = options.username(&*value),
86+
"user" => options = options.username(&value),
8787

88-
"password" => options = options.password(&*value),
88+
"password" => options = options.password(&value),
8989

90-
"application_name" => options = options.application_name(&*value),
90+
"application_name" => options = options.application_name(&value),
9191

9292
"options" => {
9393
if let Some(options) = options.options.as_mut() {
9494
options.push(' ');
95-
options.push_str(&*value);
95+
options.push_str(&value);
9696
} else {
9797
options.options = Some(value.to_string());
9898
}
9999
}
100100

101101
k if k.starts_with("options[") => {
102102
if let Some(key) = k.strip_prefix("options[").unwrap().strip_suffix(']') {
103-
options = options.options([(key, &*value)]);
103+
options = options.options([(key, &value)]);
104104
}
105105
}
106106

sqlx-core/src/postgres/types/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ where
9494
T: Encode<'q, Postgres> + Type<Postgres>,
9595
{
9696
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
97-
let type_info = if self.len() < 1 {
97+
let type_info = if self.is_empty() {
9898
T::type_info()
9999
} else {
100100
self[0].produces().unwrap_or_else(T::type_info)

sqlx-core/src/postgres/types/float.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl Decode<'_, Postgres> for f64 {
6666
Ok(match value.format() {
6767
PgValueFormat::Binary => {
6868
if value.type_info == PgTypeInfo::NUMERIC {
69-
return Ok(PgNumeric::decode(value.as_bytes()?)?.try_into()?);
69+
return PgNumeric::decode(value.as_bytes()?)?.try_into();
7070
}
7171
let buf = value.as_bytes()?;
7272
match buf.len() {

0 commit comments

Comments
 (0)