summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2023-03-15 17:46:36 -0400
committerGitHub <noreply@github.com>2023-03-15 17:46:36 -0400
commitfb021d7ceff3f8b1d7cdb0c2bdd75ea07c0428d2 (patch)
tree09cb2bf87bba760b1abf706e0b8faedc9c368bbc /runtime
parentca51f4f6c058d16ac438ad75ac92e8954895f5aa (diff)
refactor: remove usages of `map_or` / `map_or_else` (#18212)
These methods are confusing because the arguments are backwards. I feel like they should have never been added to `Option<T>` and that clippy should suggest rewriting to `map(...).unwrap_or(...)`/`map(...).unwrap_or_else(|| ...)` https://github.com/rust-lang/rfcs/issues/1025
Diffstat (limited to 'runtime')
-rw-r--r--runtime/fmt_errors.rs20
-rw-r--r--runtime/permissions/mod.rs48
2 files changed, 40 insertions, 28 deletions
diff --git a/runtime/fmt_errors.rs b/runtime/fmt_errors.rs
index 440ee373f..6852cbcd1 100644
--- a/runtime/fmt_errors.rs
+++ b/runtime/fmt_errors.rs
@@ -45,7 +45,8 @@ pub fn format_location(frame: &JsStackFrame) -> String {
let _internal = frame
.file_name
.as_ref()
- .map_or(false, |f| f.starts_with("ext:"));
+ .map(|f| f.starts_with("ext:"))
+ .unwrap_or(false);
if frame.is_native {
return cyan("native").to_string();
}
@@ -73,7 +74,8 @@ fn format_frame(frame: &JsStackFrame) -> String {
let _internal = frame
.file_name
.as_ref()
- .map_or(false, |f| f.starts_with("ext:"));
+ .map(|f| f.starts_with("ext:"))
+ .unwrap_or(false);
let is_method_call =
!(frame.is_top_level.unwrap_or_default() || frame.is_constructor);
let mut result = String::new();
@@ -252,7 +254,10 @@ fn format_js_error_inner(
if let Some(aggregated) = &js_error.aggregated {
let aggregated_message = format_aggregated_error(
aggregated,
- circular.as_ref().map_or(0, |circular| circular.index),
+ circular
+ .as_ref()
+ .map(|circular| circular.index)
+ .unwrap_or(0),
);
s.push_str(&aggregated_message);
}
@@ -274,9 +279,12 @@ fn format_js_error_inner(
write!(s, "\n at {}", format_frame(frame)).unwrap();
}
if let Some(cause) = &js_error.cause {
- let is_caused_by_circular = circular.as_ref().map_or(false, |circular| {
- errors_are_equal_without_cause(circular.reference.from, js_error)
- });
+ let is_caused_by_circular = circular
+ .as_ref()
+ .map(|circular| {
+ errors_are_equal_without_cause(circular.reference.from, js_error)
+ })
+ .unwrap_or(false);
let error_string = if is_caused_by_circular {
cyan(format!("[Circular *{}]", circular.unwrap().index)).to_string()
diff --git a/runtime/permissions/mod.rs b/runtime/permissions/mod.rs
index 3662b2d7f..c985295a7 100644
--- a/runtime/permissions/mod.rs
+++ b/runtime/permissions/mod.rs
@@ -70,7 +70,9 @@ impl PermissionState {
format!(
"{} access{}",
name,
- info().map_or(String::new(), |info| { format!(" to {info}") }),
+ info()
+ .map(|info| { format!(" to {info}") })
+ .unwrap_or_default(),
)
}
@@ -114,7 +116,9 @@ impl PermissionState {
let msg = format!(
"{} access{}",
name,
- info().map_or(String::new(), |info| { format!(" to {info}") }),
+ info()
+ .map(|info| { format!(" to {info}") })
+ .unwrap_or_default(),
);
match permission_prompt(&msg, name, api_name, true) {
PromptResponse::Allow => {
@@ -1569,14 +1573,14 @@ impl Permissions {
) -> Result<UnaryPermission<NetDescriptor>, AnyError> {
Ok(UnaryPermission::<NetDescriptor> {
global_state: global_state_from_option(state),
- granted_list: state.as_ref().map_or_else(
- || Ok(HashSet::new()),
- |v| {
+ granted_list: state
+ .as_ref()
+ .map(|v| {
v.iter()
.map(|x| NetDescriptor::from_str(x))
.collect::<Result<HashSet<NetDescriptor>, AnyError>>()
- },
- )?,
+ })
+ .unwrap_or_else(|| Ok(HashSet::new()))?,
prompt,
..Default::default()
})
@@ -1588,9 +1592,9 @@ impl Permissions {
) -> Result<UnaryPermission<EnvDescriptor>, AnyError> {
Ok(UnaryPermission::<EnvDescriptor> {
global_state: global_state_from_option(state),
- granted_list: state.as_ref().map_or_else(
- || Ok(HashSet::new()),
- |v| {
+ granted_list: state
+ .as_ref()
+ .map(|v| {
v.iter()
.map(|x| {
if x.is_empty() {
@@ -1600,8 +1604,8 @@ impl Permissions {
}
})
.collect()
- },
- )?,
+ })
+ .unwrap_or_else(|| Ok(HashSet::new()))?,
prompt,
..Default::default()
})
@@ -1613,9 +1617,9 @@ impl Permissions {
) -> Result<UnaryPermission<SysDescriptor>, AnyError> {
Ok(UnaryPermission::<SysDescriptor> {
global_state: global_state_from_option(state),
- granted_list: state.as_ref().map_or_else(
- || Ok(HashSet::new()),
- |v| {
+ granted_list: state
+ .as_ref()
+ .map(|v| {
v.iter()
.map(|x| {
if x.is_empty() {
@@ -1625,8 +1629,8 @@ impl Permissions {
}
})
.collect()
- },
- )?,
+ })
+ .unwrap_or_else(|| Ok(HashSet::new()))?,
prompt,
..Default::default()
})
@@ -1638,9 +1642,9 @@ impl Permissions {
) -> Result<UnaryPermission<RunDescriptor>, AnyError> {
Ok(UnaryPermission::<RunDescriptor> {
global_state: global_state_from_option(state),
- granted_list: state.as_ref().map_or_else(
- || Ok(HashSet::new()),
- |v| {
+ granted_list: state
+ .as_ref()
+ .map(|v| {
v.iter()
.map(|x| {
if x.is_empty() {
@@ -1650,8 +1654,8 @@ impl Permissions {
}
})
.collect()
- },
- )?,
+ })
+ .unwrap_or_else(|| Ok(HashSet::new()))?,
prompt,
..Default::default()
})