Skip to content

Commit 9583dd5

Browse files
committed
Replace if let with match where appropriate
1 parent f29796d commit 9583dd5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+201
-269
lines changed

crates/hir/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -2119,10 +2119,9 @@ impl Impl {
21192119
};
21202120

21212121
let fp = TyFingerprint::for_inherent_impl(&ty);
2122-
let fp = if let Some(fp) = fp {
2123-
fp
2124-
} else {
2125-
return Vec::new();
2122+
let fp = match fp {
2123+
Some(fp) => fp,
2124+
None => return Vec::new(),
21262125
};
21272126

21282127
let mut all = Vec::new();

crates/hir_def/src/body/lower.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,9 @@ impl ExprCollector<'_> {
474474
}
475475
ast::Expr::PrefixExpr(e) => {
476476
let expr = self.collect_expr_opt(e.expr());
477-
if let Some(op) = e.op_kind() {
478-
self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr)
479-
} else {
480-
self.alloc_expr(Expr::Missing, syntax_ptr)
477+
match e.op_kind() {
478+
Some(op) => self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr),
479+
None => self.alloc_expr(Expr::Missing, syntax_ptr),
481480
}
482481
}
483482
ast::Expr::ClosureExpr(e) => {
@@ -624,10 +623,9 @@ impl ExprCollector<'_> {
624623
}
625624

626625
fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId {
627-
if let Some(expr) = expr {
628-
self.collect_expr(expr)
629-
} else {
630-
self.missing_expr()
626+
match expr {
627+
Some(expr) => self.collect_expr(expr),
628+
None => self.missing_expr(),
631629
}
632630
}
633631

@@ -724,10 +722,9 @@ impl ExprCollector<'_> {
724722
}
725723

726724
fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId {
727-
if let Some(block) = expr {
728-
self.collect_block(block)
729-
} else {
730-
self.missing_expr()
725+
match expr {
726+
Some(block) => self.collect_block(block),
727+
None => self.missing_expr(),
731728
}
732729
}
733730

@@ -890,10 +887,9 @@ impl ExprCollector<'_> {
890887
}
891888

