From 9583dd572597fd1e3bbde80e0b8cf68ad8acc090 Mon Sep 17 00:00:00 2001 From: Aramis Razzaghipour Date: Sun, 3 Oct 2021 23:53:01 +1100 Subject: [PATCH] Replace `if let` with `match` where appropriate --- crates/hir/src/lib.rs | 7 ++-- crates/hir_def/src/body/lower.rs | 28 ++++++-------- crates/hir_def/src/find_path.rs | 37 ++++++++----------- crates/hir_def/src/item_tree.rs | 7 ++-- crates/hir_def/src/nameres/path_resolution.rs | 11 ++---- crates/hir_def/src/path/lower/lower_use.rs | 7 ++-- crates/hir_def/src/type_ref.rs | 7 ++-- crates/hir_expand/src/name.rs | 7 ++-- crates/hir_ty/src/autoderef.rs | 7 ++-- crates/hir_ty/src/chalk_ext.rs | 7 ++-- .../match_check/deconstruct_pat.rs | 7 ++-- crates/hir_ty/src/display.rs | 7 ++-- crates/hir_ty/src/infer/expr.rs | 7 ++-- crates/hir_ty/src/infer/pat.rs | 7 ++-- crates/hir_ty/src/infer/unify.rs | 7 ++-- crates/hir_ty/src/lower.rs | 7 ++-- crates/hir_ty/src/method_resolution.rs | 7 ++-- crates/hir_ty/src/tests.rs | 14 +++---- crates/ide/src/display/navigation_target.rs | 7 ++-- crates/ide/src/join_lines.rs | 7 ++-- crates/ide/src/static_index.rs | 14 +++---- .../src/handlers/extract_function.rs | 7 ++-- .../src/handlers/extract_variable.rs | 9 +++-- .../src/handlers/generate_function.rs | 36 +++++++----------- .../ide_assists/src/handlers/inline_call.rs | 7 ++-- .../src/handlers/pull_assignment_up.rs | 9 ++--- .../ide_assists/src/handlers/qualify_path.rs | 7 ++-- crates/ide_assists/src/utils.rs | 22 ++++------- crates/ide_assists/src/utils/suggest_name.rs | 7 ++-- crates/ide_completion/src/context.rs | 7 ++-- crates/ide_completion/src/render/function.rs | 7 ++-- crates/ide_completion/src/render/pattern.rs | 7 ++-- .../src/render/struct_literal.rs | 7 ++-- crates/ide_ssr/src/resolving.rs | 7 ++-- .../src/abis/abi_1_47/rustc_server.rs | 7 ++-- .../src/abis/abi_1_55/rustc_server.rs | 7 ++-- .../src/abis/abi_1_56/rustc_server.rs | 7 ++-- crates/rust-analyzer/src/handlers.rs | 14 +++---- crates/syntax/src/ast/edit_in_place.rs | 30 ++++++--------- crates/syntax/src/ast/make.rs | 11 ++++-- crates/syntax/src/ast/node_ext.rs | 7 ++-- crates/syntax/src/ast/token_ext.rs | 16 ++++---- crates/syntax/src/tests.rs | 9 ++--- crates/tt/src/buffer.rs | 7 ++-- 44 files changed, 201 insertions(+), 269 deletions(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 7b010fc27101..038c69bc7325 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -2119,10 +2119,9 @@ impl Impl { }; let fp = TyFingerprint::for_inherent_impl(&ty); - let fp = if let Some(fp) = fp { - fp - } else { - return Vec::new(); + let fp = match fp { + Some(fp) => fp, + None => return Vec::new(), }; let mut all = Vec::new(); diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index 804d98ce3814..4c1a3344ec9b 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs @@ -474,10 +474,9 @@ impl ExprCollector<'_> { } ast::Expr::PrefixExpr(e) => { let expr = self.collect_expr_opt(e.expr()); - if let Some(op) = e.op_kind() { - self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr) - } else { - self.alloc_expr(Expr::Missing, syntax_ptr) + match e.op_kind() { + Some(op) => self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr), + None => self.alloc_expr(Expr::Missing, syntax_ptr), } } ast::Expr::ClosureExpr(e) => { @@ -624,10 +623,9 @@ impl ExprCollector<'_> { } fn collect_expr_opt(&mut self, expr: Option) -> ExprId { - if let Some(expr) = expr { - self.collect_expr(expr) - } else { - self.missing_expr() + match expr { + Some(expr) => self.collect_expr(expr), + None => self.missing_expr(), } } @@ -724,10 +722,9 @@ impl ExprCollector<'_> { } fn collect_block_opt(&mut self, expr: Option) -> ExprId { - if let Some(block) = expr { - self.collect_block(block) - } else { - self.missing_expr() + match expr { + Some(block) => self.collect_block(block), + None => self.missing_expr(), } } @@ -890,10 +887,9 @@ impl ExprCollector<'_> { } fn collect_pat_opt(&mut self, pat: Option) -> PatId { - if let Some(pat) = pat { - self.collect_pat(pat) - } else { - self.missing_pat() + match pat { + Some(pat) => self.collect_pat(pat), + None => self.missing_pat(), } } diff --git a/crates/hir_def/src/find_path.rs b/crates/hir_def/src/find_path.rs index 08ffa8e53d22..3251638c7aae 100644 --- a/crates/hir_def/src/find_path.rs +++ b/crates/hir_def/src/find_path.rs @@ -209,10 +209,9 @@ fn find_path_inner( ) { path.push_segment(name); - let new_path = if let Some(best_path) = best_path { - select_best_path(best_path, path, prefer_no_std) - } else { - path + let new_path = match best_path { + Some(best_path) => select_best_path(best_path, path, prefer_no_std), + None => path, }; best_path_len = new_path.len(); best_path = Some(new_path); @@ -243,10 +242,9 @@ fn find_path_inner( }); for path in extern_paths { - let new_path = if let Some(best_path) = best_path { - select_best_path(best_path, path, prefer_no_std) - } else { - path + let new_path = match best_path { + Some(best_path) => select_best_path(best_path, path, prefer_no_std), + None => path, }; best_path = Some(new_path); } @@ -261,12 +259,11 @@ fn find_path_inner( } } - if let Some(prefix) = prefixed.map(PrefixKind::prefix) { - best_path.or_else(|| { + match prefixed.map(PrefixKind::prefix) { + Some(prefix) => best_path.or_else(|| { scope_name.map(|scope_name| ModPath::from_segments(prefix, vec![scope_name])) - }) - } else { - best_path + }), + None => best_path, } } @@ -346,15 +343,13 @@ fn find_local_import_locations( if let Some((name, vis)) = data.scope.name_of(item) { if vis.is_visible_from(db, from) { - let is_private = if let Visibility::Module(private_to) = vis { - private_to.local_id == module.local_id - } else { - false + let is_private = match vis { + Visibility::Module(private_to) => private_to.local_id == module.local_id, + Visibility::Public => false, }; - let is_original_def = if let Some(module_def_id) = item.as_module_def_id() { - data.scope.declarations().any(|it| it == module_def_id) - } else { - false + let is_original_def = match item.as_module_def_id() { + Some(module_def_id) => data.scope.declarations().any(|it| it == module_def_id), + None => false, }; // Ignore private imports. these could be used if we are diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index 3900e7e97cc8..b7a5758a0a2c 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs @@ -475,10 +475,9 @@ macro_rules! mod_items { } fn id_from_mod_item(mod_item: ModItem) -> Option> { - if let ModItem::$typ(id) = mod_item { - Some(id) - } else { - None + match mod_item { + ModItem::$typ(id) => Some(id), + _ => None, } } diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs index 4fb81bde4619..30f11cc69435 100644 --- a/crates/hir_def/src/nameres/path_resolution.rs +++ b/crates/hir_def/src/nameres/path_resolution.rs @@ -400,13 +400,10 @@ impl DefMap { }; let from_scope_or_builtin = match shadow { BuiltinShadowMode::Module => from_scope.or(from_builtin), - BuiltinShadowMode::Other => { - if let Some(ModuleDefId::ModuleId(_)) = from_scope.take_types() { - from_builtin.or(from_scope) - } else { - from_scope.or(from_builtin) - } - } + BuiltinShadowMode::Other => match from_scope.take_types() { + Some(ModuleDefId::ModuleId(_)) => from_builtin.or(from_scope), + Some(_) | None => from_scope.or(from_builtin), + }, }; let from_extern_prelude = self .extern_prelude diff --git a/crates/hir_def/src/path/lower/lower_use.rs b/crates/hir_def/src/path/lower/lower_use.rs index 0ee406f63a64..6b777cfeff4e 100644 --- a/crates/hir_def/src/path/lower/lower_use.rs +++ b/crates/hir_def/src/path/lower/lower_use.rs @@ -18,10 +18,9 @@ pub(crate) fn convert_path( path: ast::Path, hygiene: &Hygiene, ) -> Option { - let prefix = if let Some(qual) = path.qualifier() { - Some(convert_path(db, prefix, qual, hygiene)?) - } else { - prefix + let prefix = match path.qualifier() { + Some(qual) => Some(convert_path(db, prefix, qual, hygiene)?), + None => prefix, }; let segment = path.segment()?; diff --git a/crates/hir_def/src/type_ref.rs b/crates/hir_def/src/type_ref.rs index 9f33fd26e845..cfc69feccc6c 100644 --- a/crates/hir_def/src/type_ref.rs +++ b/crates/hir_def/src/type_ref.rs @@ -214,10 +214,9 @@ impl TypeRef { } pub(crate) fn from_ast_opt(ctx: &LowerCtx, node: Option) -> Self { - if let Some(node) = node { - TypeRef::from_ast(ctx, node) - } else { - TypeRef::Error + match node { + Some(node) => TypeRef::from_ast(ctx, node), + None => TypeRef::Error, } } diff --git a/crates/hir_expand/src/name.rs b/crates/hir_expand/src/name.rs index 9c844c6138dc..552603dce7a8 100644 --- a/crates/hir_expand/src/name.rs +++ b/crates/hir_expand/src/name.rs @@ -48,10 +48,9 @@ impl Name { /// Resolve a name from the text of token. fn resolve(raw_text: &str) -> Name { - if let Some(text) = raw_text.strip_prefix("r#") { - Name::new_text(SmolStr::new(text)) - } else { - Name::new_text(raw_text.into()) + match raw_text.strip_prefix("r#") { + Some(text) => Name::new_text(SmolStr::new(text)), + None => Name::new_text(raw_text.into()), } } diff --git a/crates/hir_ty/src/autoderef.rs b/crates/hir_ty/src/autoderef.rs index e3f008645cd5..4dc46a2cdeaf 100644 --- a/crates/hir_ty/src/autoderef.rs +++ b/crates/hir_ty/src/autoderef.rs @@ -109,10 +109,9 @@ pub(crate) fn deref( ty: InEnvironment<&Canonical>, ) -> Option> { let _p = profile::span("deref"); - if let Some(derefed) = builtin_deref(&ty.goal.value) { - Some(Canonical { value: derefed, binders: ty.goal.binders.clone() }) - } else { - deref_by_trait(db, krate, ty) + match builtin_deref(&ty.goal.value) { + Some(derefed) => Some(Canonical { value: derefed, binders: ty.goal.binders.clone() }), + None => deref_by_trait(db, krate, ty), } } diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs index c29fbd688a66..36c56820eb67 100644 --- a/crates/hir_ty/src/chalk_ext.rs +++ b/crates/hir_ty/src/chalk_ext.rs @@ -104,10 +104,9 @@ impl TyExt for Ty { } fn as_fn_def(&self, db: &dyn HirDatabase) -> Option { - if let Some(CallableDefId::FunctionId(func)) = self.callable_def(db) { - Some(func) - } else { - None + match self.callable_def(db) { + Some(CallableDefId::FunctionId(func)) => Some(func), + Some(CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_)) | None => None, } } fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> { diff --git a/crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs b/crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs index 2fa456a035a3..f9439c706e30 100644 --- a/crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs +++ b/crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs @@ -105,10 +105,9 @@ impl IntRange { #[inline] fn from_range(lo: u128, hi: u128, scalar_ty: Scalar) -> IntRange { - if let Scalar::Bool = scalar_ty { - IntRange { range: lo..=hi } - } else { - unimplemented!() + match scalar_ty { + Scalar::Bool => IntRange { range: lo..=hi }, + _ => unimplemented!(), } } diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 1ae718a36b8d..adce43aa07ea 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -167,10 +167,9 @@ impl<'a> HirFormatter<'a> { } pub fn should_truncate(&self) -> bool { - if let Some(max_size) = self.max_size { - self.curr_size >= max_size - } else { - false + match self.max_size { + Some(max_size) => self.curr_size >= max_size, + None => false, } } diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index b0306d9148ae..f5bc898a85b2 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -264,10 +264,9 @@ impl<'a> InferenceContext<'a> { // collect explicitly written argument types for arg_type in arg_types.iter() { - let arg_ty = if let Some(type_ref) = arg_type { - self.make_ty(type_ref) - } else { - self.table.new_type_var() + let arg_ty = match arg_type { + Some(type_ref) => self.make_ty(type_ref), + None => self.table.new_type_var(), }; sig_tys.push(arg_ty); } diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index 957cd829aacc..9aaf7db87be5 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -204,10 +204,9 @@ impl<'a> InferenceContext<'a> { } else { BindingMode::convert(*mode) }; - let inner_ty = if let Some(subpat) = subpat { - self.infer_pat(*subpat, &expected, default_bm) - } else { - expected + let inner_ty = match subpat { + Some(subpat) => self.infer_pat(*subpat, &expected, default_bm), + None => expected, }; let inner_ty = self.insert_type_vars_shallow(inner_ty); diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index 4c1e758904ec..c40ef7f58755 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs @@ -324,10 +324,9 @@ impl<'a> InferenceTable<'a> { /// Unify two types and register new trait goals that arise from that. pub(crate) fn unify(&mut self, ty1: &Ty, ty2: &Ty) -> bool { - let result = if let Ok(r) = self.try_unify(ty1, ty2) { - r - } else { - return false; + let result = match self.try_unify(ty1, ty2) { + Ok(r) => r, + Err(_) => return false, }; self.register_infer_ok(result); true diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 3734eb101310..df1eb1c96168 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -368,10 +368,9 @@ impl<'a> TyLoweringContext<'a> { Some((it, None)) => it, _ => return None, }; - if let TypeNs::GenericParam(param_id) = resolution { - Some(param_id) - } else { - None + match resolution { + TypeNs::GenericParam(param_id) => Some(param_id), + _ => None, } } diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index c88a8b653588..8e6ab8af0f2f 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -82,10 +82,9 @@ impl TyFingerprint { TyKind::Ref(_, _, ty) => return TyFingerprint::for_trait_impl(ty), TyKind::Tuple(_, subst) => { let first_ty = subst.interned().get(0).map(|arg| arg.assert_ty_ref(&Interner)); - if let Some(ty) = first_ty { - return TyFingerprint::for_trait_impl(ty); - } else { - TyFingerprint::Unit + match first_ty { + Some(ty) => return TyFingerprint::for_trait_impl(ty), + None => TyFingerprint::Unit, } } TyKind::AssociatedType(_, _) diff --git a/crates/hir_ty/src/tests.rs b/crates/hir_ty/src/tests.rs index cf6833e56ad4..29250dca00cc 100644 --- a/crates/hir_ty/src/tests.rs +++ b/crates/hir_ty/src/tests.rs @@ -195,10 +195,9 @@ fn check_impl(ra_fixture: &str, allow_none: bool, only_types: bool, display_sour mismatch.expected.display_test(&db), mismatch.actual.display_test(&db) ); - if let Some(annotation) = mismatches.remove(&range) { - assert_eq!(actual, annotation); - } else { - format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual); + match mismatches.remove(&range) { + Some(annotation) => assert_eq!(actual, annotation), + None => format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual), } } 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.actual.display_test(&db) ); - if let Some(annotation) = mismatches.remove(&range) { - assert_eq!(actual, annotation); - } else { - format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual); + match mismatches.remove(&range) { + Some(annotation) => assert_eq!(actual, annotation), + None => format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual), } } } diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index 65c29f680532..01c1259fc54e 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs @@ -292,10 +292,9 @@ impl TryToNav for hir::Impl { fn try_to_nav(&self, db: &RootDatabase) -> Option { let src = self.source(db)?; let derive_attr = self.is_builtin_derive(db); - let frange = if let Some(item) = &derive_attr { - item.syntax().original_file_range(db) - } else { - src.syntax().original_file_range(db) + let frange = match &derive_attr { + Some(item) => item.syntax().original_file_range(db), + None => src.syntax().original_file_range(db), }; let focus_range = if derive_attr.is_some() { None diff --git a/crates/ide/src/join_lines.rs b/crates/ide/src/join_lines.rs index 000d74abacb7..4851f9aee0c7 100644 --- a/crates/ide/src/join_lines.rs +++ b/crates/ide/src/join_lines.rs @@ -136,10 +136,9 @@ fn remove_newline( } T!['}'] => { // Removes: comma, newline (incl. surrounding whitespace) - let space = if let Some(left) = prev.prev_sibling_or_token() { - compute_ws(left.kind(), next.kind()) - } else { - " " + let space = match prev.prev_sibling_or_token() { + Some(left) => compute_ws(left.kind(), next.kind()), + None => " ", }; edit.replace( TextRange::new(prev.text_range().start(), token.text_range().end()), diff --git a/crates/ide/src/static_index.rs b/crates/ide/src/static_index.rs index aa62e2eae5a3..eb1a04c3f761 100644 --- a/crates/ide/src/static_index.rs +++ b/crates/ide/src/static_index.rs @@ -103,10 +103,9 @@ impl StaticIndex<'_> { for token in tokens { let range = token.text_range(); let node = token.parent().unwrap(); - let def = if let Some(x) = get_definition(&sema, token.clone()) { - x - } else { - continue; + let def = match get_definition(&sema, token.clone()) { + Some(x) => x, + None => continue, }; let id = if let Some(x) = self.def_map.get(&def) { *x @@ -124,10 +123,9 @@ impl StaticIndex<'_> { let token = self.tokens.get_mut(id).unwrap(); token.references.push(ReferenceData { range: FileRange { range, file_id }, - is_definition: if let Some(x) = def.try_to_nav(self.db) { - x.file_id == file_id && x.focus_or_full_range() == range - } else { - false + is_definition: match def.try_to_nav(self.db) { + Some(x) => x.file_id == file_id && x.focus_or_full_range() == range, + None => false, }, }); result.tokens.push((range, id)); diff --git a/crates/ide_assists/src/handlers/extract_function.rs b/crates/ide_assists/src/handlers/extract_function.rs index 585ef31daf2e..b7eb2aefad41 100644 --- a/crates/ide_assists/src/handlers/extract_function.rs +++ b/crates/ide_assists/src/handlers/extract_function.rs @@ -827,10 +827,9 @@ impl FunctionBody { locals .map(|local| (local, local.source(ctx.db()))) .filter(|(_, src)| is_defined_outside_of_body(ctx, self, src)) - .filter_map(|(local, src)| { - if let Either::Left(src) = src.value { - Some((local, src)) - } else { + .filter_map(|(local, src)| match src.value { + Either::Left(src) => Some((local, src)), + Either::Right(_) => { stdx::never!(false, "Local::is_self returned false, but source is SelfParam"); None } diff --git a/crates/ide_assists/src/handlers/extract_variable.rs b/crates/ide_assists/src/handlers/extract_variable.rs index 9b4e1380a467..419bf2787748 100644 --- a/crates/ide_assists/src/handlers/extract_variable.rs +++ b/crates/ide_assists/src/handlers/extract_variable.rs @@ -69,10 +69,11 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option None => to_extract.syntax().text_range(), }; - if let Anchor::WrapInBlock(_) = anchor { - format_to!(buf, "{{ let {} = ", var_name); - } else { - format_to!(buf, "let {} = ", var_name); + match anchor { + Anchor::Before(_) | Anchor::Replace(_) => { + format_to!(buf, "let {} = ", var_name) + } + Anchor::WrapInBlock(_) => format_to!(buf, "{{ let {} = ", var_name), }; format_to!(buf, "{}", to_extract.syntax()); diff --git a/crates/ide_assists/src/handlers/generate_function.rs b/crates/ide_assists/src/handlers/generate_function.rs index 5e57cb76ebc2..ea3656ed2148 100644 --- a/crates/ide_assists/src/handlers/generate_function.rs +++ b/crates/ide_assists/src/handlers/generate_function.rs @@ -213,10 +213,9 @@ impl FunctionTemplate { Some(cap) => { let cursor = if self.should_focus_return_type { // Focus the return type if there is one - if let Some(ref ret_type) = self.ret_type { - ret_type.syntax() - } else { - self.tail_expr.syntax() + match self.ret_type { + Some(ref ret_type) => ret_type.syntax(), + None => self.tail_expr.syntax(), } } else { self.tail_expr.syntax() @@ -447,10 +446,9 @@ fn fn_args( arg_types.push(match fn_arg_type(ctx, target_module, &arg) { Some(ty) => { if !ty.is_empty() && ty.starts_with('&') { - if let Some((new_ty, _)) = useless_type_special_case("", &ty[1..].to_owned()) { - new_ty - } else { - ty + match useless_type_special_case("", &ty[1..].to_owned()) { + Some((new_ty, _)) => new_ty, + None => ty, } } else { ty @@ -575,20 +573,14 @@ fn next_space_for_fn_in_module( ) -> Option<(FileId, GeneratedFunctionTarget)> { let file = module_source.file_id.original_file(db); let assist_item = match &module_source.value { - hir::ModuleSource::SourceFile(it) => { - if let Some(last_item) = it.items().last() { - GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()) - } else { - GeneratedFunctionTarget::BehindItem(it.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::SourceFile(it) => match it.items().last() { + Some(last_item) => GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()), + None => 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::BlockExpr(it) => { if let Some(last_item) = it.statements().take_while(|stmt| matches!(stmt, ast::Stmt::Item(_))).last() diff --git a/crates/ide_assists/src/handlers/inline_call.rs b/crates/ide_assists/src/handlers/inline_call.rs index d252d61a6960..091f015ee0aa 100644 --- a/crates/ide_assists/src/handlers/inline_call.rs +++ b/crates/ide_assists/src/handlers/inline_call.rs @@ -141,10 +141,9 @@ pub(crate) fn inline_into_callers(acc: &mut Assists, ctx: &AssistContext) -> Opt for (file_id, refs) in usages.into_iter() { inline_refs_for_file(file_id, refs); } - if let Some(refs) = current_file_usage { - inline_refs_for_file(def_file, refs); - } else { - builder.edit_file(def_file); + match current_file_usage { + Some(refs) => inline_refs_for_file(def_file, refs), + None => builder.edit_file(def_file), } if remove_def { builder.delete(ast_func.syntax().text_range()); diff --git a/crates/ide_assists/src/handlers/pull_assignment_up.rs b/crates/ide_assists/src/handlers/pull_assignment_up.rs index 8946ecfac7c7..d142397c24f0 100644 --- a/crates/ide_assists/src/handlers/pull_assignment_up.rs +++ b/crates/ide_assists/src/handlers/pull_assignment_up.rs @@ -127,12 +127,9 @@ impl<'a> AssignmentsCollector<'a> { } } fn collect_block(&mut self, block: &ast::BlockExpr) -> Option<()> { - let last_expr = block.tail_expr().or_else(|| { - if let ast::Stmt::ExprStmt(stmt) = block.statements().last()? { - stmt.expr() - } else { - None - } + let last_expr = block.tail_expr().or_else(|| match block.statements().last()? { + ast::Stmt::ExprStmt(stmt) => stmt.expr(), + ast::Stmt::Item(_) | ast::Stmt::LetStmt(_) => None, })?; if let ast::Expr::BinExpr(expr) = last_expr { diff --git a/crates/ide_assists/src/handlers/qualify_path.rs b/crates/ide_assists/src/handlers/qualify_path.rs index 8a2ec8ba809f..0b33acc39be0 100644 --- a/crates/ide_assists/src/handlers/qualify_path.rs +++ b/crates/ide_assists/src/handlers/qualify_path.rs @@ -181,10 +181,9 @@ fn find_trait_method( fn item_as_trait(db: &RootDatabase, item: hir::ItemInNs) -> Option { let item_module_def = item.as_module_def()?; - if let hir::ModuleDef::Trait(trait_) = item_module_def { - Some(trait_) - } else { - item_module_def.as_assoc_item(db)?.containing_trait(db) + match item_module_def { + hir::ModuleDef::Trait(trait_) => Some(trait_), + _ => item_module_def.as_assoc_item(db)?.containing_trait(db), } } diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs index dfb22afc7e6f..3d4ab968fbb8 100644 --- a/crates/ide_assists/src/utils.rs +++ b/crates/ide_assists/src/utils.rs @@ -250,13 +250,10 @@ fn invert_special_case(expr: &ast::Expr) -> Option { }; Some(make::expr_method_call(receiver, make::name_ref(method), arg_list)) } - ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::UnaryOp::Not => { - if let ast::Expr::ParenExpr(parexpr) = pe.expr()? { - parexpr.expr() - } else { - pe.expr() - } - } + ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::UnaryOp::Not => match pe.expr()? { + ast::Expr::ParenExpr(parexpr) => parexpr.expr(), + _ => pe.expr(), + }, ast::Expr::Literal(lit) => match lit.kind() { ast::LiteralKind::Bool(b) => match b { 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 pat_head = match pat { - ast::Pat::IdentPat(bind_pat) => { - if let Some(p) = bind_pat.pat() { - first_node_text(&p) - } else { - return pat.syntax().text() == var.syntax().text(); - } - } + ast::Pat::IdentPat(bind_pat) => match bind_pat.pat() { + Some(p) => first_node_text(&p), + None => return pat.syntax().text() == var.syntax().text(), + }, pat => first_node_text(pat), }; diff --git a/crates/ide_assists/src/utils/suggest_name.rs b/crates/ide_assists/src/utils/suggest_name.rs index 35e230019625..74523234b0e2 100644 --- a/crates/ide_assists/src/utils/suggest_name.rs +++ b/crates/ide_assists/src/utils/suggest_name.rs @@ -144,10 +144,9 @@ fn is_valid_name(name: &str) -> bool { fn is_useless_method(method: &ast::MethodCallExpr) -> bool { let ident = method.name_ref().and_then(|it| it.ident_token()); - if let Some(ident) = ident { - USELESS_METHODS.contains(&ident.text()) - } else { - false + match ident { + Some(ident) => USELESS_METHODS.contains(&ident.text()), + None => false, } } diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index ddf8722bc85d..d6e15e6af7b3 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs @@ -509,10 +509,9 @@ impl<'a> CompletionContext<'a> { .and_then(|pat| self.sema.type_of_pat(&pat)) .or_else(|| it.initializer().and_then(|it| self.sema.type_of_expr(&it))) .map(TypeInfo::original); - let name = if let Some(ast::Pat::IdentPat(ident)) = it.pat() { - ident.name().map(NameOrNameRef::Name) - } else { - None + let name = match it.pat() { + Some(ast::Pat::IdentPat(ident)) => ident.name().map(NameOrNameRef::Name), + Some(_) | None => None, }; (ty, name) diff --git a/crates/ide_completion/src/render/function.rs b/crates/ide_completion/src/render/function.rs index 95244a758de6..904624f9ab01 100644 --- a/crates/ide_completion/src/render/function.rs +++ b/crates/ide_completion/src/render/function.rs @@ -74,10 +74,9 @@ impl<'a> FunctionRender<'a> { fn render(self, import_to_add: Option) -> CompletionItem { let params = self.params(); - let call = if let Some(receiver) = &self.receiver { - format!("{}.{}", receiver, &self.name) - } else { - self.name.clone() + let call = match &self.receiver { + Some(receiver) => format!("{}.{}", receiver, &self.name), + None => self.name.clone(), }; let mut item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), call.clone()); diff --git a/crates/ide_completion/src/render/pattern.rs b/crates/ide_completion/src/render/pattern.rs index 306e418e91db..521296fd9b72 100644 --- a/crates/ide_completion/src/render/pattern.rs +++ b/crates/ide_completion/src/render/pattern.rs @@ -63,10 +63,9 @@ fn build_completion( .set_documentation(ctx.docs(def)) .set_deprecated(ctx.is_deprecated(def)) .detail(&pat); - if let Some(snippet_cap) = ctx.snippet_cap() { - item.insert_snippet(snippet_cap, pat); - } else { - item.insert_text(pat); + match ctx.snippet_cap() { + Some(snippet_cap) => item.insert_snippet(snippet_cap, pat), + None => item.insert_text(pat), }; item.build() } diff --git a/crates/ide_completion/src/render/struct_literal.rs b/crates/ide_completion/src/render/struct_literal.rs index 68871a46716b..810b51effd38 100644 --- a/crates/ide_completion/src/render/struct_literal.rs +++ b/crates/ide_completion/src/render/struct_literal.rs @@ -38,10 +38,9 @@ fn build_completion( .set_documentation(ctx.docs(def)) .set_deprecated(ctx.is_deprecated(def)) .detail(&literal); - if let Some(snippet_cap) = ctx.snippet_cap() { - item.insert_snippet(snippet_cap, literal); - } else { - item.insert_text(literal); + match ctx.snippet_cap() { + Some(snippet_cap) => item.insert_snippet(snippet_cap, literal), + None => item.insert_text(literal), }; item.build() } diff --git a/crates/ide_ssr/src/resolving.rs b/crates/ide_ssr/src/resolving.rs index df20ab375d22..9cdd270b8036 100644 --- a/crates/ide_ssr/src/resolving.rs +++ b/crates/ide_ssr/src/resolving.rs @@ -47,10 +47,9 @@ impl ResolvedRule { ) -> Result { let resolver = Resolver { resolution_scope, placeholders_by_stand_in: rule.placeholders_by_stand_in }; - let resolved_template = if let Some(template) = rule.template { - Some(resolver.resolve_pattern_tree(template)?) - } else { - None + let resolved_template = match rule.template { + Some(template) => Some(resolver.resolve_pattern_tree(template)?), + None => None, }; Ok(ResolvedRule { pattern: resolver.resolve_pattern_tree(rule.pattern)?, diff --git a/crates/proc_macro_srv/src/abis/abi_1_47/rustc_server.rs b/crates/proc_macro_srv/src/abis/abi_1_47/rustc_server.rs index ff4976d0cbf3..a77433a55121 100644 --- a/crates/proc_macro_srv/src/abis/abi_1_47/rustc_server.rs +++ b/crates/proc_macro_srv/src/abis/abi_1_47/rustc_server.rs @@ -497,10 +497,9 @@ impl server::Literal for Rustc { } fn integer(&mut self, n: &str) -> Self::Literal { - let n = if let Ok(n) = n.parse::() { - n.to_string() - } else { - n.parse::().unwrap().to_string() + let n = match n.parse::() { + Ok(n) => n.to_string(), + Err(_) => n.parse::().unwrap().to_string(), }; Literal { text: n.into(), id: tt::TokenId::unspecified() } } diff --git a/crates/proc_macro_srv/src/abis/abi_1_55/rustc_server.rs b/crates/proc_macro_srv/src/abis/abi_1_55/rustc_server.rs index 56f9853399c3..498fa7ea2dca 100644 --- a/crates/proc_macro_srv/src/abis/abi_1_55/rustc_server.rs +++ b/crates/proc_macro_srv/src/abis/abi_1_55/rustc_server.rs @@ -500,10 +500,9 @@ impl server::Literal for Rustc { } fn integer(&mut self, n: &str) -> Self::Literal { - let n = if let Ok(n) = n.parse::() { - n.to_string() - } else { - n.parse::().unwrap().to_string() + let n = match n.parse::() { + Ok(n) => n.to_string(), + Err(_) => n.parse::().unwrap().to_string(), }; Literal { text: n.into(), id: tt::TokenId::unspecified() } } diff --git a/crates/proc_macro_srv/src/abis/abi_1_56/rustc_server.rs b/crates/proc_macro_srv/src/abis/abi_1_56/rustc_server.rs index c1064ae96847..fb8a4c8cbe87 100644 --- a/crates/proc_macro_srv/src/abis/abi_1_56/rustc_server.rs +++ b/crates/proc_macro_srv/src/abis/abi_1_56/rustc_server.rs @@ -504,10 +504,9 @@ impl server::Literal for Rustc { } fn integer(&mut self, n: &str) -> Self::Literal { - let n = if let Ok(n) = n.parse::() { - n.to_string() - } else { - n.parse::().unwrap().to_string() + let n = match n.parse::() { + Ok(n) => n.to_string(), + Err(_) => n.parse::().unwrap().to_string(), }; Literal { text: n.into(), id: tt::TokenId::unspecified() } } diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index e62bb9499fa3..6cb6b0a8d024 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -427,10 +427,9 @@ pub(crate) fn handle_workspace_symbol( // If no explicit marker was set, check request params. If that's also empty // use global config. if !all_symbols { - let search_kind = if let Some(ref search_kind) = params.search_kind { - search_kind - } else { - &config.search_kind + let search_kind = match params.search_kind { + Some(ref search_kind) => search_kind, + None => &config.search_kind, }; all_symbols = match search_kind { lsp_ext::WorkspaceSymbolSearchKind::OnlyTypes => false, @@ -439,10 +438,9 @@ pub(crate) fn handle_workspace_symbol( } if !libs { - let search_scope = if let Some(ref search_scope) = params.search_scope { - search_scope - } else { - &config.search_scope + let search_scope = match params.search_scope { + Some(ref search_scope) => search_scope, + None => &config.search_scope, }; libs = match search_scope { lsp_ext::WorkspaceSymbolSearchScope::Workspace => false, diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs index d304e215b404..552d6fc1e3eb 100644 --- a/crates/syntax/src/ast/edit_in_place.rs +++ b/crates/syntax/src/ast/edit_in_place.rs @@ -60,10 +60,9 @@ impl GenericParamsOwnerEdit for ast::Impl { match self.generic_param_list() { Some(it) => it, None => { - let position = if let Some(imp_token) = self.impl_token() { - Position::after(imp_token) - } else { - Position::last_child_of(self.syntax()) + let position = match self.impl_token() { + Some(imp_token) => Position::after(imp_token), + None => Position::last_child_of(self.syntax()), }; create_generic_param_list(position) } @@ -72,10 +71,9 @@ impl GenericParamsOwnerEdit for ast::Impl { fn get_or_create_where_clause(&self) -> ast::WhereClause { if self.where_clause().is_none() { - let position = if let Some(items) = self.assoc_item_list() { - Position::before(items.syntax()) - } else { - Position::last_child_of(self.syntax()) + let position = match self.assoc_item_list() { + Some(items) => Position::before(items.syntax()), + None => Position::last_child_of(self.syntax()), }; create_where_clause(position); } @@ -102,10 +100,9 @@ impl GenericParamsOwnerEdit for ast::Trait { fn get_or_create_where_clause(&self) -> ast::WhereClause { if self.where_clause().is_none() { - let position = if let Some(items) = self.assoc_item_list() { - Position::before(items.syntax()) - } else { - Position::last_child_of(self.syntax()) + let position = match self.assoc_item_list() { + Some(items) => Position::before(items.syntax()), + None => Position::last_child_of(self.syntax()), }; create_where_clause(position); } @@ -253,12 +250,9 @@ impl ast::WhereClause { impl ast::TypeBoundList { pub fn remove(&self) { - if let Some(colon) = - self.syntax().siblings_with_tokens(Direction::Prev).find(|it| it.kind() == T![:]) - { - ted::remove_all(colon..=self.syntax().clone().into()) - } else { - ted::remove(self.syntax()) + match 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()), } } } diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index b4ad26c13a35..d1888d165c2b 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -641,9 +641,14 @@ pub fn fn_( ret_type: Option, is_async: bool, ) -> ast::Fn { - let type_params = - if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() }; - let ret_type = if let Some(ret_type) = ret_type { format!("{} ", ret_type) } else { "".into() }; + let type_params = match type_params { + Some(type_params) => format!("<{}>", type_params), + None => "".into(), + }; + let ret_type = match ret_type { + Some(ret_type) => format!("{} ", ret_type), + None => "".into(), + }; let visibility = match visibility { None => String::new(), Some(it) => format!("{} ", it), diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 00babfd394f7..2e6544f090e0 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -549,10 +549,9 @@ impl ast::FieldExpr { } pub fn field_access(&self) -> Option { - if let Some(nr) = self.name_ref() { - Some(FieldKind::Name(nr)) - } else { - self.index_token().map(FieldKind::Index) + match self.name_ref() { + Some(nr) => Some(FieldKind::Name(nr)), + None => self.index_token().map(FieldKind::Index), } } } diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs index 6946f35ea5d8..2f8312d5d1ec 100644 --- a/crates/syntax/src/ast/token_ext.rs +++ b/crates/syntax/src/ast/token_ext.rs @@ -283,10 +283,9 @@ pub trait HasFormatSpecifier: AstToken { where F: FnMut(TextRange, FormatSpecifier), { - let char_ranges = if let Some(char_ranges) = self.char_ranges() { - char_ranges - } else { - return; + let char_ranges = match self.char_ranges() { + Some(char_ranges) => char_ranges, + None => return, }; let mut chars = char_ranges.iter().peekable(); @@ -528,10 +527,11 @@ pub trait HasFormatSpecifier: AstToken { } } - if let Some((_, Ok('}'))) = chars.peek() { - skip_char_and_emit(&mut chars, FormatSpecifier::Close, &mut callback); - } else { - continue; + match chars.peek() { + Some((_, Ok('}'))) => { + skip_char_and_emit(&mut chars, FormatSpecifier::Close, &mut callback); + } + Some((_, _)) | None => continue, } } _ => { diff --git a/crates/syntax/src/tests.rs b/crates/syntax/src/tests.rs index f8f7a3ad325f..022db39f3309 100644 --- a/crates/syntax/src/tests.rs +++ b/crates/syntax/src/tests.rs @@ -227,12 +227,9 @@ where T: crate::AstNode, F: Fn(&str) -> Result, { - dir_tests(&test_data_dir(), ok_paths, "rast", |text, path| { - if let Ok(node) = f(text) { - format!("{:#?}", crate::ast::AstNode::syntax(&node)) - } else { - panic!("Failed to parse '{:?}'", path); - } + dir_tests(&test_data_dir(), ok_paths, "rast", |text, path| match f(text) { + Ok(node) => format!("{:#?}", crate::ast::AstNode::syntax(&node)), + Err(_) => panic!("Failed to parse '{:?}'", path), }); dir_tests(&test_data_dir(), err_paths, "rast", |text, path| { if f(text).is_ok() { diff --git a/crates/tt/src/buffer.rs b/crates/tt/src/buffer.rs index 3ce72fea854c..1fa61c41ddbc 100644 --- a/crates/tt/src/buffer.rs +++ b/crates/tt/src/buffer.rs @@ -205,10 +205,9 @@ impl<'a> Cursor<'a> { /// Bump the cursor pub fn bump(self) -> Cursor<'a> { if let Some(Entry::End(exit)) = self.buffer.entry(&self.ptr) { - if let Some(exit) = exit { - Cursor::create(self.buffer, *exit) - } else { - self + match exit { + Some(exit) => Cursor::create(self.buffer, *exit), + None => self, } } else { Cursor::create(self.buffer, EntryPtr(self.ptr.0, self.ptr.1 + 1))