]> Untitled Git - lemmy.git/commitdiff
Forbid usage of unwrap
authorFelix Ableitner <me@nutomic.com>
Mon, 1 Mar 2021 12:56:07 +0000 (13:56 +0100)
committerFelix Ableitner <me@nutomic.com>
Mon, 1 Mar 2021 18:24:34 +0000 (19:24 +0100)
.drone.yml
crates/apub/src/activity_queue.rs
crates/apub/src/inbox/user_inbox.rs
crates/db_queries/src/lib.rs
crates/utils/src/lib.rs
crates/utils/src/request.rs
crates/utils/src/settings/mod.rs
crates/utils/src/utils.rs
src/scheduled_tasks.rs

index 1a83c09a30a8716ed5bda91f0373a5a6ccf95096..8b9f15919992399a82f70efa05cd1fe0d3623305 100644 (file)
@@ -23,6 +23,7 @@ steps:
     image: ekidd/rust-musl-builder:1.50.0
     commands:
       - cargo clippy --workspace --tests --all-targets --all-features -- -D warnings -D deprecated -D clippy::perf -D clippy::complexity -D clippy::dbg_macro
+      - cargo clippy --workspace -- -D clippy::unwrap_used
 
   - name: cargo test
     image: ekidd/rust-musl-builder:1.50.0
index 152fcaf9b716a719e62cb3692323263fda8a2b7c..f607dafe009c89b959f6f412855a7edf856a19e0 100644 (file)
@@ -223,7 +223,7 @@ where
   let hostname = Settings::get().get_hostname_without_port()?;
   let inboxes: Vec<&Url> = inboxes
     .iter()
-    .filter(|i| i.domain().unwrap() != hostname)
+    .filter(|i| i.domain().expect("valid inbox url") != hostname)
     .collect();
 
   let activity = activity.into_any_base()?;
index 467bee77c0e7f7ea1342abbed39e3ad459a0a1d9..1a906d627b2a2d57a33b06bc89161997f2428171 100644 (file)
@@ -143,7 +143,14 @@ pub(crate) async fn user_receive_message(
   let actor_url = actor.actor_id();
   match kind {
     UserValidTypes::Accept => {
-      receive_accept(&context, any_base, actor, to_user.unwrap(), request_counter).await?;
+      receive_accept(
+        &context,
+        any_base,
+        actor,
+        to_user.expect("user provided"),
+        request_counter,
+      )
+      .await?;
     }
     UserValidTypes::Announce => {
       receive_announce(&context, any_base, actor, request_counter).await?
index 5667b4262837abfcf375f13b0e318ecb5c2c4e74..0f1c4ce62c0fce7799f3d7455d3c8e21f602177a 100644 (file)
@@ -231,13 +231,14 @@ pub fn establish_unpooled_connection() -> PgConnection {
   };
   let conn =
     PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
-  embedded_migrations::run(&conn).unwrap();
+  embedded_migrations::run(&conn).expect("load migrations");
   conn
 }
 
 lazy_static! {
   static ref EMAIL_REGEX: Regex =
-    Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
+    Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$")
+      .expect("compile email regex");
 }
 
 pub mod functions {
index 58d2abb9d7f5769f6e01f86aebb7cbf9bda0c9e3..bd2b56844c70122c2c1fcdd3b14186cff7351a18 100644 (file)
@@ -86,10 +86,10 @@ lazy_static! {
     "^group:([a-z0-9_]{{3, 20}})@{}$",
     Settings::get().hostname()
   ))
-  .unwrap();
+  .expect("compile webfinger regex");
   pub static ref WEBFINGER_USER_REGEX: Regex = Regex::new(&format!(
     "^acct:([a-z0-9_]{{3, 20}})@{}$",
     Settings::get().hostname()
   ))
-  .unwrap();
+  .expect("compile webfinger regex");
 }
index 428d7897463cc614e60fe0ef1f1910ee03824bef..bc12f1394cf008bff7b1dc6c2ea426769e75ba11 100644 (file)
@@ -43,7 +43,7 @@ where
     }
   }
 
-  response.unwrap()
+  response.expect("retry http request")
 }
 
 #[derive(Deserialize, Debug)]