892889
fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId {
893-
if let Some(pat) = pat {
894-
self.collect_pat(pat)
895-
} else {
896-
self.missing_pat()
890+
match pat {
891+
Some(pat) => self.collect_pat(pat),
892+
None => self.missing_pat(),
897893
}
898894
}
899895

crates/hir_def/src/find_path.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,9 @@ fn find_path_inner(
209209
) {
210210
path.push_segment(name);
211211

212-
let new_path = if let Some(best_path) = best_path {
213-
select_best_path(best_path, path, prefer_no_std)
214-
} else {
215-
path
212+
let new_path = match best_path {
213+
Some(best_path) => select_best_path(best_path, path, prefer_no_std),
214+
None => path,
216215
};
217216
best_path_len = new_path.len();
218217
best_path = Some(new_path);
@@ -243,10 +242,9 @@ fn find_path_inner(
243242
});
244243

245244
for path in extern_paths {
246-
let new_path = if let Some(best_path) = best_path {
247-
select_best_path(best_path, path, prefer_no_std)
248-
} else {
249-
path
245+
let new_path = match best_path {
246+
Some(best_path) => select_best_path(best_path, path, prefer_no_std),
247+
None => path,
250248
};
251249
best_path = Some(new_path);
252250
}
@@ -261,12 +259,11 @@ fn find_path_inner(
261259
}
262260
}
263261

264-
if let Some(prefix) = prefixed.map(PrefixKind::prefix) {
265-
best_path.or_else(|| {
262+
match prefixed.map(PrefixKind::prefix) {
263+
Some(prefix) => best_path.or_else(|| {
266264
scope_name.map(|scope_name| ModPath::from_segments(prefix, vec![scope_name]))
267-
})
268-
} else {
269-
best_path
265+
}),
266+
None => best_path,
270267
}
271268
}
272269

@@ -346,15 +343,13 @@ fn find_local_import_locations(
346343

347344
if let Some((name, vis)) = data.scope.name_of(item) {
348345
if vis.is_visible_from(db, from) {
349-
let is_private = if let Visibility::Module(private_to) = vis {
350-
private_to.local_id == module.local_id
351-
} else {
352-
false
346+
let is_private = match vis {
347+
Visibility::Module(private_to) => private_to.local_id == module.local_id,
348+
Visibility::Public => false,
353349
};
354-
let is_original_def = if let Some(module_def_id) = item.as_module_def_id() {
355-
data.scope.declarations().any(|it| it == module_def_id)
356-
} else {
357-
false
350+
let is_original_def = match item.as_module_def_id() {
351+
Some(module_def_id) => data.scope.declarations().any(|it| it == module_def_id),
352+
None => false,
358353
};
359354

360355
// Ignore private imports. these could be used if we are

crates/hir_def/src/item_tree.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,9 @@ macro_rules! mod_items {
475475
}
476476

477477
fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> {
478-
if let ModItem::$typ(id) = mod_item {
479-
Some(id)
480-
} else {
481-
None
478+
match mod_item {
479+
ModItem::$typ(id) => Some(id),
480+
_ => None,
482481
}
483482
}
484483

crates/hir_def/src/nameres/path_resolution.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,10 @@ impl DefMap {
400400
};
401401
let from_scope_or_builtin = match shadow {
402402
BuiltinShadowMode::Module => from_scope.or(from_builtin),
403-
BuiltinShadowMode::Other => {
404-
if let Some(ModuleDefId::ModuleId(_)) = from_scope.take_types() {
405-
from_builtin.or(from_scope)
406-
} else {
407-
from_scope.or(from_builtin)
408-
}
409-
}
403+
BuiltinShadowMode::Other => match from_scope.take_types() {
404+
Some(ModuleDefId::ModuleId(_)) => from_builtin.or(from_scope),
405+
Some(_) | None => from_scope.or(from_builtin),
406+
},
410407
};
411408
let from_extern_prelude = self
412409
.extern_prelude

crates/hir_def/src/path/lower/lower_use.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ pub(crate) fn convert_path(
1818
path: ast::Path,
1919
hygiene: &Hygiene,
2020
) -> Option<ModPath> {
21-
let prefix = if let Some(qual) = path.qualifier() {
22-
Some(convert_path(db, prefix, qual, hygiene)?)
23-
} else {
24-
prefix
21+
let prefix = match path.qualifier() {
22+
Some(qual) => Some(convert_path(db, prefix, qual, hygiene)?),
23+
None => prefix,
2524
};
2625

2726
let segment = path.segment()?;

crates/hir_def/src/type_ref.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,9 @@ impl TypeRef {
214214
}
215215

216216
pub(crate) fn from_ast_opt(ctx: &LowerCtx, node: Option<ast::Type>) -> Self {
217-
if let Some(node) = node {
218-
TypeRef::from_ast(ctx, node)
219-
} else {
220-
TypeRef::Error
217+
match node {
218+
Some(node) => TypeRef::from_ast(ctx, node),
219+
None => TypeRef::Error,
221220
}
222221
}
223222

crates/hir_expand/src/name.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ impl Name {
4848

4949
/// Resolve a name from the text of token.
5050
fn resolve(raw_text: &str) -> Name {
51-
if let Some(text) = raw_text.strip_prefix("r#") {
52-
Name::new_text(SmolStr::new(text))
53-
} else {
54-
Name::new_text(raw_text.into())
51+
match raw_text.strip_prefix("r#") {
52+
Some(text) => Name::new_text(SmolStr::new(text)),
53+
None => Name::new_text(raw_text.into()),
5554
}
5655
}
5756

crates/hir_ty/src/autoderef.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,9 @@ pub(crate) fn deref(
109109
ty: InEnvironment<&Canonical<Ty>>,
110110
) -> Option<Canonical<Ty>> {
111111
let _p = profile::span("deref");
112-
if let Some(derefed) = builtin_deref(&ty.goal.value) {
113-
Some(Canonical { value: derefed, binders: ty.goal.binders.clone() })
114-
} else {
115-
deref_by_trait(db, krate, ty)
112+
match builtin_deref(&ty.goal.value) {
113+
Some(derefed) => Some(Canonical { value: derefed, binders: ty.goal.binders.clone() }),
114+
None => deref_by_trait(db, krate, ty),
116115
}
117116
}
118117

crates/hir_ty/src/chalk_ext.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,9 @@ impl TyExt for Ty {
104104
}
105105

106106
fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId> {
107-
if let Some(CallableDefId::FunctionId(func)) = self.callable_def(db) {
108-
Some(func)
109-
} else {
110-
None
107+
match self.callable_def(db) {
108+
Some(CallableDefId::FunctionId(func)) => Some(func),
109+
Some(CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_)) | None => None,
111110
}
112111
}
113112
fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> {

crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@ impl IntRange {
105105

106106
#[inline]
107107
fn from_range(lo: u128, hi: u128, scalar_ty: Scalar) -> IntRange {
108-
if let Scalar::Bool = scalar_ty {
109-
IntRange { range: lo..=hi }
110-
} else {
111-
unimplemented!()
108+
match scalar_ty {
109+
Scalar::Bool => IntRange { range: lo..=hi },
110+
_ => unimplemented!(),
112111
}
113112
}
114113

crates/hir_ty/src/display.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,9 @@ impl<'a> HirFormatter<'a> {
167167
}
168168

169169
pub fn should_truncate(&self) -> bool {
170-
if let Some(max_size) = self.max_size {
171-
self.curr_size >= max_size
172-
} else {
173-
false
170+
match self.max_size {
171+
Some(max_size) => self.curr_size >= max_size,
172+
None => false,
174173
}
175174
}
176175

crates/hir_ty/src/infer/expr.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,9 @@ impl<'a> InferenceContext<'a> {
264264

265265
// collect explicitly written argument types
266266
for arg_type in arg_types.iter() {
267-
let arg_ty = if let Some(type_ref) = arg_type {
268-
self.make_ty(type_ref)
269-
} else {
270-
self.table.new_type_var()
267+
let arg_ty = match arg_type {
268+
Some(type_ref) => self.make_ty(type_ref),
269+
None => self.table.new_type_var(),
271270
};
272271
sig_tys.push(arg_ty);
273272
}

crates/hir_ty/src/infer/pat.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,9 @@ impl<'a> InferenceContext<'a> {
204204
} else {
205205
BindingMode::convert(*mode)
206206
};
207-
let inner_ty = if let Some(subpat) = subpat {
208-
self.infer_pat(*subpat, &expected, default_bm)
209-
} else {
210-
expected
207+
let inner_ty = match subpat {
208+
Some(subpat) => self.infer_pat(*subpat, &expected, default_bm),
209+
None => expected,
211210
};
212211
let inner_ty = self.insert_type_vars_shallow(inner_ty);
213212

crates/hir_ty/src/infer/unify.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,9 @@ impl<'a> InferenceTable<'a> {
324324

325325
/// Unify two types and register new trait goals that arise from that.
326326
pub(crate) fn unify(&mut self, ty1: &Ty, ty2: &Ty) -> bool {
327-
let result = if let Ok(r) = self.try_unify(ty1, ty2) {
328-
r
329-
} else {
330-
return false;
327+
let result = match self.try_unify(ty1, ty2) {
328+
Ok(r) => r,
329+
Err(_) => return false,
331330
};
332331
self.register_infer_ok(result);
333332
true

crates/hir_ty/src/lower.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,9 @@ impl<'a> TyLoweringContext<'a> {
368368
Some((it, None)) => it,
369369
_ => return None,
370370
};
371-
if let TypeNs::GenericParam(param_id) = resolution {
372-
Some(param_id)
373-
} else {
374-
None
371+
match resolution {
372+
TypeNs::GenericParam(param_id) => Some(param_id),
373+
_ => None,
375374
}
376375
}
377376

crates/hir_ty/src/method_resolution.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,9 @@ impl TyFingerprint {
8282
TyKind::Ref(_, _, ty) => return TyFingerprint::for_trait_impl(ty),
8383
TyKind::Tuple(_, subst) => {
8484
let first_ty = subst.interned().get(0).map(|arg| arg.assert_ty_ref(&Interner));
85-
if let Some(ty) = first_ty {
86-
return TyFingerprint::for_trait_impl(ty);
87-
} else {
88-
TyFingerprint::Unit
85+
match first_ty {
86+
Some(ty) => return TyFingerprint::for_trait_impl(ty),
87+
None => TyFingerprint::Unit,
8988
}
9089
}
9190
TyKind::AssociatedType(_, _)

crates/hir_ty/src/tests.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,9 @@ fn check_impl(ra_fixture: &str, allow_none: bool, only_types: bool, display_sour
195195
mismatch.expected.display_test(&db),
196196
mismatch.actual.display_test(&db)
197197
);
198-
if let Some(annotation) = mismatches.remove(&range) {
199-
assert_eq!(actual, annotation);
200-
} else {
201-
format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual);
198+
match mismatches.remove(&range) {
199+
Some(annotation) => assert_eq!(actual, annotation),
200+
None => format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual),
202201
}
203202
}
204203
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
215214
mismatch.expected.display_test(&db),
216215
mismatch.actual.display_test(&db)
217216
);
218-
if let Some(annotation) = mismatches.remove(&range) {
219-
assert_eq!(actual, annotation);
220-
} else {
221-
format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual);
217+
match mismatches.remove(&range) {
218+
Some(annotation) => assert_eq!(actual, annotation),
219+
None => format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual),
222220
}
223221
}
224222
}

crates/ide/src/display/navigation_target.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,9 @@ impl TryToNav for hir::Impl {
292292
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
293293
let src = self.source(db)?;
294294
let derive_attr = self.is_builtin_derive(db);
295-
let frange = if let Some(item) = &derive_attr {
296-
item.syntax().original_file_range(db)
297-
} else {
298-
src.syntax().original_file_range(db)
295+
let frange = match &derive_attr {
296+
Some(item) => item.syntax().original_file_range(db),
297+
None => src.syntax().original_file_range(db),
299298
};
300299
let focus_range = if derive_attr.is_some() {
301300
None

crates/ide/src/join_lines.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,9 @@ fn remove_newline(
136136
}
137137
T!['}'] => {
138138
// Removes: comma, newline (incl. surrounding whitespace)
139-
let space = if let Some(left) = prev.prev_sibling_or_token() {
140-
compute_ws(left.kind(), next.kind())
141-
} else {
142-
" "
139+
let space = match prev.prev_sibling_or_token() {
140+
Some(left) => compute_ws(left.kind(), next.kind()),
141+
None => " ",
143142
};
144143
edit.replace(
145144
TextRange::new(prev.text_range().start(), token.text_range().end()),

0 commit comments

Comments
 (0)