1
Fork 0

prefer if let to match with None => { } arm in some places

In #34268 (8531d581), 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 #34638 (d37edef9), 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?
This commit is contained in:
Zack M. Davis 2016-10-17 19:00:20 -07:00
parent e0111758eb
commit 1e7cd5edcc
11 changed files with 97 additions and 136 deletions

View file

@ -61,9 +61,8 @@ impl<'a, 'gcx, 'tcx> TypeFreshener<'a, 'gcx, 'tcx> {
-> Ty<'tcx> where -> Ty<'tcx> where
F: FnOnce(u32) -> ty::InferTy, F: FnOnce(u32) -> ty::InferTy,
{ {
match opt_ty { if let Some(ty) = opt_ty {
Some(ty) => { return ty.fold_with(self); } return ty.fold_with(self);
None => { }
} }
match self.freshen_map.entry(key) { match self.freshen_map.entry(key) {

View file

@ -478,12 +478,9 @@ impl RegionMaps {
//! Returns the scope when temp created by expr_id will be cleaned up //! Returns the scope when temp created by expr_id will be cleaned up
// check for a designated rvalue scope // check for a designated rvalue scope
match self.rvalue_scopes.borrow().get(&expr_id) { if let Some(&s) = self.rvalue_scopes.borrow().get(&expr_id) {
Some(&s) => { debug!("temporary_scope({:?}) = {:?} [custom]", expr_id, s);
debug!("temporary_scope({:?}) = {:?} [custom]", expr_id, s); return Some(s);
return Some(s);
}
None => { }
} }
let scope_map : &[CodeExtent] = &self.scope_map.borrow(); let scope_map : &[CodeExtent] = &self.scope_map.borrow();
@ -928,19 +925,15 @@ fn resolve_local(visitor: &mut RegionResolutionVisitor, local: &hir::Local) {
// //
// FIXME(#6308) -- Note that `[]` patterns work more smoothly post-DST. // FIXME(#6308) -- Note that `[]` patterns work more smoothly post-DST.
match local.init { if let Some(ref expr) = local.init {
Some(ref expr) => { record_rvalue_scope_if_borrow_expr(visitor, &expr, blk_scope);
record_rvalue_scope_if_borrow_expr(visitor, &expr, blk_scope);
let is_borrow = let is_borrow =
if let Some(ref ty) = local.ty { is_borrowed_ty(&ty) } else { false }; if let Some(ref ty) = local.ty { is_borrowed_ty(&ty) } else { false };
if is_binding_pat(&local.pat) || is_borrow { if is_binding_pat(&local.pat) || is_borrow {
record_rvalue_scope(visitor, &expr, blk_scope); record_rvalue_scope(visitor, &expr, blk_scope);
}
} }
None => { }
} }
intravisit::walk_local(visitor, local); intravisit::walk_local(visitor, local);
@ -1023,16 +1016,12 @@ fn resolve_local(visitor: &mut RegionResolutionVisitor, local: &hir::Local) {
record_rvalue_scope_if_borrow_expr(visitor, &subexpr, blk_id) record_rvalue_scope_if_borrow_expr(visitor, &subexpr, blk_id)
} }
hir::ExprBlock(ref block) => { hir::ExprBlock(ref block) => {
match block.expr { if let Some(ref subexpr) = block.expr {
Some(ref subexpr) => { record_rvalue_scope_if_borrow_expr(
record_rvalue_scope_if_borrow_expr( visitor, &subexpr, blk_id);
visitor, &subexpr, blk_id);
}
None => { }
} }
} }
_ => { _ => {}
}
} }
} }

View file

@ -1405,9 +1405,8 @@ impl<'tcx> ProjectionCache<'tcx> {
/// cache hit, so it's actually a good thing). /// cache hit, so it's actually a good thing).
fn try_start(&mut self, key: ty::ProjectionTy<'tcx>) fn try_start(&mut self, key: ty::ProjectionTy<'tcx>)
-> Result<(), ProjectionCacheEntry<'tcx>> { -> Result<(), ProjectionCacheEntry<'tcx>> {
match self.map.get(&key) { if let Some(entry) = self.map.get(&key) {
Some(entry) => return Err(entry.clone()), return Err(entry.clone());
None => { }
} }
self.map.insert(key, ProjectionCacheEntry::InProgress); self.map.insert(key, ProjectionCacheEntry::InProgress);

View file

@ -788,14 +788,11 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
stack); stack);
assert!(!stack.obligation.predicate.has_escaping_regions()); assert!(!stack.obligation.predicate.has_escaping_regions());
match self.check_candidate_cache(&cache_fresh_trait_pred) { if let Some(c) = self.check_candidate_cache(&cache_fresh_trait_pred) {
Some(c) => { debug!("CACHE HIT: SELECT({:?})={:?}",
debug!("CACHE HIT: SELECT({:?})={:?}", cache_fresh_trait_pred,
cache_fresh_trait_pred, c);
c); return c;
return c;
}
None => { }
} }
// If no match, compute result and insert into cache. // If no match, compute result and insert into cache.

View file

@ -135,15 +135,12 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for CheckLoanCtxt<'a, 'tcx> {
borrow_id, cmt, loan_region, borrow_id, cmt, loan_region,
bk, loan_cause); bk, loan_cause);
match opt_loan_path(&cmt) { if let Some(lp) = opt_loan_path(&cmt) {
Some(lp) => { let moved_value_use_kind = match loan_cause {
let moved_value_use_kind = match loan_cause { euv::ClosureCapture(_) => MovedInCapture,
euv::ClosureCapture(_) => MovedInCapture, _ => MovedInUse,
_ => MovedInUse, };
}; self.check_if_path_is_moved(borrow_id, borrow_span, moved_value_use_kind, &lp);
self.check_if_path_is_moved(borrow_id, borrow_span, moved_value_use_kind, &lp);
}
None => { }
} }
self.check_for_conflicting_loans(borrow_id); self.check_for_conflicting_loans(borrow_id);
@ -158,33 +155,29 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for CheckLoanCtxt<'a, 'tcx> {
debug!("mutate(assignment_id={}, assignee_cmt={:?})", debug!("mutate(assignment_id={}, assignee_cmt={:?})",
assignment_id, assignee_cmt); assignment_id, assignee_cmt);
match opt_loan_path(&assignee_cmt) { if let Some(lp) = opt_loan_path(&assignee_cmt) {
Some(lp) => { match mode {
match mode { MutateMode::Init | MutateMode::JustWrite => {
MutateMode::Init | MutateMode::JustWrite => { // In a case like `path = 1`, then path does not
// In a case like `path = 1`, then path does not // have to be *FULLY* initialized, but we still
// have to be *FULLY* initialized, but we still // must be careful lest it contains derefs of
// must be careful lest it contains derefs of // pointers.
// pointers. self.check_if_assigned_path_is_moved(assignee_cmt.id,
self.check_if_assigned_path_is_moved(assignee_cmt.id, assignment_span,
assignment_span, MovedInUse,
MovedInUse, &lp);
&lp); }
} MutateMode::WriteAndRead => {
MutateMode::WriteAndRead => { // In a case like `path += 1`, then path must be
// In a case like `path += 1`, then path must be // fully initialized, since we will read it before
// fully initialized, since we will read it before // we write it.
// we write it. self.check_if_path_is_moved(assignee_cmt.id,
self.check_if_path_is_moved(assignee_cmt.id, assignment_span,
assignment_span, MovedInUse,
MovedInUse, &lp);
&lp);
}
} }
} }
None => { }
} }
self.check_assignment(assignment_id, assignment_span, assignee_cmt); self.check_assignment(assignment_id, assignment_span, assignee_cmt);
} }
@ -601,39 +594,36 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
span: Span, span: Span,
cmt: mc::cmt<'tcx>, cmt: mc::cmt<'tcx>,
mode: euv::ConsumeMode) { mode: euv::ConsumeMode) {
match opt_loan_path(&cmt) { if let Some(lp) = opt_loan_path(&cmt) {
Some(lp) => { let moved_value_use_kind = match mode {
let moved_value_use_kind = match mode { euv::Copy => {
euv::Copy => { self.check_for_copy_of_frozen_path(id, span, &lp);
self.check_for_copy_of_frozen_path(id, span, &lp); MovedInUse
MovedInUse }
} euv::Move(_) => {
euv::Move(_) => { match self.move_data.kind_of_move_of_path(id, &lp) {
match self.move_data.kind_of_move_of_path(id, &lp) { None => {
None => { // Sometimes moves don't have a move kind;
// Sometimes moves don't have a move kind; // this either means that the original move
// this either means that the original move // was from something illegal to move,
// was from something illegal to move, // or was moved from referent of an unsafe
// or was moved from referent of an unsafe // pointer or something like that.
// pointer or something like that. MovedInUse
}
Some(move_kind) => {
self.check_for_move_of_borrowed_path(id, span,
&lp, move_kind);
if move_kind == move_data::Captured {
MovedInCapture
} else {
MovedInUse MovedInUse
} }
Some(move_kind) => {
self.check_for_move_of_borrowed_path(id, span,
&lp, move_kind);
if move_kind == move_data::Captured {
MovedInCapture
} else {
MovedInUse
}
}
} }
} }
}; }
};
self.check_if_path_is_moved(id, span, moved_value_use_kind, &lp); self.check_if_path_is_moved(id, span, moved_value_use_kind, &lp);
}
None => { }
} }
} }

