Skip to content

Commit 1e7cd5e

Browse files
committed
prefer if let to match with None => { } arm in some places
In rust-lang#34268 (8531d58), we replaced matches of None to the unit value `()` with `if let`s in places where it was deemed that this made the code unambiguously clearer and more idiomatic. In rust-lang#34638 (d37edef), we did the same for matches of None to the empty block `{}`. A casual observer, upon seeing these commits fly by, might suppose that the matter was then settled, that no further pull requests on this utterly trivial point of style could or would be made. Unless ... It turns out that sometimes people write the empty block with a space in between the braces. Who knew?
1 parent e011175 commit 1e7cd5e

File tree

11 files changed

+97
-136
lines changed

11 files changed

+97
-136
lines changed

src/librustc/infer/freshen.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ impl<'a, 'gcx, 'tcx> TypeFreshener<'a, 'gcx, 'tcx> {
6161
-> Ty<'tcx> where
6262
F: FnOnce(u32) -> ty::InferTy,
6363
{
64-
match opt_ty {
65-
Some(ty) => { return ty.fold_with(self); }
66-
None => { }
64+
if let Some(ty) = opt_ty {
65+
return ty.fold_with(self);
6766
}
6867

6968
match self.freshen_map.entry(key) {

src/librustc/middle/region.rs

+13-24
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,9 @@ impl RegionMaps {
478478
//! Returns the scope when temp created by expr_id will be cleaned up
479479
480480
// check for a designated rvalue scope
481-
match self.rvalue_scopes.borrow().get(&expr_id) {
482-
Some(&s) => {
483-
debug!("temporary_scope({:?}) = {:?} [custom]", expr_id, s);
484-
return Some(s);
485-
}
486-
None => { }
481+
if let Some(&s) = self.rvalue_scopes.borrow().get(&expr_id) {
482+
debug!("temporary_scope({:?}) = {:?} [custom]", expr_id, s);
483+
return Some(s);
487484
}
488485

489486
let scope_map : &[CodeExtent] = &self.scope_map.borrow();
@@ -928,19 +925,15 @@ fn resolve_local(visitor: &mut RegionResolutionVisitor, local: &hir::Local) {
928925
//
929926
// FIXME(#6308) -- Note that `[]` patterns work more smoothly post-DST.
930927

931-
match local.init {
932-
Some(ref expr) => {
933-
record_rvalue_scope_if_borrow_expr(visitor, &expr, blk_scope);
928+
if let Some(ref expr) = local.init {
929+
record_rvalue_scope_if_borrow_expr(visitor, &expr, blk_scope);
934930

935-
let is_borrow =
936-
if let Some(ref ty) = local.ty { is_borrowed_ty(&ty) } else { false };
931+
let is_borrow =
932+
if let Some(ref ty) = local.ty { is_borrowed_ty(&ty) } else { false };
937933

938-
if is_binding_pat(&local.pat) || is_borrow {
939-
record_rvalue_scope(visitor, &expr, blk_scope);
940-
}
934+
if is_binding_pat(&local.pat) || is_borrow {
935+
record_rvalue_scope(visitor, &expr, blk_scope);
941936
}
942-
943-
None => { }
944937
}
945938

946939
intravisit::walk_local(visitor, local);
@@ -1023,16 +1016,12 @@ fn resolve_local(visitor: &mut RegionResolutionVisitor, local: &hir::Local) {
10231016
record_rvalue_scope_if_borrow_expr(visitor, &subexpr, blk_id)
10241017
}
10251018
hir::ExprBlock(ref block) => {
1026-
match block.expr {
1027-
Some(ref subexpr) => {
1028-
record_rvalue_scope_if_borrow_expr(
1029-
visitor, &subexpr, blk_id);
1030-
}
1031-
None => { }
1019+
if let Some(ref subexpr) = block.expr {
1020+
record_rvalue_scope_if_borrow_expr(
1021+
visitor, &subexpr, blk_id);
10321022
}
10331023
}
1034-
_ => {
1035-
}
1024+
_ => {}
10361025
}
10371026
}
10381027

src/librustc/traits/project.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1405,9 +1405,8 @@ impl<'tcx> ProjectionCache<'tcx> {
14051405
/// cache hit, so it's actually a good thing).
14061406
fn try_start(&mut self, key: ty::ProjectionTy<'tcx>)
14071407
-> Result<(), ProjectionCacheEntry<'tcx>> {
1408-
match self.map.get(&key) {
1409-
Some(entry) => return Err(entry.clone()),
1410-
None => { }
1408+
if let Some(entry) = self.map.get(&key) {
1409+
return Err(entry.clone());
14111410
}
14121411

14131412
self.map.insert(key, ProjectionCacheEntry::InProgress);

src/librustc/traits/select.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -788,14 +788,11 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
788788
stack);
789789
assert!(!stack.obligation.predicate.has_escaping_regions());
790790

791-
match self.check_candidate_cache(&cache_fresh_trait_pred) {
792-
Some(c) => {
793-
debug!("CACHE HIT: SELECT({:?})={:?}",
794-
cache_fresh_trait_pred,
795-
c);
796-
return c;
797-
}
798-
None => { }
791+
if let Some(c) = self.check_candidate_cache(&cache_fresh_trait_pred) {
792+
debug!("CACHE HIT: SELECT({:?})={:?}",
793+
cache_fresh_trait_pred,
794+
c);
795+
return c;
799796
}
800797

801798
// If no match, compute result and insert into cache.

src/librustc_borrowck/borrowck/check_loans.rs

+51-61
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,12 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for CheckLoanCtxt<'a, 'tcx> {
135135
borrow_id, cmt, loan_region,
136136
bk, loan_cause);
137137

138-
match opt_loan_path(&cmt) {
139-
Some(lp) => {
140-
let moved_value_use_kind = match loan_cause {
141-
euv::ClosureCapture(_) => MovedInCapture,
142-
_ => MovedInUse,
143-
};
144-
self.check_if_path_is_moved(borrow_id, borrow_span, moved_value_use_kind, &lp);
145-
}
146-
None => { }
138+
if let Some(lp) = opt_loan_path(&cmt) {
139+
let moved_value_use_kind = match loan_cause {
140+
euv::ClosureCapture(_) => MovedInCapture,
141+
_ => MovedInUse,
142+
};
143+
self.check_if_path_is_moved(borrow_id, borrow_span, moved_value_use_kind, &lp);
147144
}
148145

149146
self.check_for_conflicting_loans(borrow_id);
@@ -158,33 +155,29 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for CheckLoanCtxt<'a, 'tcx> {
158155
debug!("mutate(assignment_id={}, assignee_cmt={:?})",
159156
assignment_id, assignee_cmt);
160157

161-
match opt_loan_path(&assignee_cmt) {
162-
Some(lp) => {
163-
match mode {
164-
MutateMode::Init | MutateMode::JustWrite => {
165-
// In a case like `path = 1`, then path does not
166-
// have to be *FULLY* initialized, but we still
167-
// must be careful lest it contains derefs of
168-
// pointers.
169-
self.check_if_assigned_path_is_moved(assignee_cmt.id,
170-
assignment_span,
171-
MovedInUse,
172-
&lp);
173-
}
174-
MutateMode::WriteAndRead => {
175-
// In a case like `path += 1`, then path must be
176-
// fully initialized, since we will read it before
177-
// we write it.
178-
self.check_if_path_is_moved(assignee_cmt.id,
179-
assignment_span,
180-
MovedInUse,
181-
&lp);
182-
}
158+
if let Some(lp) = opt_loan_path(&assignee_cmt) {
159+
match mode {
160+
MutateMode::Init | MutateMode::JustWrite => {
161+
// In a case like `path = 1`, then path does not
162+
// have to be *FULLY* initialized, but we still
163+
// must be careful lest it contains derefs of
164+
// pointers.
165+
self.check_if_assigned_path_is_moved(assignee_cmt.id,
166+
assignment_span,
167+
MovedInUse,
168+
&lp);
169+
}
170+
MutateMode::WriteAndRead => {
171+
// In a case like `path += 1`, then path must be
172+
// fully initialized, since we will read it before
173+
// we write it.
174+
self.check_if_path_is_moved(assignee_cmt.id,
175+
assignment_span,
176+
MovedInUse,
177+
&lp);
183178
}
184179
}
185-
None => { }
186180
}
187-
188181
self.check_assignment(assignment_id, assignment_span, assignee_cmt);
189182
}
190183

@@ -601,39 +594,36 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
601594
span: Span,
602595
cmt: mc::cmt<'tcx>,
603596
mode: euv::ConsumeMode) {
604-
match opt_loan_path(&cmt) {
605-
Some(lp) => {
606-
let moved_value_use_kind = match mode {
607-
euv::Copy => {
608-
self.check_for_copy_of_frozen_path(id, span, &lp);
609-
MovedInUse
610-
}
611-
euv::Move(_) => {
612-
match self.move_data.kind_of_move_of_path(id, &lp) {
613-
None => {
614-
// Sometimes moves don't have a move kind;
615-
// this either means that the original move
616-
// was from something illegal to move,
617-
// or was moved from referent of an unsafe
618-
// pointer or something like that.
597+
if let Some(lp) = opt_loan_path(&cmt) {
598+
let moved_value_use_kind = match mode {
599+
euv::Copy => {
600+
self.check_for_copy_of_frozen_path(id, span, &lp);
601+
MovedInUse
602+
}
603+
euv::Move(_) => {
604+
match self.move_data.kind_of_move_of_path(id, &lp) {
605+
None => {
606+
// Sometimes moves don't have a move kind;
607+
// this either means that the original move
608+
// was from something illegal to move,
609+
// or was moved from referent of an unsafe
610+
// pointer or something like that.
611+
MovedInUse
612+
}
613+
Some(move_kind) => {
614+
self.check_for_move_of_borrowed_path(id, span,
615+
&lp, move_kind);
616+
if move_kind == move_data::Captured {
617+
MovedInCapture
618+
} else {
619619
MovedInUse
620620
}
621-
Some(move_kind) => {
622-
self.check_for_move_of_borrowed_path(id, span,
623-
&lp, move_kind);
624-
if move_kind == move_data::Captured {
625-
MovedInCapture
626-
} else {
627-
MovedInUse
628-
}
629-
}
630621
}
631622
}
632-
};
623+
}
624+
};
633625

634-
self.check_if_path_is_moved(id, span, moved_value_use_kind, &lp);
635-
}
636-
None => { }
626+
self.check_if_path_is_moved(id, span, moved_value_use_kind, &lp);
637627
}
638628
}
639629

src/librustc_trans/callee.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,8 @@ fn trans_fn_pointer_shim<'a, 'tcx>(
302302
};
303303

304304
// Check if we already trans'd this shim.
305-
match ccx.fn_pointer_shims().borrow().get(&bare_fn_ty_maybe_ref) {
306-
Some(&llval) => { return llval; }
307-
None => { }
305+
if let Some(&llval) = ccx.fn_pointer_shims().borrow().get(&bare_fn_ty_maybe_ref) {
306+
return llval;
308307
}
309308

310309
debug!("trans_fn_pointer_shim(bare_fn_ty={:?})",

src/librustc_trans/meth.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ pub fn get_vtable<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
119119
debug!("get_vtable(trait_ref={:?})", trait_ref);
120120

121121
// Check the cache.
122-
match ccx.vtables().borrow().get(&trait_ref) {
123-
Some(&val) => { return val }
124-
None => { }
122+
if let Some(&val) = ccx.vtables().borrow().get(&trait_ref) {
123+
return val;
125124
}
126125

127126
// Not in the cache. Build it.

src/librustc_typeck/astconv.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1629,9 +1629,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
16291629
let tcx = self.tcx();
16301630

16311631
let cache = self.ast_ty_to_ty_cache();
1632-
match cache.borrow().get(&ast_ty.id) {
1633-
Some(ty) => { return ty; }
1634-
None => { }
1632+
if let Some(ty) = cache.borrow().get(&ast_ty.id) {
1633+
return ty;
16351634
}
16361635

16371636
let result_ty = match ast_ty.node {

src/librustc_typeck/check/mod.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -742,17 +742,14 @@ pub fn check_item_type<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, it: &'tcx hir::Item) {
742742
hir::ItemImpl(.., ref impl_items) => {
743743
debug!("ItemImpl {} with id {}", it.name, it.id);
744744
let impl_def_id = ccx.tcx.map.local_def_id(it.id);
745-
match ccx.tcx.impl_trait_ref(impl_def_id) {
746-
Some(impl_trait_ref) => {
747-
check_impl_items_against_trait(ccx,
748-
it.span,
749-
impl_def_id,
750-
&impl_trait_ref,
751-
impl_items);
752-
let trait_def_id = impl_trait_ref.def_id;
753-
check_on_unimplemented(ccx, trait_def_id, it);
754-
}
755-
None => { }
745+
if let Some(impl_trait_ref) = ccx.tcx.impl_trait_ref(impl_def_id) {
746+
check_impl_items_against_trait(ccx,
747+
it.span,
748+
impl_def_id,
749+
&impl_trait_ref,
750+
impl_items);
751+
let trait_def_id = impl_trait_ref.def_id;
752+
check_on_unimplemented(ccx, trait_def_id, it);
756753
}
757754
}
758755
hir::ItemTrait(..) => {
@@ -1812,9 +1809,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
18121809
f: F) where
18131810
F: FnOnce(&ty::ItemSubsts<'tcx>),
18141811
{
1815-
match self.tables.borrow().item_substs.get(&id) {
1816-
Some(s) => { f(s) }
1817-
None => { }
1812+
if let Some(s) = self.tables.borrow().item_substs.get(&id) {
1813+
f(s);
18181814
}
18191815
}
18201816

src/librustc_typeck/collect.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,10 @@ impl<'a,'tcx> CrateCtxt<'a,'tcx> {
156156
{
157157
{
158158
let mut stack = self.stack.borrow_mut();
159-
match stack.iter().enumerate().rev().find(|&(_, r)| *r == request) {
160-
None => { }
161-
Some((i, _)) => {
162-
let cycle = &stack[i..];
163-
self.report_cycle(span, cycle);
164-
return Err(ErrorReported);
165-
}
159+
if let Some((i, _)) = stack.iter().enumerate().rev().find(|&(_, r)| *r == request) {
160+
let cycle = &stack[i..];
161+
self.report_cycle(span, cycle);
162+
return Err(ErrorReported);
166163
}
167164
stack.push(request);
168165
}

src/libsyntax/print/pprust.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -2450,13 +2450,10 @@ impl<'a> State<'a> {
24502450
|s, ty| s.print_type(&ty)));
24512451
try!(word(&mut self.s, ")"));
24522452

2453-
match data.output {
2454-
None => { }
2455-
Some(ref ty) => {
2456-
try!(self.space_if_not_bol());
2457-
try!(self.word_space("->"));
2458-
try!(self.print_type(&ty));
2459-
}
2453+
if let Some(ref ty) = data.output {
2454+
try!(self.space_if_not_bol());
2455+
try!(self.word_space("->"));
2456+
try!(self.print_type(&ty));
24602457
}
24612458
}
24622459
}

0 commit comments

Comments
 (0)