From 53d5183394242612847accd2c3267005dc2af7eb Mon Sep 17 00:00:00 2001 From: Ron Kuris Date: Fri, 17 Nov 2023 22:07:08 +0000 Subject: [PATCH] Enable lints for discussion These lints are only in fwdctl because it was easy to fix the few errors that popped up. Once we agree these are the right lints, we can move them to the top level. For ease of review, here are links to the lint descriptions we are enabling as well reasons why I think they are important: - https://rust-lang.github.io/rust-clippy/rust-1.74.0/index.html#/unwrap_used - - code that panics should be avoided, except in tests - - not all panics have good error messages - https://rust-lang.github.io/rust-clippy/rust-1.74.0/index.html#/indexing_slicing - - panics - - there are good alternatives to index dereference that do not panic - https://rust-lang.github.io/rust-clippy/rust-1.74.0/index.html#/explicit_deref_methods - - easier and clearer just to dereference - https://rust-lang.github.io/rust-clippy/rust-1.74.0/index.html#/missing_const_for_fn - - non-const functions prevent callers from being const --- fwdctl/Cargo.toml | 9 +++++++++ fwdctl/src/create.rs | 4 ++-- fwdctl/src/delete.rs | 2 +- fwdctl/src/dump.rs | 4 ++-- fwdctl/src/get.rs | 4 ++-- fwdctl/src/insert.rs | 2 +- fwdctl/src/root.rs | 4 ++-- 7 files changed, 19 insertions(+), 10 deletions(-) diff --git a/fwdctl/Cargo.toml b/fwdctl/Cargo.toml index 1236f9ce1..e91ccf120 100644 --- a/fwdctl/Cargo.toml +++ b/fwdctl/Cargo.toml @@ -16,3 +16,12 @@ futures-util = "0.3.29" assert_cmd = "2.0.7" predicates = "3.0.1" serial_test = "2.0.0" + +[lints.rust] +unsafe_code = "deny" + +[lints.clippy] +unwrap_used = "warn" +indexing_slicing = "warn" +explicit_deref_methods = "warn" +missing_const_for_fn = "warn" diff --git a/fwdctl/src/create.rs b/fwdctl/src/create.rs index d52ed1127..51d3ed251 100644 --- a/fwdctl/src/create.rs +++ b/fwdctl/src/create.rs @@ -245,7 +245,7 @@ pub struct Options { max_revisions: u32, } -pub fn initialize_db_config(opts: &Options) -> DbConfig { +pub(super) const fn initialize_db_config(opts: &Options) -> DbConfig { DbConfig { meta_ncached_pages: opts.meta_ncached_pages, meta_ncached_files: opts.meta_ncached_files, @@ -279,7 +279,7 @@ pub fn initialize_db_config(opts: &Options) -> DbConfig { } } -pub async fn run(opts: &Options) -> Result<(), api::Error> { +pub(super) async fn run(opts: &Options) -> Result<(), api::Error> { let db_config = initialize_db_config(opts); log::debug!("database configuration parameters: \n{:?}\n", db_config); diff --git a/fwdctl/src/delete.rs b/fwdctl/src/delete.rs index 50bba1b29..c47f5a9b4 100644 --- a/fwdctl/src/delete.rs +++ b/fwdctl/src/delete.rs @@ -27,7 +27,7 @@ pub struct Options { pub db: String, } -pub async fn run(opts: &Options) -> Result<(), api::Error> { +pub(super) async fn run(opts: &Options) -> Result<(), api::Error> { log::debug!("deleting key {:?}", opts); let cfg = DbConfig::builder() .truncate(false) diff --git a/fwdctl/src/dump.rs b/fwdctl/src/dump.rs index 9320943de..3ae12358a 100644 --- a/fwdctl/src/dump.rs +++ b/fwdctl/src/dump.rs @@ -23,7 +23,7 @@ pub struct Options { pub db: String, } -pub async fn run(opts: &Options) -> Result<(), api::Error> { +pub(super) async fn run(opts: &Options) -> Result<(), api::Error> { log::debug!("dump database {:?}", opts); let cfg = DbConfig::builder() .truncate(false) @@ -37,7 +37,7 @@ pub async fn run(opts: &Options) -> Result<(), api::Error> { match stream.next().await { None => break, Some(Ok((key, value))) => { - println!("'{}': '{}'", u8_to_string(&key), u8_to_string(&value)) + println!("'{}': '{}'", u8_to_string(&key), u8_to_string(&value)); } Some(Err(e)) => return Err(e), } diff --git a/fwdctl/src/get.rs b/fwdctl/src/get.rs index 6a637ac4d..a94c437d1 100644 --- a/fwdctl/src/get.rs +++ b/fwdctl/src/get.rs @@ -26,7 +26,7 @@ pub struct Options { pub db: String, } -pub async fn run(opts: &Options) -> Result<(), api::Error> { +pub(super) async fn run(opts: &Options) -> Result<(), api::Error> { log::debug!("get key value pair {:?}", opts); let cfg = DbConfig::builder() .truncate(false) @@ -39,7 +39,7 @@ pub async fn run(opts: &Options) -> Result<(), api::Error> { match rev.val(opts.key.as_bytes()).await { Ok(Some(val)) => { let s = String::from_utf8_lossy(val.as_ref()); - println!("{:?}", s); + println!("{s:?}"); Ok(()) } Ok(None) => { diff --git a/fwdctl/src/insert.rs b/fwdctl/src/insert.rs index 84e6ef2b7..8cb56b8f7 100644 --- a/fwdctl/src/insert.rs +++ b/fwdctl/src/insert.rs @@ -31,7 +31,7 @@ pub struct Options { pub db: String, } -pub async fn run(opts: &Options) -> Result<(), api::Error> { +pub(super) async fn run(opts: &Options) -> Result<(), api::Error> { log::debug!("inserting key value pair {:?}", opts); let cfg = DbConfig::builder() .truncate(false) diff --git a/fwdctl/src/root.rs b/fwdctl/src/root.rs index 119a6b463..973d44616 100644 --- a/fwdctl/src/root.rs +++ b/fwdctl/src/root.rs @@ -23,7 +23,7 @@ pub struct Options { pub db: String, } -pub async fn run(opts: &Options) -> Result<(), api::Error> { +pub(super) async fn run(opts: &Options) -> Result<(), api::Error> { log::debug!("root hash {:?}", opts); let cfg = DbConfig::builder() .truncate(false) @@ -32,6 +32,6 @@ pub async fn run(opts: &Options) -> Result<(), api::Error> { let db = Db::new(opts.db.clone(), &cfg.build()).await?; let root = db.root_hash().await?; - println!("{:X?}", root); + println!("{root:X?}"); Ok(()) }