Skip to content

Commit de8df29

Browse files
authored
Added clippy to CI and fixed all clippy warnings (#613)
* Fixed all clippy warnings. * Added `clippy` to CI. * Reverted an unwanted change + Applied `cargo fmt`. * Fixed the idiom version. * Revert "Fixed the idiom version." This reverts commit 6f78be0. * Fixed clippy issues on CI. * Revert "Fixed clippy issues on CI." This reverts commit a9fa6ba. * Revert "Reverted an unwanted change + Applied `cargo fmt`." This reverts commit 6bd37b6. * Revert "Fixed all clippy warnings." This reverts commit d1f3b84. * Removed Clippy * Removed Lint * `admin.rs` clippy fixes. * Applied more clippy changes. * Even more clippy changes. * `client.rs` clippy fixes. * `server.rs` clippy fixes. * Revert "Removed Lint" This reverts commit cb5042b. * Revert "Removed Clippy" This reverts commit 6dec8bf. * Applied lint. * Revert "Revert "Fixed clippy issues on CI."" This reverts commit 49164a7.
1 parent c4fb72b commit de8df29

18 files changed

+258
-304
lines changed

.circleci/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ jobs:
6363
- run:
6464
name: "Lint"
6565
command: "cargo fmt --check"
66+
- run:
67+
name: "Clippy"
68+
command: "cargo clippy --all --all-targets -- -Dwarnings"
6669
- run:
6770
name: "Tests"
6871
command: "cargo clean && cargo build && cargo test && bash .circleci/run_tests.sh && .circleci/generate_coverage.sh"

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Thank you for contributing! Just a few tips here:
44

5-
1. `cargo fmt` your code before opening up a PR
5+
1. `cargo fmt` and `cargo clippy` your code before opening up a PR
66
2. Run the test suite (e.g. `pgbench`) to make sure everything still works. The tests are in `.circleci/run_tests.sh`.
77
3. Performance is important, make sure there are no regressions in your branch vs. `main`.
88

src/admin.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ where
283283
{
284284
let mut res = BytesMut::new();
285285

286-
let detail_msg = vec![
286+
let detail_msg = [
287287
"",
288288
"SHOW HELP|CONFIG|DATABASES|POOLS|CLIENTS|SERVERS|USERS|VERSION",
289289
// "SHOW PEERS|PEER_POOLS", // missing PEERS|PEER_POOLS
@@ -301,7 +301,6 @@ where
301301
// "KILL <db>",
302302
// "SUSPEND",
303303
"SHUTDOWN",
304-
// "WAIT_CLOSE [<db>]", // missing
305304
];
306305

307306
res.put(notify("Console usage", detail_msg.join("\n\t")));
@@ -802,7 +801,7 @@ where
802801
T: tokio::io::AsyncWrite + std::marker::Unpin,
803802
{
804803
let parts: Vec<&str> = match tokens.len() == 2 {
805-
true => tokens[1].split(",").map(|part| part.trim()).collect(),
804+
true => tokens[1].split(',').map(|part| part.trim()).collect(),
806805
false => Vec::new(),
807806
};
808807

@@ -865,7 +864,7 @@ where
865864
T: tokio::io::AsyncWrite + std::marker::Unpin,
866865
{
867866
let parts: Vec<&str> = match tokens.len() == 2 {
868-
true => tokens[1].split(",").map(|part| part.trim()).collect(),
867+
true => tokens[1].split(',').map(|part| part.trim()).collect(),
869868
false => Vec::new(),
870869
};
871870

src/client.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub async fn client_entrypoint(
131131
// Client requested a TLS connection.
132132
Ok((ClientConnectionType::Tls, _)) => {
133133
// TLS settings are configured, will setup TLS now.
134-
if tls_certificate != None {
134+
if tls_certificate.is_some() {
135135
debug!("Accepting TLS request");
136136

137137
let mut yes = BytesMut::new();
@@ -448,7 +448,7 @@ where
448448
None => "pgcat",
449449
};
450450

451-
let client_identifier = ClientIdentifier::new(&application_name, &username, &pool_name);
451+
let client_identifier = ClientIdentifier::new(application_name, username, pool_name);
452452

453453
let admin = ["pgcat", "pgbouncer"]
454454
.iter()
@@ -795,7 +795,7 @@ where
795795
let mut will_prepare = false;
796796

797797
let client_identifier = ClientIdentifier::new(
798-
&self.server_parameters.get_application_name(),
798+
self.server_parameters.get_application_name(),
799799
&self.username,
800800
&self.pool_name,
801801
);
@@ -982,15 +982,11 @@ where
982982
}
983983

984984
// Check on plugin results.
985-
match plugin_output {
986-
Some(PluginOutput::Deny(error)) => {
987-
self.buffer.clear();
988-
error_response(&mut self.write, &error).await?;
989-
plugin_output = None;
990-
continue;
991-
}
992-
993-
_ => (),
985+
if let Some(PluginOutput::Deny(error)) = plugin_output {
986+
self.buffer.clear();
987+
error_response(&mut self.write, &error).await?;
988+
plugin_output = None;
989+
continue;
994990
};
995991

996992
// Check if the pool is paused and wait until it's resumed.
@@ -1267,7 +1263,7 @@ where
12671263

12681264
// Safe to unwrap because we know this message has a certain length and has the code
12691265
// This reads the first byte without advancing the internal pointer and mutating the bytes
1270-
let code = *message.get(0).unwrap() as char;
1266+
let code = *message.first().unwrap() as char;
12711267

12721268
trace!("Message: {}", code);
12731269

@@ -1325,7 +1321,7 @@ where
13251321
self.stats.transaction();
13261322
server
13271323
.stats()
1328-
.transaction(&self.server_parameters.get_application_name());
1324+
.transaction(self.server_parameters.get_application_name());
13291325

13301326
// Release server back to the pool if we are in transaction mode.
13311327
// If we are in session mode, we keep the server until the client disconnects.
@@ -1400,13 +1396,10 @@ where
14001396
let close: Close = (&message).try_into()?;
14011397

14021398
if close.is_prepared_statement() && !close.anonymous() {
1403-
match self.prepared_statements.get(&close.name) {
1404-
Some(parse) => {
1405-
server.will_close(&parse.generated_name);
1406-
}
1407-
1399+
if let Some(parse) = self.prepared_statements.get(&close.name) {
1400+
server.will_close(&parse.generated_name);
1401+
} else {
14081402
// A prepared statement slipped through? Not impossible, since we don't support PREPARE yet.
1409-
None => (),
14101403
};
14111404
}
14121405
}
@@ -1445,7 +1438,7 @@ where
14451438

14461439
self.buffer.put(&message[..]);
14471440

1448-
let first_message_code = (*self.buffer.get(0).unwrap_or(&0)) as char;
1441+
let first_message_code = (*self.buffer.first().unwrap_or(&0)) as char;
14491442

14501443
// Almost certainly true
14511444
if first_message_code == 'P' && !prepared_statements_enabled {
@@ -1477,7 +1470,7 @@ where
14771470
self.stats.transaction();
14781471
server
14791472
.stats()
1480-
.transaction(&self.server_parameters.get_application_name());
1473+
.transaction(self.server_parameters.get_application_name());
14811474

14821475
// Release server back to the pool if we are in transaction mode.
14831476
// If we are in session mode, we keep the server until the client disconnects.
@@ -1739,7 +1732,7 @@ where
17391732
client_stats.query();
17401733
server.stats().query(
17411734
Instant::now().duration_since(query_start).as_millis() as u64,
1742-
&self.server_parameters.get_application_name(),
1735+
self.server_parameters.get_application_name(),
17431736
);
17441737

17451738
Ok(())

src/cmd_args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct Args {
2525
}
2626

2727
pub fn parse() -> Args {
28-
return Args::parse();
28+
Args::parse()
2929
}
3030

3131
#[derive(ValueEnum, Clone, Debug)]

src/config.rs

Lines changed: 45 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -236,18 +236,14 @@ impl Default for User {
236236

237237
impl User {
238238
fn validate(&self) -> Result<(), Error> {
239-
match self.min_pool_size {
240-
Some(min_pool_size) => {
241-
if min_pool_size > self.pool_size {
242-
error!(
243-
"min_pool_size of {} cannot be larger than pool_size of {}",
244-
min_pool_size, self.pool_size
245-
);
246-
return Err(Error::BadConfig);
247-
}
239+
if let Some(min_pool_size) = self.min_pool_size {
240+
if min_pool_size > self.pool_size {
241+
error!(
242+
"min_pool_size of {} cannot be larger than pool_size of {}",
243+
min_pool_size, self.pool_size
244+
);
245+
return Err(Error::BadConfig);
248246
}
249-
250-
None => (),
251247
};
252248

253249
Ok(())
@@ -677,9 +673,9 @@ impl Pool {
677673
Some(key) => {
678674
// No quotes in the key so we don't have to compare quoted
679675
// to unquoted idents.
680-
let key = key.replace("\"", "");
676+
let key = key.replace('\"', "");
681677

682-
if key.split(".").count() != 2 {
678+
if key.split('.').count() != 2 {
683679
error!(
684680
"automatic_sharding_key '{}' must be fully qualified, e.g. t.{}`",
685681
key, key
@@ -692,17 +688,14 @@ impl Pool {
692688
None => None,
693689
};
694690

695-
match self.default_shard {
696-
DefaultShard::Shard(shard_number) => {
697-
if shard_number >= self.shards.len() {
698-
error!("Invalid shard {:?}", shard_number);
699-
return Err(Error::BadConfig);
700-
}
691+
if let DefaultShard::Shard(shard_number) = self.default_shard {
692+
if shard_number >= self.shards.len() {
693+
error!("Invalid shard {:?}", shard_number);
694+
return Err(Error::BadConfig);
701695
}
702-
_ => (),
703696
}
704697

705-
for (_, user) in &self.users {
698+
for user in self.users.values() {
706699
user.validate()?;
707700
}
708701

@@ -777,8 +770,8 @@ impl<'de> serde::Deserialize<'de> for DefaultShard {
777770
D: Deserializer<'de>,
778771
{
779772
let s = String::deserialize(deserializer)?;
780-
if s.starts_with("shard_") {
781-
let shard = s[6..].parse::<usize>().map_err(serde::de::Error::custom)?;
773+
if let Some(s) = s.strip_prefix("shard_") {
774+
let shard = s.parse::<usize>().map_err(serde::de::Error::custom)?;
782775
return Ok(DefaultShard::Shard(shard));
783776
}
784777

@@ -874,7 +867,7 @@ pub trait Plugin {
874867
impl std::fmt::Display for Plugins {
875868
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
876869
fn is_enabled<T: Plugin>(arg: Option<&T>) -> bool {
877-
if let Some(ref arg) = arg {
870+
if let Some(arg) = arg {
878871
arg.is_enabled()
879872
} else {
880873
false
@@ -955,6 +948,7 @@ pub struct Query {
955948
}
956949

957950
impl Query {
951+
#[allow(clippy::needless_range_loop)]
958952
pub fn substitute(&mut self, db: &str, user: &str) {
959953
for col in self.result.iter_mut() {
960954
for i in 0..col.len() {
@@ -1079,8 +1073,8 @@ impl From<&Config> for std::collections::HashMap<String, String> {
10791073
(
10801074
format!("pools.{:?}.users", pool_name),
10811075
pool.users
1082-
.iter()
1083-
.map(|(_username, user)| &user.username)
1076+
.values()
1077+
.map(|user| &user.username)
10841078
.cloned()
10851079
.collect::<Vec<String>>()
10861080
.join(", "),
@@ -1165,13 +1159,9 @@ impl Config {
11651159
Some(tls_certificate) => {
11661160
info!("TLS certificate: {}", tls_certificate);
11671161

1168-
match self.general.tls_private_key.clone() {
1169-
Some(tls_private_key) => {
1170-
info!("TLS private key: {}", tls_private_key);
1171-
info!("TLS support is enabled");
1172-
}
1173-
1174-
None => (),
1162+
if let Some(tls_private_key) = self.general.tls_private_key.clone() {
1163+
info!("TLS private key: {}", tls_private_key);
1164+
info!("TLS support is enabled");
11751165
}
11761166
}
11771167

@@ -1206,8 +1196,8 @@ impl Config {
12061196
pool_name,
12071197
pool_config
12081198
.users
1209-
.iter()
1210-
.map(|(_, user_cfg)| user_cfg.pool_size)
1199+
.values()
1200+
.map(|user_cfg| user_cfg.pool_size)
12111201
.sum::<u32>()
12121202
.to_string()
12131203
);
@@ -1377,34 +1367,31 @@ impl Config {
13771367
}
13781368

13791369
// Validate TLS!
1380-
match self.general.tls_certificate.clone() {
1381-
Some(tls_certificate) => {
1382-
match load_certs(Path::new(&tls_certificate)) {
1383-
Ok(_) => {
1384-
// Cert is okay, but what about the private key?
1385-
match self.general.tls_private_key.clone() {
1386-
Some(tls_private_key) => match load_keys(Path::new(&tls_private_key)) {
1387-
Ok(_) => (),
1388-
Err(err) => {
1389-
error!("tls_private_key is incorrectly configured: {:?}", err);
1390-
return Err(Error::BadConfig);
1391-
}
1392-
},
1393-
1394-
None => {
1395-
error!("tls_certificate is set, but the tls_private_key is not");
1370+
if let Some(tls_certificate) = self.general.tls_certificate.clone() {
1371+
match load_certs(Path::new(&tls_certificate)) {
1372+
Ok(_) => {
1373+
// Cert is okay, but what about the private key?
1374+
match self.general.tls_private_key.clone() {
1375+
Some(tls_private_key) => match load_keys(Path::new(&tls_private_key)) {
1376+
Ok(_) => (),
1377+
Err(err) => {
1378+
error!("tls_private_key is incorrectly configured: {:?}", err);
13961379
return Err(Error::BadConfig);
13971380
}
1398-
};
1399-
}
1381+
},
14001382

1401-
Err(err) => {
1402-
error!("tls_certificate is incorrectly configured: {:?}", err);
1403-
return Err(Error::BadConfig);
1404-
}
1383+
None => {
1384+
error!("tls_certificate is set, but the tls_private_key is not");
1385+
return Err(Error::BadConfig);
1386+
}
1387+
};
1388+
}
1389+
1390+
Err(err) => {
1391+
error!("tls_certificate is incorrectly configured: {:?}", err);
1392+
return Err(Error::BadConfig);
14051393
}
14061394
}
1407-
None => (),
14081395
};
14091396

14101397
for pool in self.pools.values_mut() {

0 commit comments

Comments
 (0)