index abd6f733cde9a641a13569df57921c6bb89bb46c..3911a18f92ca3270c557ecb3734ef318cd1d8a9e 100644 (file)
@@ -55,7 +55,7 @@ impl Settings {
 
   /// Returns the config as a struct.
   pub fn get() -> Self {
-    SETTINGS.read().unwrap().to_owned()
+    SETTINGS.read().expect("read config").to_owned()
   }
 
   pub fn get_database_url(&self) -> String {
@@ -116,18 +116,18 @@ impl Settings {
     )
   }
 
-  pub fn save_config_file(data: &str) -> Result<String, Error> {
+  pub fn save_config_file(data: &str) -> Result<String, LemmyError> {
     fs::write(CONFIG_FILE, data)?;
 
     // Reload the new settings
     // From https://stackoverflow.com/questions/29654927/how-do-i-assign-a-string-to-a-mutable-static-variable/47181804#47181804
-    let mut new_settings = SETTINGS.write().unwrap();
+    let mut new_settings = SETTINGS.write().expect("write config");
     *new_settings = match Settings::init() {
       Ok(c) => c,
       Err(e) => panic!("{}", e),
     };
 
-    Self::read_config_file()
+    Ok(Self::read_config_file()?)
   }
 
   pub fn database(&self) -> DatabaseConfig {
@@ -137,7 +137,7 @@ impl Settings {
     self.hostname.to_owned().unwrap_or_default()
   }
   pub fn bind(&self) -> IpAddr {
-    self.bind.unwrap()
+    self.bind.expect("return bind address")
   }
   pub fn port(&self) -> u16 {
     self.port.unwrap_or_default()
index 98eada07edbe67fabaf606acb75942b6214bdd98..33ea833d5dd004fa22a41f9351d0811d3c8560b8 100644 (file)
@@ -6,15 +6,15 @@ use rand::{distributions::Alphanumeric, thread_rng, Rng};
 use regex::{Regex, RegexBuilder};
 
 lazy_static! {
-static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
-static ref SLUR_REGEX: Regex = RegexBuilder::new(r"(fag(g|got|tard)?\b|cock\s?sucker(s|ing)?|\bn(i|1)g(\b|g?(a|er)?(s|z)?)\b|mudslime?s?|kikes?|\bspi(c|k)s?\b|\bchinks?|gooks?|bitch(es|ing|y)?|whor(es?|ing)|\btr(a|@)nn?(y|ies?)|\b(b|re|r)tard(ed)?s?)").case_insensitive(true).build().unwrap();
-static ref USERNAME_MATCHES_REGEX: Regex = Regex::new(r"/u/[a-zA-Z][0-9a-zA-Z_]*").unwrap();
-// TODO keep this old one, it didn't work with port well tho
-// static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)").unwrap();
-static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._:-]+)").unwrap();
-static ref VALID_USERNAME_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9_]{3,20}$").unwrap();
-static ref VALID_COMMUNITY_NAME_REGEX: Regex = Regex::new(r"^[a-z0-9_]{3,20}$").unwrap();
-static ref VALID_POST_TITLE_REGEX: Regex = Regex::new(r".*\S.*").unwrap();
+  static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").expect("compile regex");
+  static ref SLUR_REGEX: Regex = RegexBuilder::new(r"(fag(g|got|tard)?\b|cock\s?sucker(s|ing)?|\bn(i|1)g(\b|g?(a|er)?(s|z)?)\b|mudslime?s?|kikes?|\bspi(c|k)s?\b|\bchinks?|gooks?|bitch(es|ing|y)?|whor(es?|ing)|\btr(a|@)nn?(y|ies?)|\b(b|re|r)tard(ed)?s?)").case_insensitive(true).build().expect("compile regex");
+  static ref USERNAME_MATCHES_REGEX: Regex = Regex::new(r"/u/[a-zA-Z][0-9a-zA-Z_]*").expect("compile regex");
+  // TODO keep this old one, it didn't work with port well tho
+  // static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)").expect("compile regex");
+  static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._:-]+)").expect("compile regex");
+  static ref VALID_USERNAME_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9_]{3,20}$").expect("compile regex");
+  static ref VALID_COMMUNITY_NAME_REGEX: Regex = Regex::new(r"^[a-z0-9_]{3,20}$").expect("compile regex");
+  static ref VALID_POST_TITLE_REGEX: Regex = Regex::new(r".*\S.*").expect("compile regex");
 }
 
 pub fn naive_from_unix(time: i64) -> NaiveDateTime {
index 53dfd6bf36867417f7655c07c49532af8d7b6116..4751e9ea205a5588e041b3be7d632c4f63686114 100644 (file)
@@ -48,14 +48,14 @@ fn reindex_aggregates_tables(conn: &PgConnection) {
 fn reindex_table(conn: &PgConnection, table_name: &str) {
   info!("Reindexing table {} ...", table_name);
   let query = format!("reindex table concurrently {}", table_name);
-  sql_query(query).execute(conn).unwrap();
+  sql_query(query).execute(conn).expect("reindex table");
   info!("Done.");
 }
 
 /// Clear old activities (this table gets very large)
 fn clear_old_activities(conn: &PgConnection) {
   info!("Clearing old activities...");
-  Activity::delete_olds(&conn).unwrap();
+  Activity::delete_olds(&conn).expect("clear old activities");
   info!("Done.");
 }
 
@@ -75,10 +75,14 @@ fn active_counts(conn: &PgConnection) {
       "update site_aggregates set users_active_{} = (select * from site_aggregates_activity('{}'))",
       i.1, i.0
     );
-    sql_query(update_site_stmt).execute(conn).unwrap();
+    sql_query(update_site_stmt)
+      .execute(conn)
+      .expect("update site stats");
 
     let update_community_stmt = format!("update community_aggregates ca set users_active_{} = mv.count_ from community_aggregates_activity('{}') mv where ca.community_id = mv.community_id_", i.1, i.0);
-    sql_query(update_community_stmt).execute(conn).unwrap();
+    sql_query(update_community_stmt)
+      .execute(conn)
+      .expect("update community stats");
   }
 
   info!("Done.");