Skip to content

Commit

Permalink
Replace if let with match where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
lunacookies committed Oct 4, 2021
1 parent f29796d commit 9583dd5
Show file tree
Hide file tree
Showing 44 changed files with 201 additions and 269 deletions.
7 changes: 3 additions & 4 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
28 changes: 12 additions & 16 deletions crates/hir_def/src/body/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -624,10 +623,9 @@ impl ExprCollector<'_> {
}

fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> 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(),
}
}

Expand Down Expand Up @@ -724,10 +722,9 @@ impl ExprCollector<'_> {
}

fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> 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(),
}
}

Expand Down Expand Up @@ -890,10 +887,9 @@ impl ExprCollector<'_> {
}

fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> 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(),
}
}

Expand Down
37 changes: 16 additions & 21 deletions crates/hir_def/src/find_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions crates/hir_def/src/item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,9 @@ macro_rules! mod_items {
}

fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> {
if let ModItem::$typ(id) = mod_item {
Some(id)
} else {
None
match mod_item {
ModItem::$typ(id) => Some(id),
_ => None,
}
}

Expand Down
11 changes: 4 additions & 7 deletions crates/hir_def/src/nameres/path_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions crates/hir_def/src/path/lower/lower_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ pub(crate) fn convert_path(
path: ast::Path,
hygiene: &Hygiene,
) -> Option<ModPath> {
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()?;
Expand Down
7 changes: 3 additions & 4 deletions crates/hir_def/src/type_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,9 @@ impl TypeRef {
}

pub(crate) fn from_ast_opt(ctx: &LowerCtx, node: Option<ast::Type>) -> 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,
}
}

Expand Down
7 changes: 3 additions & 4 deletions crates/hir_expand/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}

Expand Down
7 changes: 3 additions & 4 deletions crates/hir_ty/src/autoderef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,9 @@ pub(crate) fn deref(
ty: InEnvironment<&Canonical<Ty>>,
) -> Option<Canonical<Ty>> {
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),
}
}

Expand Down
7 changes: 3 additions & 4 deletions crates/hir_ty/src/chalk_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,9 @@ impl TyExt for Ty {
}

fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId> {
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)> {
Expand Down
7 changes: 3 additions & 4 deletions crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
}
}

Expand Down
7 changes: 3 additions & 4 deletions crates/hir_ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
7 changes: 3 additions & 4 deletions crates/hir_ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
7 changes: 3 additions & 4 deletions crates/hir_ty/src/infer/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
7 changes: 3 additions & 4 deletions crates/hir_ty/src/infer/unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions crates/hir_ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
7 changes: 3 additions & 4 deletions crates/hir_ty/src/method_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_, _)
Expand Down
14 changes: 6 additions & 8 deletions crates/hir_ty/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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),
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions crates/ide/src/display/navigation_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,9 @@ impl TryToNav for hir::Impl {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
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
Expand Down
7 changes: 3 additions & 4 deletions crates/ide/src/join_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
Loading

0 comments on commit 9583dd5

Please sign in to comment.