diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-03-15 17:46:36 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-15 17:46:36 -0400 |
commit | fb021d7ceff3f8b1d7cdb0c2bdd75ea07c0428d2 (patch) | |
tree | 09cb2bf87bba760b1abf706e0b8faedc9c368bbc /cli/lsp | |
parent | ca51f4f6c058d16ac438ad75ac92e8954895f5aa (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 'cli/lsp')
-rw-r--r-- | cli/lsp/capabilities.rs | 3 | ||||
-rw-r--r-- | cli/lsp/documents.rs | 19 | ||||
-rw-r--r-- | cli/lsp/language_server.rs | 26 | ||||
-rw-r--r-- | cli/lsp/path_to_regex.rs | 29 | ||||
-rw-r--r-- | cli/lsp/registries.rs | 40 | ||||
-rw-r--r-- | cli/lsp/tsc.rs | 13 |
6 files changed, 67 insertions, 63 deletions
diff --git a/cli/lsp/capabilities.rs b/cli/lsp/capabilities.rs index 029c5c9a9..e56aa6b87 100644 --- a/cli/lsp/capabilities.rs +++ b/cli/lsp/capabilities.rs @@ -19,7 +19,7 @@ fn code_action_capabilities( .as_ref() .and_then(|it| it.code_action.as_ref()) .and_then(|it| it.code_action_literal_support.as_ref()) - .map_or(CodeActionProviderCapability::Simple(true), |_| { + .map(|_| { let mut code_action_kinds = vec![CodeActionKind::QUICKFIX, CodeActionKind::REFACTOR]; code_action_kinds.extend( @@ -34,6 +34,7 @@ fn code_action_capabilities( work_done_progress_options: Default::default(), }) }) + .unwrap_or(CodeActionProviderCapability::Simple(true)) } pub fn server_capabilities( diff --git a/cli/lsp/documents.rs b/cli/lsp/documents.rs index 9426378d5..9299c662c 100644 --- a/cli/lsp/documents.rs +++ b/cli/lsp/documents.rs @@ -490,7 +490,8 @@ impl Document { pub fn script_version(&self) -> String { self .maybe_lsp_version() - .map_or_else(|| self.fs_version().to_string(), |v| v.to_string()) + .map(|v| v.to_string()) + .unwrap_or_else(|| self.fs_version().to_string()) } pub fn is_diagnosable(&self) -> bool { @@ -901,15 +902,13 @@ impl Documents { let mut file_system_docs = self.file_system_docs.lock(); file_system_docs.docs.remove(specifier) }) - .map_or_else( - || { - Err(custom_error( - "NotFound", - format!("The specifier \"{specifier}\" was not found."), - )) - }, - Ok, - )?; + .map(Ok) + .unwrap_or_else(|| { + Err(custom_error( + "NotFound", + format!("The specifier \"{specifier}\" was not found."), + )) + })?; self.dirty = true; let doc = doc.with_change(version, changes, self.get_resolver())?; self.open_docs.insert(doc.specifier().clone(), doc.clone()); diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index fc87001af..8a779d283 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -509,14 +509,14 @@ impl Inner { &self, specifier: &ModuleSpecifier, ) -> LspResult<AssetOrDocument> { - self.get_maybe_asset_or_document(specifier).map_or_else( - || { + self + .get_maybe_asset_or_document(specifier) + .map(Ok) + .unwrap_or_else(|| { Err(LspError::invalid_params(format!( "Unable to find asset or document for: {specifier}" ))) - }, - Ok, - ) + }) } /// Searches assets and documents for the provided specifier. @@ -1677,16 +1677,12 @@ impl Inner { // Refactor let start = line_index.offset_tsc(params.range.start)?; let length = line_index.offset_tsc(params.range.end)? - start; - let only = - params - .context - .only - .as_ref() - .map_or(String::default(), |values| { - values - .first() - .map_or(String::default(), |v| v.as_str().to_owned()) - }); + let only = params + .context + .only + .as_ref() + .and_then(|values| values.first().map(|v| v.as_str().to_owned())) + .unwrap_or_default(); let req = tsc::RequestMethod::GetApplicableRefactors(( specifier.clone(), tsc::TextSpan { start, length }, diff --git a/cli/lsp/path_to_regex.rs b/cli/lsp/path_to_regex.rs index 1d766e024..dcad6b039 100644 --- a/cli/lsp/path_to_regex.rs +++ b/cli/lsp/path_to_regex.rs @@ -452,14 +452,11 @@ pub fn parse( path = String::new(); } - let name = name.map_or_else( - || { - let default = StringOrNumber::Number(key); - key += 1; - default - }, - StringOrNumber::String, - ); + let name = name.map(StringOrNumber::String).unwrap_or_else(|| { + let default = StringOrNumber::Number(key); + key += 1; + default + }); let prefix = if prefix.is_empty() { None } else { @@ -498,8 +495,10 @@ pub fn parse( must_consume(&TokenType::Close, &mut tokens)?; - let name = maybe_name.clone().map_or_else( - || { + let name = maybe_name + .clone() + .map(StringOrNumber::String) + .unwrap_or_else(|| { if maybe_pattern.is_some() { let default = StringOrNumber::Number(key); key += 1; @@ -507,9 +506,7 @@ pub fn parse( } else { StringOrNumber::String("".to_string()) } - }, - StringOrNumber::String, - ); + }); let pattern = if maybe_name.is_some() && maybe_pattern.is_none() { default_pattern.clone() } else { @@ -570,11 +567,13 @@ pub fn tokens_to_regex( let prefix = key .prefix .clone() - .map_or_else(|| "".to_string(), |s| escape_string(&s)); + .map(|s| escape_string(&s)) + .unwrap_or_default(); let suffix = key .suffix .clone() - .map_or_else(|| "".to_string(), |s| escape_string(&s)); + .map(|s| escape_string(&s)) + .unwrap_or_default(); if !key.pattern.is_empty() { if !prefix.is_empty() || !suffix.is_empty() { diff --git a/cli/lsp/registries.rs b/cli/lsp/registries.rs index fb60c7626..c1fa815e9 100644 --- a/cli/lsp/registries.rs +++ b/cli/lsp/registries.rs @@ -295,18 +295,20 @@ fn validate_config(config: &RegistryConfigurationJson) -> Result<(), AnyError> { } for registry in &config.registries { let (_, keys) = string_to_regex(®istry.schema, None)?; - let key_names: Vec<String> = keys.map_or_else(Vec::new, |keys| { - keys - .iter() - .filter_map(|k| { - if let StringOrNumber::String(s) = &k.name { - Some(s.clone()) - } else { - None - } - }) - .collect() - }); + let key_names: Vec<String> = keys + .map(|keys| { + keys + .iter() + .filter_map(|k| { + if let StringOrNumber::String(s) = &k.name { + Some(s.clone()) + } else { + None + } + }) + .collect() + }) + .unwrap_or_default(); for key_name in &key_names { if !registry @@ -664,18 +666,20 @@ impl ModuleRegistry { }) .ok()?; let mut i = tokens.len(); - let last_key_name = - StringOrNumber::String(tokens.iter().last().map_or_else( - || "".to_string(), - |t| { + let last_key_name = StringOrNumber::String( + tokens + .iter() + .last() + .map(|t| { if let Token::Key(key) = t { if let StringOrNumber::String(s) = &key.name { return s.clone(); } } "".to_string() - }, - )); + }) + .unwrap_or_default(), + ); loop { let matcher = Matcher::new(&tokens[..i], None) .map_err(|e| { diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index 79c1817e2..335d91ed7 100644 --- a/cli/lsp/tsc.rs +++ b/cli/lsp/tsc.rs @@ -1024,15 +1024,19 @@ impl NavigationTree { ) -> bool { let mut should_include = self.should_include_entry(); if !should_include - && self.child_items.as_ref().map_or(true, |v| v.is_empty()) + && self + .child_items + .as_ref() + .map(|v| v.is_empty()) + .unwrap_or(true) { return false; } let children = self .child_items - .as_ref() - .map_or(&[] as &[NavigationTree], |v| v.as_slice()); + .as_deref() + .unwrap_or(&[] as &[NavigationTree]); for span in self.spans.iter() { let range = TextRange::at(span.start.into(), span.length.into()); let mut symbol_children = Vec::<lsp::DocumentSymbol>::new(); @@ -1514,7 +1518,8 @@ impl RefactorActionInfo { .iter() .find(|action| action.matches(&self.name)); maybe_match - .map_or(lsp::CodeActionKind::REFACTOR, |action| action.kind.clone()) + .map(|action| action.kind.clone()) + .unwrap_or(lsp::CodeActionKind::REFACTOR) } } |