From: Felix Ableitner Date: Mon, 1 Mar 2021 12:56:07 +0000 (+0100) Subject: Forbid usage of unwrap X-Git-Url: http://these/git/%7B%60%24%7BrepoUrl%7D/blob/master/docs/static/%7BrepoUrl%7D?a=commitdiff_plain;h=7f56281c26c56e094ccb1db7a5f7a70ec8d77e68;p=lemmy.git Forbid usage of unwrap --- diff --git a/.drone.yml b/.drone.yml index 1a83c09a..8b9f1591 100644 --- a/.drone.yml +++ b/.drone.yml @@ -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 diff --git a/crates/apub/src/activity_queue.rs b/crates/apub/src/activity_queue.rs index 152fcaf9..f607dafe 100644 --- a/crates/apub/src/activity_queue.rs +++ b/crates/apub/src/activity_queue.rs @@ -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()?; diff --git a/crates/apub/src/inbox/user_inbox.rs b/crates/apub/src/inbox/user_inbox.rs index 467bee77..1a906d62 100644 --- a/crates/apub/src/inbox/user_inbox.rs +++ b/crates/apub/src/inbox/user_inbox.rs @@ -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? diff --git a/crates/db_queries/src/lib.rs b/crates/db_queries/src/lib.rs index 5667b426..0f1c4ce6 100644 --- a/crates/db_queries/src/lib.rs +++ b/crates/db_queries/src/lib.rs @@ -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 { diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs index 58d2abb9..bd2b5684 100644 --- a/crates/utils/src/lib.rs +++ b/crates/utils/src/lib.rs @@ -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"); } diff --git a/crates/utils/src/request.rs b/crates/utils/src/request.rs index 428d7897..bc12f139 100644 --- a/crates/utils/src/request.rs +++ b/crates/utils/src/request.rs @@ -43,7 +43,7 @@ where } } - response.unwrap() + response.expect("retry http request") } #[derive(Deserialize, Debug)] diff --git a/crates/utils/src/settings/mod.rs b/crates/utils/src/settings/mod.rs index abd6f733..3911a18f 100644 --- a/crates/utils/src/settings/mod.rs +++ b/crates/utils/src/settings/mod.rs @@ -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 { + pub fn save_config_file(data: &str) -> Result { 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() diff --git a/crates/utils/src/utils.rs b/crates/utils/src/utils.rs index 98eada07..33ea833d 100644 --- a/crates/utils/src/utils.rs +++ b/crates/utils/src/utils.rs @@ -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[\w.]+)@(?P[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)").unwrap(); -static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P[\w.]+)@(?P[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[\w.]+)@(?P[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)").expect("compile regex"); + static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P[\w.]+)@(?P[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 { diff --git a/src/scheduled_tasks.rs b/src/scheduled_tasks.rs index 53dfd6bf..4751e9ea 100644 --- a/src/scheduled_tasks.rs +++ b/src/scheduled_tasks.rs @@ -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.");