Replace if let with match where appropriate

This commit is contained in:
Aramis Razzaghipour 2021-10-03 23:53:01 +11:00
parent f29796da61
commit 9583dd5725
No known key found for this signature in database
GPG key ID: F788F7E990136003
44 changed files with 201 additions and 269 deletions

View file

@ -2119,10 +2119,9 @@ impl Impl {
}; };
let fp = TyFingerprint::for_inherent_impl(&ty); let fp = TyFingerprint::for_inherent_impl(&ty);
let fp = if let Some(fp) = fp { let fp = match fp {
fp Some(fp) => fp,
} else { None => return Vec::new(),
return Vec::new();
}; };
let mut all = Vec::new(); let mut all = Vec::new();

View file

@ -474,10 +474,9 @@ impl ExprCollector<'_> {
} }
ast::Expr::PrefixExpr(e) => { ast::Expr::PrefixExpr(e) => {
let expr = self.collect_expr_opt(e.expr()); let expr = self.collect_expr_opt(e.expr());
if let Some(op) = e.op_kind() { match e.op_kind() {
self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr) Some(op) => self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr),
} else { None => self.alloc_expr(Expr::Missing, syntax_ptr),
self.alloc_expr(Expr::Missing, syntax_ptr)
} }
} }
ast::Expr::ClosureExpr(e) => { ast::Expr::ClosureExpr(e) => {
@ -624,10 +623,9 @@ impl ExprCollector<'_> {
} }
fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId { fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId {
if let Some(expr) = expr { match expr {
self.collect_expr(expr) Some(expr) => self.collect_expr(expr),
} else { None => self.missing_expr(),
self.missing_expr()
} }
} }
@ -724,10 +722,9 @@ impl ExprCollector<'_> {
} }
fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId { fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId {
if let Some(block) = expr { match expr {
self.collect_block(block) Some(block) => self.collect_block(block),
} else { None => self.missing_expr(),
self.missing_expr()
} }
} }
@ -890,10 +887,9 @@ impl ExprCollector<'_> {
} }
fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId { fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId {
if let Some(pat) = pat { match pat {
self.collect_pat(pat) Some(pat) => self.collect_pat(pat),
} else { None => self.missing_pat(),
self.missing_pat()
} }
} }

View file

