prefer if let to match with None => () arm in some places

Casual grepping revealed some places in the codebase (some of which
antedated `if let`'s December 2014 stabilization in c200ae5a) where we
were using a match with a `None => ()` arm where (in the present
author's opinion) an `if let` conditional would be more readable. (Other
places where matching to the unit value did seem to better express the
intent were left alone.)

It's likely that we don't care about making such trivial,
non-functional, sheerly æsthetic changes.

But if we do, this is a patch.
This commit is contained in:
Zack M. Davis 2016-06-13 22:43:30 -07:00
parent 6551acc8e5
commit 8531d58104
11 changed files with 98 additions and 136 deletions

View File

@ -1728,12 +1728,9 @@ impl<'a> State<'a> {
} }
} }
self.print_name(path1.node)?; self.print_name(path1.node)?;
match *sub { if let Some(ref p) = *sub {
Some(ref p) => { word(&mut self.s, "@")?;
word(&mut self.s, "@")?; self.print_pat(&p)?;
self.print_pat(&p)?;
}
None => (),
} }
} }
PatKind::TupleStruct(ref path, ref elts, ddpos) => { PatKind::TupleStruct(ref path, ref elts, ddpos) => {
@ -2246,25 +2243,21 @@ impl<'a> State<'a> {
Some(cm) => cm, Some(cm) => cm,
_ => return Ok(()), _ => return Ok(()),
}; };
match self.next_comment() { if let Some(ref cmnt) = self.next_comment() {
Some(ref cmnt) => { if (*cmnt).style != comments::Trailing {
if (*cmnt).style != comments::Trailing { return Ok(());
return Ok(()); }
} let span_line = cm.lookup_char_pos(span.hi);
let span_line = cm.lookup_char_pos(span.hi); let comment_line = cm.lookup_char_pos((*cmnt).pos);
let comment_line = cm.lookup_char_pos((*cmnt).pos); let mut next = (*cmnt).pos + BytePos(1);
let mut next = (*cmnt).pos + BytePos(1); if let Some(p) = next_pos {
match next_pos { next = p;
None => (), }
Some(p) => next = p, if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
} span_line.line == comment_line.line {
if span.hi < (*cmnt).pos && (*cmnt).pos < next && self.print_comment(cmnt)?;
span_line.line == comment_line.line { self.cur_cmnt_and_lit.cur_cmnt += 1;
self.print_comment(cmnt)?;
self.cur_cmnt_and_lit.cur_cmnt += 1;
}
} }
_ => (),
} }
Ok(()) Ok(())
} }

View File

@ -1856,11 +1856,10 @@ fn lifetimes_in_scope<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
}, },
None => None None => None
}; };
if method_id_opt.is_some() { if let Some(method_id) = method_id_opt {
let method_id = method_id_opt.unwrap();
let parent = tcx.map.get_parent(method_id); let parent = tcx.map.get_parent(method_id);
match tcx.map.find(parent) { if let Some(node) = tcx.map.find(parent) {
Some(node) => match node { match node {
ast_map::NodeItem(item) => match item.node { ast_map::NodeItem(item) => match item.node {
hir::ItemImpl(_, _, ref gen, _, _, _) => { hir::ItemImpl(_, _, ref gen, _, _, _) => {
taken.extend_from_slice(&gen.lifetimes); taken.extend_from_slice(&gen.lifetimes);
@ -1868,8 +1867,7 @@ fn lifetimes_in_scope<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
_ => () _ => ()
}, },
_ => () _ => ()
}, }
None => ()
} }
} }
return taken; return taken;
@ -1938,4 +1936,3 @@ fn name_to_dummy_lifetime(name: ast::Name) -> hir::Lifetime {
span: codemap::DUMMY_SP, span: codemap::DUMMY_SP,
name: name } name: name }
} }

View File

