From b28ca32db22d5e2ed34db556c6fd50a5fc2d679c Mon Sep 17 00:00:00 2001 From: Alan Du Date: Mon, 3 Jun 2019 10:27:51 -0400 Subject: Fix clippy::or_fun_call --- crates/ra_assists/src/auto_import.rs | 4 ++-- crates/ra_assists/src/change_visibility.rs | 2 +- crates/ra_hir/src/lang_item.rs | 2 +- crates/ra_hir/src/nameres.rs | 17 +++++++++++------ crates/ra_hir/src/ty.rs | 2 +- crates/ra_hir/src/ty/infer.rs | 2 +- crates/ra_ide_api/src/completion/complete_pattern.rs | 2 +- crates/ra_ide_api/src/extend_selection.rs | 2 +- crates/ra_lsp_server/src/main.rs | 2 +- crates/ra_mbe/src/mbe_expander.rs | 14 ++++++-------- crates/ra_mbe/src/mbe_parser.rs | 4 ++-- crates/ra_syntax/src/validation/unescape.rs | 2 +- 12 files changed, 29 insertions(+), 26 deletions(-) (limited to 'crates') diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs index 1566cf179..9617beb5c 100644 --- a/crates/ra_assists/src/auto_import.rs +++ b/crates/ra_assists/src/auto_import.rs @@ -334,7 +334,7 @@ fn best_action_for_target<'b, 'a: 'b>( .filter_map(ast::UseItem::use_tree) .map(|u| walk_use_tree_for_best_action(&mut storage, None, u, target)) .fold(None, |best, a| { - best.and_then(|best| Some(*ImportAction::better(&best, &a))).or(Some(a)) + best.and_then(|best| Some(*ImportAction::better(&best, &a))).or_else(|| Some(a)) }); match best_action { @@ -347,7 +347,7 @@ fn best_action_for_target<'b, 'a: 'b>( let anchor = container .children() .find(|n| n.range().start() < anchor.range().start()) - .or(Some(anchor)); + .or_else(|| Some(anchor)); return ImportAction::add_new_use(anchor, false); } diff --git a/crates/ra_assists/src/change_visibility.rs b/crates/ra_assists/src/change_visibility.rs index 620f534b5..4eb24d27f 100644 --- a/crates/ra_assists/src/change_visibility.rs +++ b/crates/ra_assists/src/change_visibility.rs @@ -59,7 +59,7 @@ fn vis_offset(node: &SyntaxNode) -> TextUnit { }) .next() .map(|it| it.range().start()) - .unwrap_or(node.range().start()) + .unwrap_or_else(|| node.range().start()) } fn change_vis(mut ctx: AssistCtx, vis: &ast::Visibility) -> Option { diff --git a/crates/ra_hir/src/lang_item.rs b/crates/ra_hir/src/lang_item.rs index 4cacaeba4..684fbc068 100644 --- a/crates/ra_hir/src/lang_item.rs +++ b/crates/ra_hir/src/lang_item.rs @@ -95,7 +95,7 @@ impl LangItems { .nth(0); if let Some(lang_item_name) = lang_item_name { let imp = ImplBlock::from_id(*module, impl_id); - self.items.entry(lang_item_name).or_insert(LangItemTarget::ImplBlock(imp)); + self.items.entry(lang_item_name).or_insert_with(|| LangItemTarget::ImplBlock(imp)); } } diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index d822f7d93..7bddee9b9 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -332,7 +332,8 @@ impl CrateDefMap { let name = path.expand_macro_expr()?; // search local first // FIXME: Remove public_macros check when we have a correct local_macors implementation - let local = self.public_macros.get(&name).or(self.local_macros.get(&name)).map(|it| *it); + let local = + self.public_macros.get(&name).or_else(|| self.local_macros.get(&name)).map(|it| *it); if local.is_some() { return local; } @@ -479,8 +480,10 @@ impl CrateDefMap { } fn resolve_name_in_crate_root_or_extern_prelude(&self, name: &Name) -> ItemOrMacro { - let from_crate_root = - self[self.root].scope.get_item_or_macro(name).unwrap_or(Either::Left(PerNs::none())); + let from_crate_root = self[self.root] + .scope + .get_item_or_macro(name) + .unwrap_or_else(|| Either::Left(PerNs::none())); let from_extern_prelude = self.resolve_name_in_extern_prelude(name); or(from_crate_root, Either::Left(from_extern_prelude)) @@ -505,8 +508,10 @@ impl CrateDefMap { // - current module / scope // - extern prelude // - std prelude - let from_scope = - self[module].scope.get_item_or_macro(name).unwrap_or(Either::Left(PerNs::none()));; + let from_scope = self[module] + .scope + .get_item_or_macro(name) + .unwrap_or_else(|| Either::Left(PerNs::none()));; let from_extern_prelude = self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)); let from_prelude = self.resolve_in_prelude(db, name); @@ -525,7 +530,7 @@ impl CrateDefMap { } else { db.crate_def_map(prelude.krate)[prelude.module_id].scope.get_item_or_macro(name) }; - resolution.unwrap_or(Either::Left(PerNs::none())) + resolution.unwrap_or_else(|| Either::Left(PerNs::none())) } else { Either::Left(PerNs::none()) } diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 76d34c12b..4a37e0268 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -451,7 +451,7 @@ impl Ty { /// Substitutes `Ty::Bound` vars (as opposed to type parameters). pub fn subst_bound_vars(self, substs: &Substs) -> Ty { self.fold(&mut |ty| match ty { - Ty::Bound(idx) => substs.get(idx as usize).cloned().unwrap_or(Ty::Bound(idx)), + Ty::Bound(idx) => substs.get(idx as usize).cloned().unwrap_or_else(|| Ty::Bound(idx)), ty => ty, }) } diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 5edc59f18..905fe9f0e 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -462,7 +462,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let mut resolved = if remaining_index.is_none() { def.take_values()? } else { def.take_types()? }; - let remaining_index = remaining_index.unwrap_or(path.segments.len()); + let remaining_index = remaining_index.unwrap_or_else(|| path.segments.len()); let mut actual_def_ty: Option = None; let krate = resolver.krate()?; diff --git a/crates/ra_ide_api/src/completion/complete_pattern.rs b/crates/ra_ide_api/src/completion/complete_pattern.rs index 74833a756..6655a05a7 100644 --- a/crates/ra_ide_api/src/completion/complete_pattern.rs +++ b/crates/ra_ide_api/src/completion/complete_pattern.rs @@ -10,7 +10,7 @@ pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { let names = ctx.analyzer.all_names(ctx.db); for (name, res) in names.into_iter() { let r = res.as_ref(); - let def = match r.take_types().or(r.take_values()) { + let def = match r.take_types().or_else(|| r.take_values()) { Some(hir::Resolution::Def(def)) => def, _ => continue, }; diff --git a/crates/ra_ide_api/src/extend_selection.rs b/crates/ra_ide_api/src/extend_selection.rs index 00c445310..a713b762c 100644 --- a/crates/ra_ide_api/src/extend_selection.rs +++ b/crates/ra_ide_api/src/extend_selection.rs @@ -95,7 +95,7 @@ fn extend_single_word_in_comment_or_string( } let start_idx = before.rfind(non_word_char)? as u32; - let end_idx = after.find(non_word_char).unwrap_or(after.len()) as u32; + let end_idx = after.find(non_word_char).unwrap_or_else(|| after.len()) as u32; let from: TextUnit = (start_idx + 1).into(); let to: TextUnit = (cursor_position + end_idx).into(); diff --git a/crates/ra_lsp_server/src/main.rs b/crates/ra_lsp_server/src/main.rs index b0b70df5c..a0df32dde 100644 --- a/crates/ra_lsp_server/src/main.rs +++ b/crates/ra_lsp_server/src/main.rs @@ -49,7 +49,7 @@ fn main_inner() -> Result<()> { let opts = params .initialization_options .and_then(|v| InitializationOptions::deserialize(v).ok()) - .unwrap_or(InitializationOptions::default()); + .unwrap_or_default(); ra_lsp_server::main_loop(workspace_roots, opts, r, s) })?; diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index 7cfb47f7a..55a6ecf58 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -105,17 +105,15 @@ impl Bindings { } fn get(&self, name: &SmolStr, nesting: &[usize]) -> Result<&tt::TokenTree, ExpandError> { - let mut b = self - .inner - .get(name) - .ok_or(ExpandError::BindingError(format!("could not find binding `{}`", name)))?; + let mut b = self.inner.get(name).ok_or_else(|| { + ExpandError::BindingError(format!("could not find binding `{}`", name)) + })?; for &idx in nesting.iter() { b = match b { Binding::Simple(_) => break, - Binding::Nested(bs) => bs.get(idx).ok_or(ExpandError::BindingError(format!( - "could not find nested binding `{}`", - name - )))?, + Binding::Nested(bs) => bs.get(idx).ok_or_else(|| { + ExpandError::BindingError(format!("could not find nested binding `{}`", name)) + })?, Binding::Empty => { return Err(ExpandError::BindingError(format!( "could not find empty binding `{}`", diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs index d8fe293c7..dca16b537 100644 --- a/crates/ra_mbe/src/mbe_parser.rs +++ b/crates/ra_mbe/src/mbe_parser.rs @@ -125,8 +125,8 @@ fn parse_repeat(p: &mut TtCursor, transcriber: bool) -> Result