@ -209,10 +209,9 @@ fn find_path_inner(
) { ) {
path.push_segment(name); path.push_segment(name);
let new_path = if let Some(best_path) = best_path { let new_path = match best_path {
select_best_path(best_path, path, prefer_no_std) Some(best_path) => select_best_path(best_path, path, prefer_no_std),
} else { None => path,
path
}; };
best_path_len = new_path.len(); best_path_len = new_path.len();
best_path = Some(new_path); best_path = Some(new_path);
@ -243,10 +242,9 @@ fn find_path_inner(
}); });
for path in extern_paths { for path in extern_paths {
let new_path = if let Some(best_path) = best_path { let new_path = match best_path {
select_best_path(best_path, path, prefer_no_std) Some(best_path) => select_best_path(best_path, path, prefer_no_std),
} else { None => path,
path
}; };
best_path = Some(new_path); best_path = Some(new_path);
} }
@ -261,12 +259,11 @@ fn find_path_inner(
} }
} }
if let Some(prefix) = prefixed.map(PrefixKind::prefix) { match prefixed.map(PrefixKind::prefix) {
best_path.or_else(|| { Some(prefix) => best_path.or_else(|| {
scope_name.map(|scope_name| ModPath::from_segments(prefix, vec![scope_name])) scope_name.map(|scope_name| ModPath::from_segments(prefix, vec![scope_name]))
}) }),
} else { None => best_path,
best_path
} }
} }
@ -346,15 +343,13 @@ fn find_local_import_locations(
if let Some((name, vis)) = data.scope.name_of(item) { if let Some((name, vis)) = data.scope.name_of(item) {
if vis.is_visible_from(db, from) { if vis.is_visible_from(db, from) {
let is_private = if let Visibility::Module(private_to) = vis { let is_private = match vis {
private_to.local_id == module.local_id Visibility::Module(private_to) => private_to.local_id == module.local_id,
} else { Visibility::Public => false,
false
}; };
let is_original_def = if let Some(module_def_id) = item.as_module_def_id() { let is_original_def = match item.as_module_def_id() {
data.scope.declarations().any(|it| it == module_def_id) Some(module_def_id) => data.scope.declarations().any(|it| it == module_def_id),
} else { None => false,
false
}; };
// Ignore private imports. these could be used if we are // Ignore private imports. these could be used if we are

View file

@ -475,10 +475,9 @@ macro_rules! mod_items {
} }
fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> { fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> {
if let ModItem::$typ(id) = mod_item { match mod_item {
Some(id) ModItem::$typ(id) => Some(id),
} else { _ => None,
None
} }
} }

View file

@ -400,13 +400,10 @@ impl DefMap {
}; };
let from_scope_or_builtin = match shadow { let from_scope_or_builtin = match shadow {
BuiltinShadowMode::Module => from_scope.or(from_builtin), BuiltinShadowMode::Module => from_scope.or(from_builtin),
BuiltinShadowMode::Other => { BuiltinShadowMode::Other => match from_scope.take_types() {
if let Some(ModuleDefId::ModuleId(_)) = from_scope.take_types() { Some(ModuleDefId::ModuleId(_)) => from_builtin.or(from_scope),
from_builtin.or(from_scope) Some(_) | None => from_scope.or(from_builtin),
} else { },
from_scope.or(from_builtin)
}
}
}; };
let from_extern_prelude = self let from_extern_prelude = self
.extern_prelude .extern_prelude

View file

@ -18,10 +18,9 @@ pub(crate) fn convert_path(
path: ast::Path, path: ast::Path,
hygiene: &Hygiene, hygiene: &Hygiene,
) -> Option<ModPath> { ) -> Option<ModPath> {
let prefix = if let Some(qual) = path.qualifier() { let prefix = match path.qualifier() {
Some(convert_path(db, prefix, qual, hygiene)?) Some(qual) => Some(convert_path(db, prefix, qual, hygiene)?),
} else { None => prefix,
prefix
}; };
let segment = path.segment()?; let segment = path.segment()?;

View file

@ -214,10 +214,9 @@ impl TypeRef {
} }
pub(crate) fn from_ast_opt(ctx: &LowerCtx, node: Option<ast::Type>) -> Self { pub(crate) fn from_ast_opt(ctx: &LowerCtx, node: Option<ast::Type>) -> Self {
if let Some(node) = node { match node {
TypeRef::from_ast(ctx, node) Some(node) => TypeRef::from_ast(ctx, node),
} else { None => TypeRef::Error,
TypeRef::Error
} }
} }

View file

@ -48,10 +48,9 @@ impl Name {
/// Resolve a name from the text of token. /// Resolve a name from the text of token.
fn resolve(raw_text: &str) -> Name { fn resolve(raw_text: &str) -> Name {
if let Some(text) = raw_text.strip_prefix("r#") { match raw_text.strip_prefix("r#") {
Name::new_text(SmolStr::new(text)) Some(text) => Name::new_text(SmolStr::new(text)),
} else { None => Name::new_text(raw_text.into()),
Name::new_text(raw_text.into())
} }
} }

View file

@ -109,10 +109,9 @@ pub(crate) fn deref(
ty: InEnvironment<&Canonical<Ty>>, ty: InEnvironment<&Canonical<Ty>>,
) -> Option<Canonical<Ty>> { ) -> Option<Canonical<Ty>> {
let _p = profile::span("deref"); let _p = profile::span("deref");
if let Some(derefed) = builtin_deref(&ty.goal.value) { match builtin_deref(&ty.goal.value) {
Some(Canonical { value: derefed, binders: ty.goal.binders.clone() }) Some(derefed) => Some(Canonical { value: derefed, binders: ty.goal.binders.clone() }),
} else { None => deref_by_trait(db, krate, ty),
deref_by_trait(db, krate, ty)
} }
} }

View file

@ -104,10 +104,9 @@ impl TyExt for Ty {
} }
fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId> { fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId> {
if let Some(CallableDefId::FunctionId(func)) = self.callable_def(db) { match self.callable_def(db) {
Some(func) Some(CallableDefId::FunctionId(func)) => Some(func),
} else { Some(CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_)) | None => None,
None
} }
} }
fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> { fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> {

View file

@ -105,10 +105,9 @@ impl IntRange {
#[inline] #[inline]
fn from_range(lo: u128, hi: u128, scalar_ty: Scalar) -> IntRange { fn from_range(lo: u128, hi: u128, scalar_ty: Scalar) -> IntRange {
if let Scalar::Bool = scalar_ty { match scalar_ty {
IntRange { range: lo..=hi } Scalar::Bool => IntRange { range: lo..=hi },
} else { _ => unimplemented!(),
unimplemented!()
} }
} }

View file

@ -167,10 +167,9 @@ impl<'a> HirFormatter<'a> {
} }
pub fn should_truncate(&self) -> bool { pub fn should_truncate(&self) -> bool {
if let Some(max_size) = self.max_size { match self.max_size {
self.curr_size >= max_size Some(max_size) => self.curr_size >= max_size,
} else { None => false,
false
} }
} }

View file

@ -264,10 +264,9 @@ impl<'a> InferenceContext<'a> {
// collect explicitly written argument types // collect explicitly written argument types
for arg_type in arg_types.iter() { for arg_type in arg_types.iter() {
let arg_ty = if let Some(type_ref) = arg_type { let arg_ty = match arg_type {
self.make_ty(type_ref) Some(type_ref) => self.make_ty(type_ref),
} else { None => self.table.new_type_var(),
self.table.new_type_var()
}; };
sig_tys.push(arg_ty); sig_tys.push(arg_ty);
} }

View file

@ -204,10 +204,9 @@ impl<'a> InferenceContext<'a> {
} else { } else {
BindingMode::convert(*mode) BindingMode::convert(*mode)
}; };
let inner_ty = if let Some(subpat) = subpat { let inner_ty = match subpat {
self.infer_pat(*subpat, &expected, default_bm) Some(subpat) => self.infer_pat(*subpat, &expected, default_bm),
} else { None => expected,
expected
}; };
let inner_ty = self.insert_type_vars_shallow(inner_ty); let inner_ty = self.insert_type_vars_shallow(inner_ty);

View file

@ -324,10 +324,9 @@ impl<'a> InferenceTable<'a> {
/// Unify two types and register new trait goals that arise from that. /// Unify two types and register new trait goals that arise from that.
pub(crate) fn unify(&mut self, ty1: &Ty, ty2: &Ty) -> bool { pub(crate) fn unify(&mut self, ty1: &Ty, ty2: &Ty) -> bool {
let result = if let Ok(r) = self.try_unify(ty1, ty2) { let result = match self.try_unify(ty1, ty2) {
r Ok(r) => r,
} else { Err(_) => return false,
return false;
}; };
self.register_infer_ok(result); self.register_infer_ok(result);
true true

View file

@ -368,10 +368,9 @@ impl<'a> TyLoweringContext<'a> {
Some((it, None)) => it, Some((it, None)) => it,
_ => return None, _ => return None,
}; };
if let TypeNs::GenericParam(param_id) = resolution { match resolution {
Some(param_id) TypeNs::GenericParam(param_id) => Some(param_id),
} else { _ => None,
None
} }
} }

View file

@ -82,10 +82,9 @@ impl TyFingerprint {
TyKind::Ref(_, _, ty) => return TyFingerprint::for_trait_impl(ty), TyKind::Ref(_, _, ty) => return TyFingerprint::for_trait_impl(ty),
TyKind::Tuple(_, subst) => { TyKind::Tuple(_, subst) => {
let first_ty = subst.interned().get(0).map(|arg| arg.assert_ty_ref(&Interner)); let first_ty = subst.interned().get(0).map(|arg| arg.assert_ty_ref(&Interner));
if let Some(ty) = first_ty { match first_ty {
return TyFingerprint::for_trait_impl(ty); Some(ty) => return TyFingerprint::for_trait_impl(ty),
} else { None => TyFingerprint::Unit,
TyFingerprint::Unit
} }
} }
TyKind::AssociatedType(_, _) TyKind::AssociatedType(_, _)

View file

@ -195,10 +195,9 @@ fn check_impl(ra_fixture: &str, allow_none: bool, only_types: bool, display_sour
mismatch.expected.display_test(&db), mismatch.expected.display_test(&db),
mismatch.actual.display_test(&db) mismatch.actual.display_test(&db)
); );
if let Some(annotation) = mismatches.remove(&range) { match mismatches.remove(&range) {
assert_eq!(actual, annotation); Some(annotation) => assert_eq!(actual, annotation),
} else { None => format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual),
format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual);
} }
} }
for (expr, mismatch) in inference_result.expr_type_mismatches() { for (expr, mismatch) in inference_result.expr_type_mismatches() {
@ -215,10 +214,9 @@ fn check_impl(ra_fixture: &str, allow_none: bool, only_types: bool, display_sour
mismatch.expected.display_test(&db), mismatch.expected.display_test(&db),
mismatch.actual.display_test(&db) mismatch.actual.display_test(&db)
); );
if let Some(annotation) = mismatches.remove(&range) { match mismatches.remove(&range) {
assert_eq!(actual, annotation); Some(annotation) => assert_eq!(actual, annotation),
} else { None => format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual),
format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual);
} }
} }
} }

