Merge pull request 'Forbid usage of unwrap' (#179) from clippy-unwrap into main

Reviewed-on: https://yerbamate.ml/LemmyNet/lemmy/pulls/179
This commit is contained in:
dessalines 2021-03-02 16:49:19 +00:00
commit 0a52396706
9 changed files with 38 additions and 25 deletions

View File

@ -23,6 +23,7 @@ steps:
image: ekidd/rust-musl-builder:1.50.0 image: ekidd/rust-musl-builder:1.50.0
commands: 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 --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 - name: cargo test
image: ekidd/rust-musl-builder:1.50.0 image: ekidd/rust-musl-builder:1.50.0

View File

@ -223,7 +223,7 @@ where
let hostname = Settings::get().get_hostname_without_port()?; let hostname = Settings::get().get_hostname_without_port()?;
let inboxes: Vec<&Url> = inboxes let inboxes: Vec<&Url> = inboxes
.iter() .iter()
.filter(|i| i.domain().unwrap() != hostname) .filter(|i| i.domain().expect("valid inbox url") != hostname)
.collect(); .collect();
let activity = activity.into_any_base()?; let activity = activity.into_any_base()?;

View File

@ -143,7 +143,14 @@ pub(crate) async fn user_receive_message(
let actor_url = actor.actor_id(); let actor_url = actor.actor_id();
match kind { match kind {
UserValidTypes::Accept => { 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 => { UserValidTypes::Announce => {
receive_announce(&context, any_base, actor, request_counter).await? receive_announce(&context, any_base, actor, request_counter).await?

View File

@ -231,13 +231,14 @@ pub fn establish_unpooled_connection() -> PgConnection {
}; };
let conn = let conn =
PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url)); 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 conn
} }
lazy_static! { lazy_static! {
static ref EMAIL_REGEX: Regex = 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 { pub mod functions {

View File

@ -86,10 +86,10 @@ lazy_static! {
"^group:([a-z0-9_]{{3, 20}})@{}$", "^group:([a-z0-9_]{{3, 20}})@{}$",
Settings::get().hostname() Settings::get().hostname()
)) ))
.unwrap(); .expect("compile webfinger regex");
pub static ref WEBFINGER_USER_REGEX: Regex = Regex::new(&format!( pub static ref WEBFINGER_USER_REGEX: Regex = Regex::new(&format!(
"^acct:([a-z0-9_]{{3, 20}})@{}$", "^acct:([a-z0-9_]{{3, 20}})@{}$",
Settings::get().hostname() Settings::get().hostname()
)) ))
.unwrap(); .expect("compile webfinger regex");
} }

View File

@ -43,7 +43,7 @@ where
} }
} }
response.unwrap() response.expect("retry http request")
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]

View File

@ -55,7 +55,7 @@ impl Settings {
/// Returns the config as a struct. /// Returns the config as a struct.
pub fn get() -> Self { pub fn get() -> Self {
SETTINGS.read().unwrap().to_owned() SETTINGS.read().expect("read config").to_owned()
} }
pub fn get_database_url(&self) -> String { 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)?; fs::write(CONFIG_FILE, data)?;
// Reload the new settings // Reload the new settings
// From https://stackoverflow.com/questions/29654927/how-do-i-assign-a-string-to-a-mutable-static-variable/47181804#47181804 // 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() { *new_settings = match Settings::init() {
Ok(c) => c, Ok(c) => c,
Err(e) => panic!("{}", e), Err(e) => panic!("{}", e),
}; };
Self::read_config_file() Ok(Self::read_config_file()?)
} }
pub fn database(&self) -> DatabaseConfig { pub fn database(&self) -> DatabaseConfig {
@ -137,7 +137,7 @@ impl Settings {
self.hostname.to_owned().unwrap_or_default() self.hostname.to_owned().unwrap_or_default()
} }
pub fn bind(&self) -> IpAddr { pub fn bind(&self) -> IpAddr {
self.bind.unwrap() self.bind.expect("return bind address")
} }
pub fn port(&self) -> u16 { pub fn port(&self) -> u16 {
self.port.unwrap_or_default() self.port.unwrap_or_default()

View File

@ -6,15 +6,15 @@ use rand::{distributions::Alphanumeric, thread_rng, Rng};
use regex::{Regex, RegexBuilder}; use regex::{Regex, RegexBuilder};
lazy_static! { 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 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().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().expect("compile regex");
static ref USERNAME_MATCHES_REGEX: Regex = Regex::new(r"/u/[a-zA-Z][0-9a-zA-Z_]*").unwrap(); 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 // 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._-]+\.[a-zA-Z0-9_-]+)").expect("compile regex");
static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._:-]+)").unwrap(); 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}$").unwrap(); 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}$").unwrap(); 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.*").unwrap(); static ref VALID_POST_TITLE_REGEX: Regex = Regex::new(r".*\S.*").expect("compile regex");
} }
pub fn naive_from_unix(time: i64) -> NaiveDateTime { pub fn naive_from_unix(time: i64) -> NaiveDateTime {

View File

@ -48,14 +48,14 @@ fn reindex_aggregates_tables(conn: &PgConnection) {
fn reindex_table(conn: &PgConnection, table_name: &str) { fn reindex_table(conn: &PgConnection, table_name: &str) {
info!("Reindexing table {} ...", table_name); info!("Reindexing table {} ...", table_name);
let query = format!("reindex table concurrently {}", 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."); info!("Done.");
} }
/// Clear old activities (this table gets very large) /// Clear old activities (this table gets very large)
fn clear_old_activities(conn: &PgConnection) { fn clear_old_activities(conn: &PgConnection) {
info!("Clearing old activities..."); info!("Clearing old activities...");
Activity::delete_olds(&conn).unwrap(); Activity::delete_olds(&conn).expect("clear old activities");
info!("Done."); info!("Done.");
} }
@ -75,10 +75,14 @@ fn active_counts(conn: &PgConnection) {
"update site_aggregates set users_active_{} = (select * from site_aggregates_activity('{}'))", "update site_aggregates set users_active_{} = (select * from site_aggregates_activity('{}'))",
i.1, i.0 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); 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."); info!("Done.");