]> git.proxmox.com Git - proxmox-backup.git/commitdiff
don't call contains_key() before remove()
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Fri, 28 Jun 2024 07:33:23 +0000 (09:33 +0200)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Fri, 28 Jun 2024 07:33:23 +0000 (09:33 +0200)
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 <w.bumiller@proxmox.com>
src/api2/access/user.rs
src/api2/config/media_pool.rs
src/api2/config/remote.rs
src/api2/config/traffic_control.rs

index 97cf520ac89533eb6860cca9e36d91cb15900ec2..1b4adaf8f169ab825be288e9615c4a369e07248f 100644 (file)
@@ -371,9 +371,7 @@ pub fn delete_user(userid: Userid, digest: Option<String>) -> 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(),
index 9b6080be71baa89120e176babcfed2e1382c38c9..9389ea02fe2b966ead6ad6b91911364d64e52289 100644 (file)
@@ -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);
     }
 
index 8325dbce666ae107d0c4bf4657028f8a37e79c85..069aef28eef096ae7f70209fa60fac2560219977 100644 (file)
@@ -288,9 +288,7 @@ pub fn delete_remote(name: String, digest: Option<String>) -> 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);
     }
 
index 674a74044029a1aebf3df4573724bd522e35a5a7..e02aa20a3dc5cbe52855eecb6faf2cf7d4eeba95 100644 (file)
@@ -258,9 +258,7 @@ pub fn delete_traffic_control(name: String, digest: Option<String>) -> 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);
     }