diff --git a/Cargo.toml b/Cargo.toml index 53e14412..e3dd1ccd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -186,3 +186,6 @@ doc_markdown = "warn" manual_midpoint = "warn" semicolon_if_nothing_returned = "warn" unnecessary_semicolon = "warn" + +# nursery lints configuration +# or_fun_call = "warn" # enable it to help detect non trivial code used in `_or` method diff --git a/src/api/k2v/router.rs b/src/api/k2v/router.rs index 4e50bfa6..642b002b 100644 --- a/src/api/k2v/router.rs +++ b/src/api/k2v/router.rs @@ -62,7 +62,7 @@ impl Endpoint { let (bucket, partition_key) = path .split_once('/') .map(|(b, p)| (b.to_owned(), p.trim_start_matches('/'))) - .unwrap_or((path.to_owned(), "")); + .unwrap_or_else(|| (path.to_owned(), "")); if bucket.is_empty() { return Err(Error::bad_request("Missing bucket name")); diff --git a/src/api/s3/multipart.rs b/src/api/s3/multipart.rs index 5e56408f..c5eea912 100644 --- a/src/api/s3/multipart.rs +++ b/src/api/s3/multipart.rs @@ -494,7 +494,7 @@ pub async fn handle_complete_multipart_upload( .root_domain .as_ref() .map(|rd| s3_xml::Value(format!("https://{}.{}/{}", bucket_name, rd, key))) - .or(Some(s3_xml::Value(format!("/{}/{}", bucket_name, key)))), + .or_else(|| Some(s3_xml::Value(format!("/{}/{}", bucket_name, key)))), bucket: s3_xml::Value(bucket_name.to_string()), key: s3_xml::Value(key), etag: s3_xml::Value(format!("\"{}\"", etag)), diff --git a/src/api/s3/router.rs b/src/api/s3/router.rs index 3f2d9208..69c95876 100644 --- a/src/api/s3/router.rs +++ b/src/api/s3/router.rs @@ -330,7 +330,7 @@ impl Endpoint { } else { path.split_once('/') .map(|(b, p)| (b.to_owned(), p.trim_start_matches('/'))) - .unwrap_or((path.to_owned(), "")) + .unwrap_or_else(|| (path.to_owned(), "")) }; if *req.method() == Method::OPTIONS { diff --git a/src/garage/cli/remote/admin_token.rs b/src/garage/cli/remote/admin_token.rs index 5a0b0595..144c11c7 100644 --- a/src/garage/cli/remote/admin_token.rs +++ b/src/garage/cli/remote/admin_token.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use format_table::format_table; use chrono::Local; @@ -42,18 +44,18 @@ impl Cli { table_list_abbr(&tok.scope) }; let exp = if tok.expired { - "expired".to_string() + Cow::Borrowed("expired") } else { tok.expiration - .map(|x| x.with_timezone(&Local).to_string()) - .unwrap_or("never".into()) + .map(|x| x.with_timezone(&Local).to_string().into()) + .unwrap_or(Cow::Borrowed("never")) }; table.push(format!( "{}\t{}\t{}\t{}\t{}", tok.id.as_deref().unwrap_or("-"), tok.created - .map(|x| x.with_timezone(&Local).date_naive().to_string()) - .unwrap_or("-".into()), + .map(|x| x.with_timezone(&Local).date_naive().to_string().into()) + .unwrap_or(Cow::Borrowed("-")), tok.name, exp, scope, @@ -236,8 +238,8 @@ fn print_token_info(token: &GetAdminTokenInfoResponse) { "Expiration:\t{}", token .expiration - .map(|x| x.with_timezone(&Local).to_string()) - .unwrap_or("never".into()) + .map(|x| x.with_timezone(&Local).to_string().into()) + .unwrap_or(Cow::Borrowed("never")) ), String::new(), ]; diff --git a/src/garage/cli/remote/key.rs b/src/garage/cli/remote/key.rs index 657607ea..68de358f 100644 --- a/src/garage/cli/remote/key.rs +++ b/src/garage/cli/remote/key.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use format_table::format_table; use chrono::Local; @@ -33,11 +35,11 @@ impl Cli { let mut table = vec!["ID\tCreated\tName\tExpiration".to_string()]; for key in keys.0.iter() { let exp = if key.expired { - "expired".to_string() + Cow::from("expired") } else { key.expiration - .map(|x| x.with_timezone(&Local).to_string()) - .unwrap_or("never".into()) + .map(|x| x.with_timezone(&Local).to_string().into()) + .unwrap_or(Cow::Borrowed("never")) }; table.push(format!( "{}\t{}\t{}\t{}", @@ -288,8 +290,8 @@ fn print_key_info(key: &GetKeyInfoResponse) { format!( "Expiration:\t{}", key.expiration - .map(|x| x.with_timezone(&Local).to_string()) - .unwrap_or("never".into()) + .map(|x| x.with_timezone(&Local).to_string().into()) + .unwrap_or(Cow::Borrowed("never")) ), String::new(), format!("Can create buckets:\t{}", key.permissions.create_bucket), diff --git a/src/model/s3/lifecycle_worker.rs b/src/model/s3/lifecycle_worker.rs index a400b5bf..f014337f 100644 --- a/src/model/s3/lifecycle_worker.rs +++ b/src/model/s3/lifecycle_worker.rs @@ -65,7 +65,11 @@ pub fn register_bg_vars( vars: &mut vars::BgVars, ) { vars.register_ro(persister, "lifecycle-last-completed", |p| { - p.get_with(|x| x.last_completed.clone().unwrap_or("never".to_string())) + p.get_with(|x| { + x.last_completed + .clone() + .unwrap_or_else(|| "never".to_string()) + }) }); } diff --git a/src/rpc/layout/helper.rs b/src/rpc/layout/helper.rs index 2fa6f913..89ffe2ad 100644 --- a/src/rpc/layout/helper.rs +++ b/src/rpc/layout/helper.rs @@ -169,7 +169,7 @@ impl LayoutHelper { Ok(versions .iter() .find(|x| x.version == sync_min) - .or(versions.last()) + .or_else(|| versions.last()) .unwrap()) } @@ -271,7 +271,7 @@ impl LayoutHelper { .map(|x| x.load(Ordering::Relaxed) == 0) .unwrap_or(true) }) - .unwrap_or(self.inner().current().version); + .unwrap_or_else(|| self.inner().current().version); let changed = self.update(|layout| { layout .update_trackers