From 0b9942da84bb9bfc9fc1eaf4170ad71864b7c179 Mon Sep 17 00:00:00 2001 From: Yiyu Lin Date: Sat, 16 May 2020 21:41:32 +0800 Subject: [PATCH] fix some unwrap() in Rust (#5485) --- cli/diagnostics.rs | 61 ++++++++++++++++++++++++---------------------- cli/flags.rs | 56 +++++++++++++++++++++--------------------- cli/ops/tls.rs | 4 +-- core/isolate.rs | 3 +-- core/js_errors.rs | 3 +-- 5 files changed, 63 insertions(+), 64 deletions(-) diff --git a/cli/diagnostics.rs b/cli/diagnostics.rs index 0c81ce79ed..ac0405acd8 100644 --- a/cli/diagnostics.rs +++ b/cli/diagnostics.rs @@ -107,14 +107,15 @@ fn format_message( level: usize, ) -> String { debug!("format_message"); - if message_chain.is_none() { - return format!("{:indent$}{}", "", message, indent = level); + + if let Some(message_chain) = message_chain { + let mut s = message_chain.format_message(level); + s.pop(); + + s + } else { + format!("{:indent$}{}", "", message, indent = level) } - - let mut s = message_chain.clone().unwrap().format_message(level); - s.pop(); - - s } /// Formats optional source, line and column numbers into a single string. @@ -146,26 +147,28 @@ fn format_maybe_related_information( } let mut s = String::new(); - let related_information = related_information.clone().unwrap(); - for rd in related_information { - s.push_str("\n\n"); - s.push_str(&format_stack( - match rd.category { - DiagnosticCategory::Error => true, - _ => false, - }, - format_message(&rd.message_chain, &rd.message, 0), - rd.source_line.clone(), - rd.start_column, - rd.end_column, - // Formatter expects 1-based line and column numbers, but ours are 0-based. - &[format_maybe_frame( - rd.script_resource_name.clone(), - rd.line_number.map(|n| n + 1), - rd.start_column.map(|n| n + 1), - )], - 4, - )); + + if let Some(related_information) = related_information { + for rd in related_information { + s.push_str("\n\n"); + s.push_str(&format_stack( + match rd.category { + DiagnosticCategory::Error => true, + _ => false, + }, + format_message(&rd.message_chain, &rd.message, 0), + rd.source_line.clone(), + rd.start_column, + rd.end_column, + // Formatter expects 1-based line and column numbers, but ours are 0-based. + &[format_maybe_frame( + rd.script_resource_name.clone(), + rd.line_number.map(|n| n + 1), + rd.start_column.map(|n| n + 1), + )], + 4, + )); + } } s @@ -222,8 +225,8 @@ impl DiagnosticMessageChain { s.push_str(&std::iter::repeat(" ").take(level * 2).collect::()); s.push_str(&self.message); s.push('\n'); - if self.next.is_some() { - let arr = self.next.clone().unwrap(); + if let Some(next) = &self.next { + let arr = next.clone(); for dm in arr { s.push_str(&dm.format_message(level + 1)); } diff --git a/cli/flags.rs b/cli/flags.rs index c703089f9d..ef3f75e233 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -1173,16 +1173,15 @@ fn reload_arg<'a, 'b>() -> Arg<'a, 'b> { } fn reload_arg_parse(flags: &mut Flags, matches: &ArgMatches) { - if matches.is_present("reload") { - if matches.value_of("reload").is_some() { - let cache_bl = matches.values_of("reload").unwrap(); - let raw_cache_blacklist: Vec = - cache_bl.map(std::string::ToString::to_string).collect(); + if let Some(cache_bl) = matches.values_of("reload") { + let raw_cache_blacklist: Vec = + cache_bl.map(std::string::ToString::to_string).collect(); + if raw_cache_blacklist.is_empty() { + flags.reload = true; + } else { flags.cache_blacklist = resolve_urls(raw_cache_blacklist); debug!("cache blacklist: {:#?}", &flags.cache_blacklist); flags.reload = false; - } else { - flags.reload = true; } } } @@ -1235,39 +1234,40 @@ fn no_remote_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { } fn permission_args_parse(flags: &mut Flags, matches: &clap::ArgMatches) { - if matches.is_present("allow-read") { - if matches.value_of("allow-read").is_some() { - let read_wl = matches.values_of("allow-read").unwrap(); - let raw_read_whitelist: Vec = - read_wl.map(PathBuf::from).collect(); + if let Some(read_wl) = matches.values_of("allow-read") { + let raw_read_whitelist: Vec = read_wl.map(PathBuf::from).collect(); + + if raw_read_whitelist.is_empty() { + flags.allow_read = true; + } else { flags.read_whitelist = resolve_fs_whitelist(&raw_read_whitelist); debug!("read whitelist: {:#?}", &flags.read_whitelist); - } else { - flags.allow_read = true; } } - if matches.is_present("allow-write") { - if matches.value_of("allow-write").is_some() { - let write_wl = matches.values_of("allow-write").unwrap(); - let raw_write_whitelist: Vec = - write_wl.map(PathBuf::from).collect(); + + if let Some(write_wl) = matches.values_of("allow-write") { + let raw_write_whitelist: Vec = + write_wl.map(PathBuf::from).collect(); + + if raw_write_whitelist.is_empty() { + flags.allow_write = true; + } else { flags.write_whitelist = resolve_fs_whitelist(&raw_write_whitelist); debug!("write whitelist: {:#?}", &flags.write_whitelist); - } else { - flags.allow_write = true; } } - if matches.is_present("allow-net") { - if matches.value_of("allow-net").is_some() { - let net_wl = matches.values_of("allow-net").unwrap(); - let raw_net_whitelist = - net_wl.map(std::string::ToString::to_string).collect(); + + if let Some(net_wl) = matches.values_of("allow-net") { + let raw_net_whitelist: Vec = + net_wl.map(std::string::ToString::to_string).collect(); + if raw_net_whitelist.is_empty() { + flags.allow_net = true; + } else { flags.net_whitelist = resolve_hosts(raw_net_whitelist); debug!("net whitelist: {:#?}", &flags.net_whitelist); - } else { - flags.allow_net = true; } } + if matches.is_present("allow-env") { flags.allow_env = true; } diff --git a/cli/ops/tls.rs b/cli/ops/tls.rs index 48c89be810..5cbdff3007 100644 --- a/cli/ops/tls.rs +++ b/cli/ops/tls.rs @@ -291,9 +291,7 @@ impl TlsListenerResource { /// Stop tracking a task. /// Happens when the task is done and thus no further tracking is needed. pub fn untrack_task(&mut self) { - if self.waker.is_some() { - self.waker.take(); - } + self.waker.take(); } } diff --git a/core/isolate.rs b/core/isolate.rs index e9ff9474cd..d017687c65 100644 --- a/core/isolate.rs +++ b/core/isolate.rs @@ -529,8 +529,7 @@ impl Future for CoreIsolate { assert_eq!(inner.shared.size(), 0); } - if overflow_response.is_some() { - let (op_id, buf) = overflow_response.take().unwrap(); + if let Some((op_id, buf)) = overflow_response.take() { async_op_response( scope, Some((op_id, buf)), diff --git a/core/js_errors.rs b/core/js_errors.rs index dfad7238ec..e8ea5a342f 100644 --- a/core/js_errors.rs +++ b/core/js_errors.rs @@ -261,8 +261,7 @@ fn format_source_loc( impl fmt::Display for JSError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - if self.script_resource_name.is_some() { - let script_resource_name = self.script_resource_name.as_ref().unwrap(); + if let Some(script_resource_name) = &self.script_resource_name { if self.line_number.is_some() && self.start_column.is_some() { assert!(self.line_number.is_some()); assert!(self.start_column.is_some());