View file

@ -292,10 +292,9 @@ impl TryToNav for hir::Impl {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
let src = self.source(db)?; let src = self.source(db)?;
let derive_attr = self.is_builtin_derive(db); let derive_attr = self.is_builtin_derive(db);
let frange = if let Some(item) = &derive_attr { let frange = match &derive_attr {
item.syntax().original_file_range(db) Some(item) => item.syntax().original_file_range(db),
} else { None => src.syntax().original_file_range(db),
src.syntax().original_file_range(db)
}; };
let focus_range = if derive_attr.is_some() { let focus_range = if derive_attr.is_some() {
None None

View file

@ -136,10 +136,9 @@ fn remove_newline(
} }
T!['}'] => { T!['}'] => {
// Removes: comma, newline (incl. surrounding whitespace) // Removes: comma, newline (incl. surrounding whitespace)
let space = if let Some(left) = prev.prev_sibling_or_token() { let space = match prev.prev_sibling_or_token() {
compute_ws(left.kind(), next.kind()) Some(left) => compute_ws(left.kind(), next.kind()),
} else { None => " ",
" "
}; };
edit.replace( edit.replace(
TextRange::new(prev.text_range().start(), token.text_range().end()), TextRange::new(prev.text_range().start(), token.text_range().end()),

View file

@ -103,10 +103,9 @@ impl StaticIndex<'_> {
for token in tokens { for token in tokens {
let range = token.text_range(); let range = token.text_range();
let node = token.parent().unwrap(); let node = token.parent().unwrap();
let def = if let Some(x) = get_definition(&sema, token.clone()) { let def = match get_definition(&sema, token.clone()) {
x Some(x) => x,
} else { None => continue,
continue;
}; };
let id = if let Some(x) = self.def_map.get(&def) { let id = if let Some(x) = self.def_map.get(&def) {
*x *x
@ -124,10 +123,9 @@ impl StaticIndex<'_> {
let token = self.tokens.get_mut(id).unwrap(); let token = self.tokens.get_mut(id).unwrap();
token.references.push(ReferenceData { token.references.push(ReferenceData {
range: FileRange { range, file_id }, range: FileRange { range, file_id },
is_definition: if let Some(x) = def.try_to_nav(self.db) { is_definition: match def.try_to_nav(self.db) {
x.file_id == file_id && x.focus_or_full_range() == range Some(x) => x.file_id == file_id && x.focus_or_full_range() == range,
} else { None => false,
false
}, },
}); });
result.tokens.push((range, id)); result.tokens.push((range, id));

View file

@ -827,10 +827,9 @@ impl FunctionBody {
locals locals
.map(|local| (local, local.source(ctx.db()))) .map(|local| (local, local.source(ctx.db())))
.filter(|(_, src)| is_defined_outside_of_body(ctx, self, src)) .filter(|(_, src)| is_defined_outside_of_body(ctx, self, src))
.filter_map(|(local, src)| { .filter_map(|(local, src)| match src.value {
if let Either::Left(src) = src.value { Either::Left(src) => Some((local, src)),
Some((local, src)) Either::Right(_) => {
} else {
stdx::never!(false, "Local::is_self returned false, but source is SelfParam"); stdx::never!(false, "Local::is_self returned false, but source is SelfParam");
None None
} }

View file

@ -69,10 +69,11 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option
None => to_extract.syntax().text_range(), None => to_extract.syntax().text_range(),
}; };
if let Anchor::WrapInBlock(_) = anchor { match anchor {
format_to!(buf, "{{ let {} = ", var_name); Anchor::Before(_) | Anchor::Replace(_) => {
} else { format_to!(buf, "let {} = ", var_name)
format_to!(buf, "let {} = ", var_name); }
Anchor::WrapInBlock(_) => format_to!(buf, "{{ let {} = ", var_name),
}; };
format_to!(buf, "{}", to_extract.syntax()); format_to!(buf, "{}", to_extract.syntax());

View file

@ -213,10 +213,9 @@ impl FunctionTemplate {
Some(cap) => { Some(cap) => {
let cursor = if self.should_focus_return_type { let cursor = if self.should_focus_return_type {
// Focus the return type if there is one // Focus the return type if there is one
if let Some(ref ret_type) = self.ret_type { match self.ret_type {
ret_type.syntax() Some(ref ret_type) => ret_type.syntax(),
} else { None => self.tail_expr.syntax(),
self.tail_expr.syntax()
} }
} else { } else {
self.tail_expr.syntax() self.tail_expr.syntax()
@ -447,10 +446,9 @@ fn fn_args(
arg_types.push(match fn_arg_type(ctx, target_module, &arg) { arg_types.push(match fn_arg_type(ctx, target_module, &arg) {
Some(ty) => { Some(ty) => {
if !ty.is_empty() && ty.starts_with('&') { if !ty.is_empty() && ty.starts_with('&') {
if let Some((new_ty, _)) = useless_type_special_case("", &ty[1..].to_owned()) { match useless_type_special_case("", &ty[1..].to_owned()) {
new_ty Some((new_ty, _)) => new_ty,
} else { None => ty,
ty
} }
} else { } else {
ty ty
@ -575,20 +573,14 @@ fn next_space_for_fn_in_module(
) -> Option<(FileId, GeneratedFunctionTarget)> { ) -> Option<(FileId, GeneratedFunctionTarget)> {
let file = module_source.file_id.original_file(db); let file = module_source.file_id.original_file(db);
let assist_item = match &module_source.value { let assist_item = match &module_source.value {
hir::ModuleSource::SourceFile(it) => { hir::ModuleSource::SourceFile(it) => match it.items().last() {
if let Some(last_item) = it.items().last() { Some(last_item) => GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()),
GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()) None => GeneratedFunctionTarget::BehindItem(it.syntax().clone()),
} else { },
GeneratedFunctionTarget::BehindItem(it.syntax().clone()) hir::ModuleSource::Module(it) => match it.item_list().and_then(|it| it.items().last()) {
} Some(last_item) => GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()),
} None => GeneratedFunctionTarget::InEmptyItemList(it.item_list()?.syntax().clone()),
hir::ModuleSource::Module(it) => { },
if let Some(last_item) = it.item_list().and_then(|it| it.items().last()) {
GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
} else {
GeneratedFunctionTarget::InEmptyItemList(it.item_list()?.syntax().clone())
}
}
hir::ModuleSource::BlockExpr(it) => { hir::ModuleSource::BlockExpr(it) => {
if let Some(last_item) = if let Some(last_item) =
it.statements().take_while(|stmt| matches!(stmt, ast::Stmt::Item(_))).last() it.statements().take_while(|stmt| matches!(stmt, ast::Stmt::Item(_))).last()

View file

@ -141,10 +141,9 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext) -> Opt
for (file_id, refs) in usages.into_iter() { for (file_id, refs) in usages.into_iter() {
inline_refs_for_file(file_id, refs); inline_refs_for_file(file_id, refs);
} }
if let Some(refs) = current_file_usage { match current_file_usage {
inline_refs_for_file(def_file, refs); Some(refs) => inline_refs_for_file(def_file, refs),
} else { None => builder.edit_file(def_file),
builder.edit_file(def_file);
} }
if remove_def { if remove_def {
builder.delete(ast_func.syntax().text_range()); builder.delete(ast_func.syntax().text_range());

View file

@ -127,12 +127,9 @@ impl<'a> AssignmentsCollector<'a> {
} }
} }
fn collect_block(&mut self, block: &ast::BlockExpr) -> Option<()> { fn collect_block(&mut self, block: &ast::BlockExpr) -> Option<()> {
let last_expr = block.tail_expr().or_else(|| { let last_expr = block.tail_expr().or_else(|| match block.statements().last()? {
if let ast::Stmt::ExprStmt(stmt) = block.statements().last()? { ast::Stmt::ExprStmt(stmt) => stmt.expr(),
stmt.expr() ast::Stmt::Item(_) | ast::Stmt::LetStmt(_) => None,
} else {
None
}
})?; })?;
if let ast::Expr::BinExpr(expr) = last_expr { if let ast::Expr::BinExpr(expr) = last_expr {

View file

@ -181,10 +181,9 @@ fn find_trait_method(
fn item_as_trait(db: &RootDatabase, item: hir::ItemInNs) -> Option<hir::Trait> { fn item_as_trait(db: &RootDatabase, item: hir::ItemInNs) -> Option<hir::Trait> {
let item_module_def = item.as_module_def()?; let item_module_def = item.as_module_def()?;
if let hir::ModuleDef::Trait(trait_) = item_module_def { match item_module_def {
Some(trait_) hir::ModuleDef::Trait(trait_) => Some(trait_),
} else { _ => item_module_def.as_assoc_item(db)?.containing_trait(db),
item_module_def.as_assoc_item(db)?.containing_trait(db)
} }
} }

View file

@ -250,13 +250,10 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
}; };
Some(make::expr_method_call(receiver, make::name_ref(method), arg_list)) Some(make::expr_method_call(receiver, make::name_ref(method), arg_list))
} }
ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::UnaryOp::Not => { ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::UnaryOp::Not => match pe.expr()? {
if let ast::Expr::ParenExpr(parexpr) = pe.expr()? { ast::Expr::ParenExpr(parexpr) => parexpr.expr(),
parexpr.expr() _ => pe.expr(),
} else { },
pe.expr()
}
}
ast::Expr::Literal(lit) => match lit.kind() { ast::Expr::Literal(lit) => match lit.kind() {
ast::LiteralKind::Bool(b) => match b { ast::LiteralKind::Bool(b) => match b {
true => Some(ast::Expr::Literal(make::expr_literal("false"))), true => Some(ast::Expr::Literal(make::expr_literal("false"))),
@ -276,13 +273,10 @@ pub(crate) fn does_pat_match_variant(pat: &ast::Pat, var: &ast::Pat) -> bool {
let first_node_text = |pat: &ast::Pat| pat.syntax().first_child().map(|node| node.text()); let first_node_text = |pat: &ast::Pat| pat.syntax().first_child().map(|node| node.text());
let pat_head = match pat { let pat_head = match pat {
ast::Pat::IdentPat(bind_pat) => { ast::Pat::IdentPat(bind_pat) => match bind_pat.pat() {
if let Some(p) = bind_pat.pat() { Some(p) => first_node_text(&p),
first_node_text(&p) None => return pat.syntax().text() == var.syntax().text(),
} else { },
return pat.syntax().text() == var.syntax().text();
}
}
pat => first_node_text(pat), pat => first_node_text(pat),
}; };

View file

@ -144,10 +144,9 @@ fn is_valid_name(name: &str) -> bool {
fn is_useless_method(method: &ast::MethodCallExpr) -> bool { fn is_useless_method(method: &ast::MethodCallExpr) -> bool {
let ident = method.name_ref().and_then(|it| it.ident_token()); let ident = method.name_ref().and_then(|it| it.ident_token());
if let Some(ident) = ident { match ident {
USELESS_METHODS.contains(&ident.text()) Some(ident) => USELESS_METHODS.contains(&ident.text()),
} else { None => false,
false
} }
} }

View file

@ -509,10 +509,9 @@ impl<'a> CompletionContext<'a> {
.and_then(|pat| self.sema.type_of_pat(&pat)) .and_then(|pat| self.sema.type_of_pat(&pat))
.or_else(|| it.initializer().and_then(|it| self.sema.type_of_expr(&it))) .or_else(|| it.initializer().and_then(|it| self.sema.type_of_expr(&it)))
.map(TypeInfo::original); .map(TypeInfo::original);
let name = if let Some(ast::Pat::IdentPat(ident)) = it.pat() { let name = match it.pat() {
ident.name().map(NameOrNameRef::Name) Some(ast::Pat::IdentPat(ident)) => ident.name().map(NameOrNameRef::Name),
} else { Some(_) | None => None,
None
}; };
(ty, name) (ty, name)

View file

@ -74,10 +74,9 @@ impl<'a> FunctionRender<'a> {
fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem { fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem {
let params = self.params(); let params = self.params();
let call = if let Some(receiver) = &self.receiver { let call = match &self.receiver {
format!("{}.{}", receiver, &self.name) Some(receiver) => format!("{}.{}", receiver, &self.name),
} else { None => self.name.clone(),
self.name.clone()
}; };
let mut item = let mut item =
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), call.clone()); CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), call.clone());

View file

@ -63,10 +63,9 @@ fn build_completion(
.set_documentation(ctx.docs(def)) .set_documentation(ctx.docs(def))
.set_deprecated(ctx.is_deprecated(def)) .set_deprecated(ctx.is_deprecated(def))
.detail(&pat); .detail(&pat);
if let Some(snippet_cap) = ctx.snippet_cap() { match ctx.snippet_cap() {
item.insert_snippet(snippet_cap, pat); Some(snippet_cap) => item.insert_snippet(snippet_cap, pat),
} else { None => item.insert_text(pat),
item.insert_text(pat);
}; };
item.build() item.build()
} }

View file

@ -38,10 +38,9 @@ fn build_completion(
.set_documentation(ctx.docs(def)) .set_documentation(ctx.docs(def))
.set_deprecated(ctx.is_deprecated(def)) .set_deprecated(ctx.is_deprecated(def))
.detail(&literal); .detail(&literal);
if let Some(snippet_cap) = ctx.snippet_cap() { match ctx.snippet_cap() {
item.insert_snippet(snippet_cap, literal); Some(snippet_cap) => item.insert_snippet(snippet_cap, literal),
} else { None => item.insert_text(literal),
item.insert_text(literal);
}; };
item.build() item.build()
} }

View file

@ -47,10 +47,9 @@ impl ResolvedRule {
) -> Result<ResolvedRule, SsrError> { ) -> Result<ResolvedRule, SsrError> {
let resolver = let resolver =
Resolver { resolution_scope, placeholders_by_stand_in: rule.placeholders_by_stand_in }; Resolver { resolution_scope, placeholders_by_stand_in: rule.placeholders_by_stand_in };
let resolved_template = if let Some(template) = rule.template { let resolved_template = match rule.template {
Some(resolver.resolve_pattern_tree(template)?) Some(template) => Some(resolver.resolve_pattern_tree(template)?),
} else { None => None,
None
}; };
Ok(ResolvedRule { Ok(ResolvedRule {
pattern: resolver.resolve_pattern_tree(rule.pattern)?, pattern: resolver.resolve_pattern_tree(rule.pattern)?,

View file

@ -497,10 +497,9 @@ impl server::Literal for Rustc {
} }
fn integer(&mut self, n: &str) -> Self::Literal { fn integer(&mut self, n: &str) -> Self::Literal {
let n = if let Ok(n) = n.parse::<i128>() { let n = match n.parse::<i128>() {
n.to_string() Ok(n) => n.to_string(),
} else { Err(_) => n.parse::<u128>().unwrap().to_string(),
n.parse::<u128>().unwrap().to_string()
}; };
Literal { text: n.into(), id: tt::TokenId::unspecified() } Literal { text: n.into(), id: tt::TokenId::unspecified() }
} }

View file

@ -500,10 +500,9 @@ impl server::Literal for Rustc {
} }
fn integer(&mut self, n: &str) -> Self::Literal { fn integer(&mut self, n: &str) -> Self::Literal {
let n = if let Ok(n) = n.parse::<i128>() { let n = match n.parse::<i128>() {
n.to_string() Ok(n) => n.to_string(),
} else { Err(_) => n.parse::<u128>().unwrap().to_string(),
n.parse::<u128>().unwrap().to_string()
}; };
Literal { text: n.into(), id: tt::TokenId::unspecified() } Literal { text: n.into(), id: tt::TokenId::unspecified() }
} }

View file

@ -504,10 +504,9 @@ impl server::Literal for Rustc {
} }
fn integer(&mut self, n: &str) -> Self::Literal { fn integer(&mut self, n: &str) -> Self::Literal {
let n = if let Ok(n) = n.parse::<i128>() { let n = match n.parse::<i128>() {
n.to_string() Ok(n) => n.to_string(),
} else { Err(_) => n.parse::<u128>().unwrap().to_string(),
n.parse::<u128>().unwrap().to_string()
}; };
Literal { text: n.into(), id: tt::TokenId::unspecified() } Literal { text: n.into(), id: tt::TokenId::unspecified() }
} }

View file

@ -427,10 +427,9 @@ pub(crate) fn handle_workspace_symbol(
// If no explicit marker was set, check request params. If that's also empty // If no explicit marker was set, check request params. If that's also empty
// use global config. // use global config.
if !all_symbols { if !all_symbols {
let search_kind = if let Some(ref search_kind) = params.search_kind { let search_kind = match params.search_kind {
search_kind Some(ref search_kind) => search_kind,
} else { None => &config.search_kind,
&config.search_kind
}; };
all_symbols = match search_kind { all_symbols = match search_kind {
lsp_ext::WorkspaceSymbolSearchKind::OnlyTypes => false, lsp_ext::WorkspaceSymbolSearchKind::OnlyTypes => false,
@ -439,10 +438,9 @@ pub(crate) fn handle_workspace_symbol(
} }
if !libs { if !libs {
let search_scope = if let Some(ref search_scope) = params.search_scope { let search_scope = match params.search_scope {
search_scope Some(ref search_scope) => search_scope,
} else { None => &config.search_scope,
&config.search_scope
}; };
libs = match search_scope { libs = match search_scope {
lsp_ext::WorkspaceSymbolSearchScope::Workspace => false, lsp_ext::WorkspaceSymbolSearchScope::Workspace => false,

View file

@ -60,10 +60,9 @@ impl GenericParamsOwnerEdit for ast::Impl {
match self.generic_param_list() { match self.generic_param_list() {
Some(it) => it, Some(it) => it,
None => { None => {
let position = if let Some(imp_token) = self.impl_token() { let position = match self.impl_token() {
Position::after(imp_token) Some(imp_token) => Position::after(imp_token),
} else { None => Position::last_child_of(self.syntax()),
Position::last_child_of(self.syntax())
}; };
create_generic_param_list(position) create_generic_param_list(position)
} }
@ -72,10 +71,9 @@ impl GenericParamsOwnerEdit for ast::Impl {
fn get_or_create_where_clause(&self) -> ast::WhereClause { fn get_or_create_where_clause(&self) -> ast::WhereClause {
if self.where_clause().is_none() { if self.where_clause().is_none() {
let position = if let Some(items) = self.assoc_item_list() { let position = match self.assoc_item_list() {
Position::before(items.syntax()) Some(items) => Position::before(items.syntax()),
} else { None => Position::last_child_of(self.syntax()),
Position::last_child_of(self.syntax())
}; };
create_where_clause(position); create_where_clause(position);
} }
@ -102,10 +100,9 @@ impl GenericParamsOwnerEdit for ast::Trait {
fn get_or_create_where_clause(&self) -> ast::WhereClause { fn get_or_create_where_clause(&self) -> ast::WhereClause {
if self.where_clause().is_none() { if self.where_clause().is_none() {
let position = if let Some(items) = self.assoc_item_list() { let position = match self.assoc_item_list() {
Position::before(items.syntax()) Some(items) => Position::before(items.syntax()),
} else { None => Position::last_child_of(self.syntax()),
Position::last_child_of(self.syntax())
}; };
create_where_clause(position); create_where_clause(position);
} }
@ -253,12 +250,9 @@ impl ast::WhereClause {
impl ast::TypeBoundList { impl ast::TypeBoundList {
pub fn remove(&self) { pub fn remove(&self) {
if let Some(colon) = match self.syntax().siblings_with_tokens(Direction::Prev).find(|it| it.kind() == T![:]) {
self.syntax().siblings_with_tokens(Direction::Prev).find(|it| it.kind() == T![:]) Some(colon) => ted::remove_all(colon..=self.syntax().clone().into()),
{ None => ted::remove(self.syntax()),
ted::remove_all(colon..=self.syntax().clone().into())
} else {
ted::remove(self.syntax())
} }
} }
} }

View file

@ -641,9 +641,14 @@ pub fn fn_(
ret_type: Option<ast::RetType>, ret_type: Option<ast::RetType>,
is_async: bool, is_async: bool,
) -> ast::Fn { ) -> ast::Fn {
let type_params = let type_params = match type_params {
if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() }; Some(type_params) => format!("<{}>", type_params),
let ret_type = if let Some(ret_type) = ret_type { format!("{} ", ret_type) } else { "".into() }; None => "".into(),
};
let ret_type = match ret_type {
Some(ret_type) => format!("{} ", ret_type),
None => "".into(),
};
let visibility = match visibility { let visibility = match visibility {
None => String::new(), None => String::new(),
Some(it) => format!("{} ", it), Some(it) => format!("{} ", it),

View file

@ -549,10 +549,9 @@ impl ast::FieldExpr {
} }
pub fn field_access(&self) -> Option<FieldKind> { pub fn field_access(&self) -> Option<FieldKind> {
if let Some(nr) = self.name_ref() { match self.name_ref() {
Some(FieldKind::Name(nr)) Some(nr) => Some(FieldKind::Name(nr)),
} else { None => self.index_token().map(FieldKind::Index),
self.index_token().map(FieldKind::Index)
} }
} }
} }

View file

@ -283,10 +283,9 @@ pub trait HasFormatSpecifier: AstToken {
where where
F: FnMut(TextRange, FormatSpecifier), F: FnMut(TextRange, FormatSpecifier),
{ {
let char_ranges = if let Some(char_ranges) = self.char_ranges() { let char_ranges = match self.char_ranges() {
char_ranges Some(char_ranges) => char_ranges,
} else { None => return,
return;
}; };
let mut chars = char_ranges.iter().peekable(); let mut chars = char_ranges.iter().peekable();
@ -528,10 +527,11 @@ pub trait HasFormatSpecifier: AstToken {
} }
} }
if let Some((_, Ok('}'))) = chars.peek() { match chars.peek() {
skip_char_and_emit(&mut chars, FormatSpecifier::Close, &mut callback); Some((_, Ok('}'))) => {
} else { skip_char_and_emit(&mut chars, FormatSpecifier::Close, &mut callback);
continue; }
Some((_, _)) | None => continue,
} }
} }
_ => { _ => {

View file

@ -227,12 +227,9 @@ where
T: crate::AstNode, T: crate::AstNode,
F: Fn(&str) -> Result<T, ()>, F: Fn(&str) -> Result<T, ()>,
{ {
dir_tests(&test_data_dir(), ok_paths, "rast", |text, path| { dir_tests(&test_data_dir(), ok_paths, "rast", |text, path| match f(text) {
if let Ok(node) = f(text) { Ok(node) => format!("{:#?}", crate::ast::AstNode::syntax(&node)),
format!("{:#?}", crate::ast::AstNode::syntax(&node)) Err(_) => panic!("Failed to parse '{:?}'", path),
} else {
panic!("Failed to parse '{:?}'", path);
}
}); });
dir_tests(&test_data_dir(), err_paths, "rast", |text, path| { dir_tests(&test_data_dir(), err_paths, "rast", |text, path| {
if f(text).is_ok() { if f(text).is_ok() {

View file

@ -205,10 +205,9 @@ impl<'a> Cursor<'a> {
/// Bump the cursor /// Bump the cursor
pub fn bump(self) -> Cursor<'a> { pub fn bump(self) -> Cursor<'a> {
if let Some(Entry::End(exit)) = self.buffer.entry(&self.ptr) { if let Some(Entry::End(exit)) = self.buffer.entry(&self.ptr) {
if let Some(exit) = exit { match exit {
Cursor::create(self.buffer, *exit) Some(exit) => Cursor::create(self.buffer, *exit),
} else { None => self,
self
} }
} else { } else {
Cursor::create(self.buffer, EntryPtr(self.ptr.0, self.ptr.1 + 1)) Cursor::create(self.buffer, EntryPtr(self.ptr.0, self.ptr.1 + 1))