Skip to content

Commit

Permalink
fix some unwrap() in Rust (denoland#5485)
Browse files Browse the repository at this point in the history
  • Loading branch information
attila-lin authored May 16, 2020
1 parent 59cb3c1 commit 0b9942d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 63 deletions.
59 changes: 31 additions & 28 deletions cli/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,15 @@ fn format_message(
level: usize,
) -> String {
debug!("format_message");
if message_chain.is_none() {
return format!("{:indent$}{}", "", message, indent = level);
}

let mut s = message_chain.clone().unwrap().format_message(level);
s.pop();
if let Some(message_chain) = message_chain {
let mut s = message_chain.format_message(level);
s.pop();

s
s
} else {
format!("{:indent$}{}", "", message, indent = level)
}
}

/// Formats optional source, line and column numbers into a single string.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -222,8 +225,8 @@ impl DiagnosticMessageChain {
s.push_str(&std::iter::repeat(" ").take(level * 2).collect::<String>());
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));
}
Expand Down
56 changes: 28 additions & 28 deletions cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> =
cache_bl.map(std::string::ToString::to_string).collect();
if let Some(cache_bl) = matches.values_of("reload") {
let raw_cache_blacklist: Vec<String> =
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;
}
}
}
Expand Down Expand Up @@ -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<PathBuf> =
read_wl.map(PathBuf::from).collect();
if let Some(read_wl) = matches.values_of("allow-read") {
let raw_read_whitelist: Vec<PathBuf> = 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<PathBuf> =
write_wl.map(PathBuf::from).collect();

if let Some(write_wl) = matches.values_of("allow-write") {
let raw_write_whitelist: Vec<PathBuf> =
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<String> =
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;
}
Expand Down
4 changes: 1 addition & 3 deletions cli/ops/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
3 changes: 1 addition & 2 deletions core/isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
3 changes: 1 addition & 2 deletions core/js_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 0b9942d

Please sign in to comment.