Fix clippy::or_fun_call

This commit is contained in:
Alan Du 2019-06-03 10:27:51 -04:00
parent 40424d4222
commit b28ca32db2
12 changed files with 29 additions and 26 deletions

View File

@ -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);
}

View File

@ -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<impl HirDatabase>, vis: &ast::Visibility) -> Option<Assist> {

View File

@ -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));
}
}

View File

@ -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())
}

View File

@ -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,
})
}

View File

@ -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<Ty> = None;
let krate = resolver.krate()?;

View File

@ -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,
};

View File

@ -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();

View File

@ -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)
})?;

View File

@ -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 `{}`",

View File

@ -125,8 +125,8 @@ fn parse_repeat(p: &mut TtCursor, transcriber: bool) -> Result<crate::Repeat, Pa
}
}
let sep = p.eat_seperator().ok_or(ParseError::Expected(String::from("separator")))?;
let rep = p.eat_punct().ok_or(ParseError::Expected(String::from("repeat")))?;
let sep = p.eat_seperator().ok_or_else(|| ParseError::Expected(String::from("separator")))?;
let rep = p.eat_punct().ok_or_else(|| ParseError::Expected(String::from("repeat")))?;
mk_repeat(rep.char, subtree, Some(sep))
}

View File

@ -255,7 +255,7 @@ where
let first_non_space = str
.bytes()
.position(|b| b != b' ' && b != b'\t' && b != b'\n' && b != b'\r')
.unwrap_or(str.len());
.unwrap_or_else(|| str.len());
*chars = str[first_non_space..].chars()
}
}