From 42c6224f92f131f38875dcf5e12a0ae293205bc7 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Fri, 28 Jun 2024 09:33:23 +0200 Subject: [PATCH] don't call contains_key() before remove() HashMap::remove() returns the value it removes as an Option<>, so instead of first checking if the key exists before removing it, just try to remove it and use the returned Option<> to test whether we should bail!(). Signed-off-by: Wolfgang Bumiller --- src/api2/access/user.rs | 8 ++------ src/api2/config/media_pool.rs | 4 +--- src/api2/config/remote.rs | 4 +--- src/api2/config/traffic_control.rs | 4 +--- 4 files changed, 5 insertions(+), 15 deletions(-) diff --git a/src/api2/access/user.rs b/src/api2/access/user.rs index 97cf520ac..1b4adaf8f 100644 --- a/src/api2/access/user.rs +++ b/src/api2/access/user.rs @@ -371,9 +371,7 @@ pub fn delete_user(userid: Userid, digest: Option) -> Result<(), Error> crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?; } - if config.sections.contains_key(userid.as_str()) { - config.sections.remove(userid.as_str()); - } else { + if config.sections.remove(userid.as_str()).is_none() { bail!("user '{}' does not exist.", userid); } @@ -649,9 +647,7 @@ pub fn delete_token( let tokenid = Authid::from((userid.clone(), Some(token_name.clone()))); let tokenid_string = tokenid.to_string(); - if config.sections.contains_key(&tokenid_string) { - config.sections.remove(&tokenid_string); - } else { + if config.sections.remove(&tokenid_string).is_none() { bail!( "token '{}' of user '{}' does not exist.", token_name.as_str(), diff --git a/src/api2/config/media_pool.rs b/src/api2/config/media_pool.rs index 9b6080be7..9389ea02f 100644 --- a/src/api2/config/media_pool.rs +++ b/src/api2/config/media_pool.rs @@ -225,9 +225,7 @@ pub fn delete_pool(name: String) -> Result<(), Error> { let (mut config, _digest) = pbs_config::media_pool::config()?; - if config.sections.contains_key(&name) { - config.sections.remove(&name); - } else { + if config.sections.remove(&name).is_none() { http_bail!(NOT_FOUND, "delete pool '{}' failed - no such pool", name); } diff --git a/src/api2/config/remote.rs b/src/api2/config/remote.rs index 8325dbce6..069aef28e 100644 --- a/src/api2/config/remote.rs +++ b/src/api2/config/remote.rs @@ -288,9 +288,7 @@ pub fn delete_remote(name: String, digest: Option) -> Result<(), Error> crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?; } - if config.sections.contains_key(&name) { - config.sections.remove(&name); - } else { + if config.sections.remove(&name).is_none() { http_bail!(NOT_FOUND, "remote '{}' does not exist.", name); } diff --git a/src/api2/config/traffic_control.rs b/src/api2/config/traffic_control.rs index 674a74044..e02aa20a3 100644 --- a/src/api2/config/traffic_control.rs +++ b/src/api2/config/traffic_control.rs @@ -258,9 +258,7 @@ pub fn delete_traffic_control(name: String, digest: Option) -> Result<() crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?; } - if config.sections.contains_key(&name) { - config.sections.remove(&name); - } else { + if config.sections.remove(&name).is_none() { http_bail!(NOT_FOUND, "traffic control rule '{}' does not exist.", name); }