View file

@ -302,9 +302,8 @@ fn trans_fn_pointer_shim<'a, 'tcx>(
}; };
// Check if we already trans'd this shim. // Check if we already trans'd this shim.
match ccx.fn_pointer_shims().borrow().get(&bare_fn_ty_maybe_ref) { if let Some(&llval) = ccx.fn_pointer_shims().borrow().get(&bare_fn_ty_maybe_ref) {
Some(&llval) => { return llval; } return llval;
None => { }
} }
debug!("trans_fn_pointer_shim(bare_fn_ty={:?})", debug!("trans_fn_pointer_shim(bare_fn_ty={:?})",

View file

@ -119,9 +119,8 @@ pub fn get_vtable<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
debug!("get_vtable(trait_ref={:?})", trait_ref); debug!("get_vtable(trait_ref={:?})", trait_ref);
// Check the cache. // Check the cache.
match ccx.vtables().borrow().get(&trait_ref) { if let Some(&val) = ccx.vtables().borrow().get(&trait_ref) {
Some(&val) => { return val } return val;
None => { }
} }
// Not in the cache. Build it. // Not in the cache. Build it.

View file

@ -1629,9 +1629,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
let tcx = self.tcx(); let tcx = self.tcx();
let cache = self.ast_ty_to_ty_cache(); let cache = self.ast_ty_to_ty_cache();
match cache.borrow().get(&ast_ty.id) { if let Some(ty) = cache.borrow().get(&ast_ty.id) {
Some(ty) => { return ty; } return ty;
None => { }
} }
let result_ty = match ast_ty.node { let result_ty = match ast_ty.node {

View file

@ -742,17 +742,14 @@ pub fn check_item_type<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, it: &'tcx hir::Item) {
hir::ItemImpl(.., ref impl_items) => { hir::ItemImpl(.., ref impl_items) => {
debug!("ItemImpl {} with id {}", it.name, it.id); debug!("ItemImpl {} with id {}", it.name, it.id);
let impl_def_id = ccx.tcx.map.local_def_id(it.id); let impl_def_id = ccx.tcx.map.local_def_id(it.id);
match ccx.tcx.impl_trait_ref(impl_def_id) { if let Some(impl_trait_ref) = ccx.tcx.impl_trait_ref(impl_def_id) {
Some(impl_trait_ref) => { check_impl_items_against_trait(ccx,
check_impl_items_against_trait(ccx, it.span,
it.span, impl_def_id,
impl_def_id, &impl_trait_ref,
&impl_trait_ref, impl_items);
impl_items); let trait_def_id = impl_trait_ref.def_id;
let trait_def_id = impl_trait_ref.def_id; check_on_unimplemented(ccx, trait_def_id, it);
check_on_unimplemented(ccx, trait_def_id, it);
}
None => { }
} }
} }
hir::ItemTrait(..) => { hir::ItemTrait(..) => {
@ -1812,9 +1809,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
f: F) where f: F) where
F: FnOnce(&ty::ItemSubsts<'tcx>), F: FnOnce(&ty::ItemSubsts<'tcx>),
{ {
match self.tables.borrow().item_substs.get(&id) { if let Some(s) = self.tables.borrow().item_substs.get(&id) {
Some(s) => { f(s) } f(s);
None => { }
} }
} }

View file

@ -156,13 +156,10 @@ impl<'a,'tcx> CrateCtxt<'a,'tcx> {
{ {
{ {
let mut stack = self.stack.borrow_mut(); let mut stack = self.stack.borrow_mut();
match stack.iter().enumerate().rev().find(|&(_, r)| *r == request) { if let Some((i, _)) = stack.iter().enumerate().rev().find(|&(_, r)| *r == request) {
None => { } let cycle = &stack[i..];
Some((i, _)) => { self.report_cycle(span, cycle);
let cycle = &stack[i..]; return Err(ErrorReported);
self.report_cycle(span, cycle);
return Err(ErrorReported);
}
} }
stack.push(request); stack.push(request);
} }

View file

@ -2450,13 +2450,10 @@ impl<'a> State<'a> {
|s, ty| s.print_type(&ty))); |s, ty| s.print_type(&ty)));
try!(word(&mut self.s, ")")); try!(word(&mut self.s, ")"));
match data.output { if let Some(ref ty) = data.output {
None => { } try!(self.space_if_not_bol());
Some(ref ty) => { try!(self.word_space("->"));
try!(self.space_if_not_bol()); try!(self.print_type(&ty));
try!(self.word_space("->"));
try!(self.print_type(&ty));
}
} }
} }
} }