@ -160,12 +160,9 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
} }
scanned.insert(id); scanned.insert(id);
match self.tcx.map.find(id) { if let Some(ref node) = self.tcx.map.find(id) {
Some(ref node) => { self.live_symbols.insert(id);
self.live_symbols.insert(id); self.visit_node(node);
self.visit_node(node);
}
None => (),
} }
} }
} }
@ -372,9 +369,8 @@ fn create_and_seed_worklist<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
} }
// Seed entry point // Seed entry point
match *tcx.sess.entry_fn.borrow() { if let Some((id, _)) = *tcx.sess.entry_fn.borrow() {
Some((id, _)) => worklist.push(id), worklist.push(id);
None => ()
} }
// Seed implemented trait items // Seed implemented trait items
@ -464,16 +460,14 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
// method of a private type is used, but the type itself is never // method of a private type is used, but the type itself is never
// called directly. // called directly.
let impl_items = self.tcx.impl_items.borrow(); let impl_items = self.tcx.impl_items.borrow();
match self.tcx.inherent_impls.borrow().get(&self.tcx.map.local_def_id(id)) { if let Some(impl_list) =
None => (), self.tcx.inherent_impls.borrow().get(&self.tcx.map.local_def_id(id)) {
Some(impl_list) => { for impl_did in impl_list.iter() {
for impl_did in impl_list.iter() { for item_did in impl_items.get(impl_did).unwrap().iter() {
for item_did in impl_items.get(impl_did).unwrap().iter() { if let Some(item_node_id) =
if let Some(item_node_id) = self.tcx.map.as_local_node_id(item_did.def_id()) {
self.tcx.map.as_local_node_id(item_did.def_id()) { if self.live_symbols.contains(&item_node_id) {
if self.live_symbols.contains(&item_node_id) { return true;
return true;
}
} }
} }
} }

View File

@ -122,15 +122,12 @@ fn gather_move<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
let potentially_illegal_move = let potentially_illegal_move =
check_and_get_illegal_move_origin(bccx, &move_info.cmt); check_and_get_illegal_move_origin(bccx, &move_info.cmt);
match potentially_illegal_move { if let Some(illegal_move_origin) = potentially_illegal_move {
Some(illegal_move_origin) => { debug!("illegal_move_origin={:?}", illegal_move_origin);
debug!("illegal_move_origin={:?}", illegal_move_origin); let error = MoveError::with_move_info(illegal_move_origin,
let error = MoveError::with_move_info(illegal_move_origin, move_info.span_path_opt);
move_info.span_path_opt); move_error_collector.add_error(error);
move_error_collector.add_error(error); return;
return
}
None => ()
} }
match opt_loan_path(&move_info.cmt) { match opt_loan_path(&move_info.cmt) {

View File

@ -929,29 +929,26 @@ impl<'a> LocalCrateReader<'a> {
return; return;
} }
match self.creader.extract_crate_info(i) { if let Some(info) = self.creader.extract_crate_info(i) {
Some(info) => { let (cnum, _, _) = self.creader.resolve_crate(&None,
let (cnum, _, _) = self.creader.resolve_crate(&None, &info.ident,
&info.ident, &info.name,
&info.name, None,
None, i.span,
i.span, PathKind::Crate,
PathKind::Crate, true);
true);
let def_id = self.definitions.opt_local_def_id(i.id).unwrap(); let def_id = self.definitions.opt_local_def_id(i.id).unwrap();
let len = self.definitions.def_path(def_id.index).data.len(); let len = self.definitions.def_path(def_id.index).data.len();
self.creader.update_extern_crate(cnum, self.creader.update_extern_crate(cnum,
ExternCrate { ExternCrate {
def_id: def_id, def_id: def_id,
span: i.span, span: i.span,
direct: true, direct: true,
path_len: len, path_len: len,
}); });
self.cstore.add_extern_mod_stmt_cnum(info.id, cnum); self.cstore.add_extern_mod_stmt_cnum(info.id, cnum);
}
None => ()
} }
} }
ast::ItemKind::ForeignMod(ref fm) => self.process_foreign_mod(i, fm), ast::ItemKind::ForeignMod(ref fm) => self.process_foreign_mod(i, fm),

View File

@ -2252,17 +2252,14 @@ pub fn update_linkage(ccx: &CrateContext,
} }
fn set_global_section(ccx: &CrateContext, llval: ValueRef, i: &hir::Item) { fn set_global_section(ccx: &CrateContext, llval: ValueRef, i: &hir::Item) {
match attr::first_attr_value_str_by_name(&i.attrs, "link_section") { if let Some(sect) = attr::first_attr_value_str_by_name(&i.attrs, "link_section") {
Some(sect) => { if contains_null(&sect) {
if contains_null(&sect) { ccx.sess().fatal(&format!("Illegal null byte in link_section value: `{}`", &sect));
ccx.sess().fatal(&format!("Illegal null byte in link_section value: `{}`", &sect)); }
} unsafe {
unsafe { let buf = CString::new(sect.as_bytes()).unwrap();
let buf = CString::new(sect.as_bytes()).unwrap(); llvm::LLVMSetSection(llval, buf.as_ptr());
llvm::LLVMSetSection(llval, buf.as_ptr()); }
}
},
None => ()
} }
} }

View File

@ -874,9 +874,8 @@ pub fn unknown_file_metadata(cx: &CrateContext) -> DIFile {
} }
fn file_metadata_(cx: &CrateContext, key: &str, file_name: &str, work_dir: &str) -> DIFile { fn file_metadata_(cx: &CrateContext, key: &str, file_name: &str, work_dir: &str) -> DIFile {
match debug_context(cx).created_files.borrow().get(key) { if let Some(file_metadata) = debug_context(cx).created_files.borrow().get(key) {
Some(file_metadata) => return *file_metadata, return *file_metadata;
None => ()
} }
debug!("file_metadata: file_name: {}, work_dir: {}", file_name, work_dir); debug!("file_metadata: file_name: {}, work_dir: {}", file_name, work_dir);

View File

@ -50,12 +50,9 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
let mono_ty = apply_param_substs(ccx.tcx(), psubsts, &item_ty); let mono_ty = apply_param_substs(ccx.tcx(), psubsts, &item_ty);
debug!("mono_ty = {:?} (post-substitution)", mono_ty); debug!("mono_ty = {:?} (post-substitution)", mono_ty);
match ccx.instances().borrow().get(&instance) { if let Some(&val) = ccx.instances().borrow().get(&instance) {
Some(&val) => { debug!("leaving monomorphic fn {:?}", instance);
debug!("leaving monomorphic fn {:?}", instance); return (val, mono_ty);
return (val, mono_ty);
}
None => ()
} }
debug!("monomorphic_fn({:?})", instance); debug!("monomorphic_fn({:?})", instance);

View File

@ -5430,18 +5430,15 @@ impl<'a> Parser<'a> {
name: String, name: String,
id_sp: Span) -> PResult<'a, (ast::ItemKind, Vec<ast::Attribute> )> { id_sp: Span) -> PResult<'a, (ast::ItemKind, Vec<ast::Attribute> )> {
let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut(); let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut();
match included_mod_stack.iter().position(|p| *p == path) { if let Some(i) = included_mod_stack.iter().position(|p| *p == path) {
Some(i) => { let mut err = String::from("circular modules: ");
let mut err = String::from("circular modules: "); let len = included_mod_stack.len();
let len = included_mod_stack.len(); for p in &included_mod_stack[i.. len] {
for p in &included_mod_stack[i.. len] { err.push_str(&p.to_string_lossy());
err.push_str(&p.to_string_lossy()); err.push_str(" -> ");
err.push_str(" -> ");
}
err.push_str(&path.to_string_lossy());
return Err(self.span_fatal(id_sp, &err[..]));
} }
None => () err.push_str(&path.to_string_lossy());
return Err(self.span_fatal(id_sp, &err[..]));
} }
included_mod_stack.push(path.clone()); included_mod_stack.push(path.clone());
drop(included_mod_stack); drop(included_mod_stack);

View File

@ -2459,12 +2459,9 @@ impl<'a> State<'a> {
} }
} }
self.print_ident(path1.node)?; self.print_ident(path1.node)?;
match *sub { if let Some(ref p) = *sub {
Some(ref p) => { word(&mut self.s, "@")?;
word(&mut self.s, "@")?; self.print_pat(&p)?;
self.print_pat(&p)?;
}
None => ()
} }
} }
PatKind::TupleStruct(ref path, ref elts, ddpos) => { PatKind::TupleStruct(ref path, ref elts, ddpos) => {
@ -3008,20 +3005,19 @@ impl<'a> State<'a> {
Some(cm) => cm, Some(cm) => cm,
_ => return Ok(()) _ => return Ok(())
}; };
match self.next_comment() { if let Some(ref cmnt) = self.next_comment() {
Some(ref cmnt) => { if (*cmnt).style != comments::Trailing { return Ok(()) }
if (*cmnt).style != comments::Trailing { return Ok(()) } let span_line = cm.lookup_char_pos(span.hi);
let span_line = cm.lookup_char_pos(span.hi); let comment_line = cm.lookup_char_pos((*cmnt).pos);
let comment_line = cm.lookup_char_pos((*cmnt).pos); let mut next = (*cmnt).pos + BytePos(1);
let mut next = (*cmnt).pos + BytePos(1); if let Some(p) = next_pos {
match next_pos { None => (), Some(p) => next = p } next = p;
if span.hi < (*cmnt).pos && (*cmnt).pos < next && }
span_line.line == comment_line.line { if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
self.print_comment(cmnt)?; span_line.line == comment_line.line {
self.cur_cmnt_and_lit.cur_cmnt += 1; self.print_comment(cmnt)?;
} self.cur_cmnt_and_lit.cur_cmnt += 1;
} }
_ => ()
} }
Ok(()) Ok(())
} }

View File

@ -47,9 +47,8 @@ impl<T: Eq + Hash + Clone + 'static> Interner<T> {
pub fn intern(&self, val: T) -> Name { pub fn intern(&self, val: T) -> Name {
let mut map = self.map.borrow_mut(); let mut map = self.map.borrow_mut();
match (*map).get(&val) { if let Some(&idx) = (*map).get(&val) {
Some(&idx) => return idx, return idx;
None => (),
} }
let mut vect = self.vect.borrow_mut(); let mut vect = self.vect.borrow_mut();
@ -161,9 +160,8 @@ impl StrInterner {
pub fn intern(&self, val: &str) -> Name { pub fn intern(&self, val: &str) -> Name {
let mut map = self.map.borrow_mut(); let mut map = self.map.borrow_mut();
match map.get(val) { if let Some(&idx) = map.get(val) {
Some(&idx) => return idx, return idx;
None => (),
} }
let new_idx = Name(self.len() as u32); let new_idx = Name(self.len() as u32);