Skip to content

Commit 2675a55

Browse files
committed
Rollup merge of #34268 - zackmdavis:if_let_over_none_unit_arm, r=jseyfried
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.
2 parents d84993b + 8531d58 commit 2675a55

File tree

11 files changed

+101
-139
lines changed

11 files changed

+101
-139
lines changed

src/librustc/hir/print.rs

+17-24
Original file line numberDiff line numberDiff line change
@@ -1728,12 +1728,9 @@ impl<'a> State<'a> {
17281728
}
17291729
}
17301730
self.print_name(path1.node)?;
1731-
match *sub {
1732-
Some(ref p) => {
1733-
word(&mut self.s, "@")?;
1734-
self.print_pat(&p)?;
1735-
}
1736-
None => (),
1731+
if let Some(ref p) = *sub {
1732+
word(&mut self.s, "@")?;
1733+
self.print_pat(&p)?;
17371734
}
17381735
}
17391736
PatKind::TupleStruct(ref path, ref elts, ddpos) => {
@@ -2246,25 +2243,21 @@ impl<'a> State<'a> {
22462243
Some(cm) => cm,
22472244
_ => return Ok(()),
22482245
};
2249-
match self.next_comment() {
2250-
Some(ref cmnt) => {
2251-
if (*cmnt).style != comments::Trailing {
2252-
return Ok(());
2253-
}
2254-
let span_line = cm.lookup_char_pos(span.hi);
2255-
let comment_line = cm.lookup_char_pos((*cmnt).pos);
2256-
let mut next = (*cmnt).pos + BytePos(1);
2257-
match next_pos {
2258-
None => (),
2259-
Some(p) => next = p,
2260-
}
2261-
if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
2262-
span_line.line == comment_line.line {
2263-
self.print_comment(cmnt)?;
2264-
self.cur_cmnt_and_lit.cur_cmnt += 1;
2265-
}
2246+
if let Some(ref cmnt) = self.next_comment() {
2247+
if (*cmnt).style != comments::Trailing {
2248+
return Ok(());
2249+
}
2250+
let span_line = cm.lookup_char_pos(span.hi);
2251+
let comment_line = cm.lookup_char_pos((*cmnt).pos);
2252+
let mut next = (*cmnt).pos + BytePos(1);
2253+
if let Some(p) = next_pos {
2254+
next = p;
2255+
}
2256+
if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
2257+
span_line.line == comment_line.line {
2258+
self.print_comment(cmnt)?;
2259+
self.cur_cmnt_and_lit.cur_cmnt += 1;
22662260
}
2267-
_ => (),
22682261
}
22692262
Ok(())
22702263
}

src/librustc/infer/error_reporting.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1856,20 +1856,18 @@ fn lifetimes_in_scope<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
18561856
},
18571857
None => None
18581858
};
1859-
if method_id_opt.is_some() {
1860-
let method_id = method_id_opt.unwrap();
1859+
if let Some(method_id) = method_id_opt {
18611860
let parent = tcx.map.get_parent(method_id);
1862-
match tcx.map.find(parent) {
1863-
Some(node) => match node {
1861+
if let Some(node) = tcx.map.find(parent) {
1862+
match node {
18641863
ast_map::NodeItem(item) => match item.node {
18651864
hir::ItemImpl(_, _, ref gen, _, _, _) => {
18661865
taken.extend_from_slice(&gen.lifetimes);
18671866
}
18681867
_ => ()
18691868
},
18701869
_ => ()
1871-
},
1872-
None => ()
1870+
}
18731871
}
18741872
}
18751873
return taken;
@@ -1938,4 +1936,3 @@ fn name_to_dummy_lifetime(name: ast::Name) -> hir::Lifetime {
19381936
span: codemap::DUMMY_SP,
19391937
name: name }
19401938
}
1941-

src/librustc/middle/dead.rs

+13-19
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,9 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
160160
}
161161
scanned.insert(id);
162162

163-
match self.tcx.map.find(id) {
164-
Some(ref node) => {
165-
self.live_symbols.insert(id);
166-
self.visit_node(node);
167-
}
168-
None => (),
163+
if let Some(ref node) = self.tcx.map.find(id) {
164+
self.live_symbols.insert(id);
165+
self.visit_node(node);
169166
}
170167
}
171168
}
@@ -372,9 +369,8 @@ fn create_and_seed_worklist<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
372369
}
373370

374371
// Seed entry point
375-
match *tcx.sess.entry_fn.borrow() {
376-
Some((id, _)) => worklist.push(id),
377-
None => ()
372+
if let Some((id, _)) = *tcx.sess.entry_fn.borrow() {
373+
worklist.push(id);
378374
}
379375

380376
// Seed implemented trait items
@@ -464,16 +460,14 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
464460
// method of a private type is used, but the type itself is never
465461
// called directly.
466462
let impl_items = self.tcx.impl_items.borrow();
467-
match self.tcx.inherent_impls.borrow().get(&self.tcx.map.local_def_id(id)) {
468-
None => (),
469-
Some(impl_list) => {
470-
for impl_did in impl_list.iter() {
471-
for item_did in impl_items.get(impl_did).unwrap().iter() {
472-
if let Some(item_node_id) =
473-
self.tcx.map.as_local_node_id(item_did.def_id()) {
474-
if self.live_symbols.contains(&item_node_id) {
475-
return true;
476-
}
463+
if let Some(impl_list) =
464+
self.tcx.inherent_impls.borrow().get(&self.tcx.map.local_def_id(id)) {
465+
for impl_did in impl_list.iter() {
466+
for item_did in impl_items.get(impl_did).unwrap().iter() {
467+
if let Some(item_node_id) =
468+
self.tcx.map.as_local_node_id(item_did.def_id()) {
469+
if self.live_symbols.contains(&item_node_id) {
470+
return true;
477471
}
478472
}
479473
}

src/librustc_borrowck/borrowck/gather_loans/gather_moves.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,12 @@ fn gather_move<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
122122

123123
let potentially_illegal_move =
124124
check_and_get_illegal_move_origin(bccx, &move_info.cmt);
125-
match potentially_illegal_move {
126-
Some(illegal_move_origin) => {
127-
debug!("illegal_move_origin={:?}", illegal_move_origin);
128-
let error = MoveError::with_move_info(illegal_move_origin,
129-
move_info.span_path_opt);
130-
move_error_collector.add_error(error);
131-
return
132-
}
133-
None => ()
125+
if let Some(illegal_move_origin) = potentially_illegal_move {
126+
debug!("illegal_move_origin={:?}", illegal_move_origin);
127+
let error = MoveError::with_move_info(illegal_move_origin,
128+
move_info.span_path_opt);
129+
move_error_collector.add_error(error);
130+
return;
134131
}
135132

136133
match opt_loan_path(&move_info.cmt) {

src/librustc_metadata/creader.rs

+20-23
Original file line numberDiff line numberDiff line change
@@ -929,29 +929,26 @@ impl<'a> LocalCrateReader<'a> {
929929
return;
930930
}
931931

932-
match self.creader.extract_crate_info(i) {
933-
Some(info) => {
934-
let (cnum, _, _) = self.creader.resolve_crate(&None,
935-
&info.ident,
936-
&info.name,
937-
None,
938-
i.span,
939-
PathKind::Crate,
940-
true);
941-
942-
let def_id = self.definitions.opt_local_def_id(i.id).unwrap();
943-
let len = self.definitions.def_path(def_id.index).data.len();
944-
945-
self.creader.update_extern_crate(cnum,
946-
ExternCrate {
947-
def_id: def_id,
948-
span: i.span,
949-
direct: true,
950-
path_len: len,
951-
});
952-
self.cstore.add_extern_mod_stmt_cnum(info.id, cnum);
953-
}
954-
None => ()
932+
if let Some(info) = self.creader.extract_crate_info(i) {
933+
let (cnum, _, _) = self.creader.resolve_crate(&None,
934+
&info.ident,
935+
&info.name,
936+
None,
937+
i.span,
938+
PathKind::Crate,
939+
true);
940+
941+
let def_id = self.definitions.opt_local_def_id(i.id).unwrap();
942+
let len = self.definitions.def_path(def_id.index).data.len();
943+
944+
self.creader.update_extern_crate(cnum,
945+
ExternCrate {
946+
def_id: def_id,
947+
span: i.span,
948+
direct: true,
949+
path_len: len,
950+
});
951+
self.cstore.add_extern_mod_stmt_cnum(info.id, cnum);
955952
}
956953
}
957954
ast::ItemKind::ForeignMod(ref fm) => self.process_foreign_mod(i, fm),

src/librustc_trans/base.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -2252,17 +2252,14 @@ pub fn update_linkage(ccx: &CrateContext,
22522252
}
22532253

22542254
fn set_global_section(ccx: &CrateContext, llval: ValueRef, i: &hir::Item) {
2255-
match attr::first_attr_value_str_by_name(&i.attrs, "link_section") {
2256-
Some(sect) => {
2257-
if contains_null(&sect) {
2258-
ccx.sess().fatal(&format!("Illegal null byte in link_section value: `{}`", &sect));
2259-
}
2260-
unsafe {
2261-
let buf = CString::new(sect.as_bytes()).unwrap();
2262-
llvm::LLVMSetSection(llval, buf.as_ptr());
2263-
}
2264-
},
2265-
None => ()
2255+
if let Some(sect) = attr::first_attr_value_str_by_name(&i.attrs, "link_section") {
2256+
if contains_null(&sect) {
2257+
ccx.sess().fatal(&format!("Illegal null byte in link_section value: `{}`", &sect));
2258+
}
2259+
unsafe {
2260+
let buf = CString::new(sect.as_bytes()).unwrap();
2261+
llvm::LLVMSetSection(llval, buf.as_ptr());
2262+
}
22662263
}
22672264
}
22682265

src/librustc_trans/debuginfo/metadata.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -874,9 +874,8 @@ pub fn unknown_file_metadata(cx: &CrateContext) -> DIFile {
874874
}
875875

876876
fn file_metadata_(cx: &CrateContext, key: &str, file_name: &str, work_dir: &str) -> DIFile {
877-
match debug_context(cx).created_files.borrow().get(key) {
878-
Some(file_metadata) => return *file_metadata,
879-
None => ()
877+
if let Some(file_metadata) = debug_context(cx).created_files.borrow().get(key) {
878+
return *file_metadata;
880879
}
881880

882881
debug!("file_metadata: file_name: {}, work_dir: {}", file_name, work_dir);

src/librustc_trans/monomorphize.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,9 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
5050
let mono_ty = apply_param_substs(ccx.tcx(), psubsts, &item_ty);
5151
debug!("mono_ty = {:?} (post-substitution)", mono_ty);
5252

53-
match ccx.instances().borrow().get(&instance) {
54-
Some(&val) => {
55-
debug!("leaving monomorphic fn {:?}", instance);
56-
return (val, mono_ty);
57-
}
58-
None => ()
53+
if let Some(&val) = ccx.instances().borrow().get(&instance) {
54+
debug!("leaving monomorphic fn {:?}", instance);
55+
return (val, mono_ty);
5956
}
6057

6158
debug!("monomorphic_fn({:?})", instance);

src/libsyntax/parse/parser.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -5430,18 +5430,15 @@ impl<'a> Parser<'a> {
54305430
name: String,
54315431
id_sp: Span) -> PResult<'a, (ast::ItemKind, Vec<ast::Attribute> )> {
54325432
let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut();
5433-
match included_mod_stack.iter().position(|p| *p == path) {
5434-
Some(i) => {
5435-
let mut err = String::from("circular modules: ");
5436-
let len = included_mod_stack.len();
5437-
for p in &included_mod_stack[i.. len] {
5438-
err.push_str(&p.to_string_lossy());
5439-
err.push_str(" -> ");
5440-
}
5441-
err.push_str(&path.to_string_lossy());
5442-
return Err(self.span_fatal(id_sp, &err[..]));
5443-
}
5444-
None => ()
5433+
if let Some(i) = included_mod_stack.iter().position(|p| *p == path) {
5434+
let mut err = String::from("circular modules: ");
5435+
let len = included_mod_stack.len();
5436+
for p in &included_mod_stack[i.. len] {
5437+
err.push_str(&p.to_string_lossy());
5438+
err.push_str(" -> ");
5439+
}
5440+
err.push_str(&path.to_string_lossy());
5441+
return Err(self.span_fatal(id_sp, &err[..]));
54455442
}
54465443
included_mod_stack.push(path.clone());
54475444
drop(included_mod_stack);

src/libsyntax/print/pprust.rs

+15-19
Original file line numberDiff line numberDiff line change
@@ -2459,12 +2459,9 @@ impl<'a> State<'a> {
24592459
}
24602460
}
24612461
self.print_ident(path1.node)?;
2462-
match *sub {
2463-
Some(ref p) => {
2464-
word(&mut self.s, "@")?;
2465-
self.print_pat(&p)?;
2466-
}
2467-
None => ()
2462+
if let Some(ref p) = *sub {
2463+
word(&mut self.s, "@")?;
2464+
self.print_pat(&p)?;
24682465
}
24692466
}
24702467
PatKind::TupleStruct(ref path, ref elts, ddpos) => {
@@ -3008,20 +3005,19 @@ impl<'a> State<'a> {
30083005
Some(cm) => cm,
30093006
_ => return Ok(())
30103007
};
3011-
match self.next_comment() {
3012-
Some(ref cmnt) => {
3013-
if (*cmnt).style != comments::Trailing { return Ok(()) }
3014-
let span_line = cm.lookup_char_pos(span.hi);
3015-
let comment_line = cm.lookup_char_pos((*cmnt).pos);
3016-
let mut next = (*cmnt).pos + BytePos(1);
3017-
match next_pos { None => (), Some(p) => next = p }
3018-
if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
3019-
span_line.line == comment_line.line {
3020-
self.print_comment(cmnt)?;
3021-
self.cur_cmnt_and_lit.cur_cmnt += 1;
3022-
}
3008+
if let Some(ref cmnt) = self.next_comment() {
3009+
if (*cmnt).style != comments::Trailing { return Ok(()) }
3010+
let span_line = cm.lookup_char_pos(span.hi);
3011+
let comment_line = cm.lookup_char_pos((*cmnt).pos);
3012+
let mut next = (*cmnt).pos + BytePos(1);
3013+
if let Some(p) = next_pos {
3014+
next = p;
3015+
}
3016+
if span.hi < (*cmnt).pos && (*cmnt).pos < next &&
3017+
span_line.line == comment_line.line {
3018+
self.print_comment(cmnt)?;
3019+
self.cur_cmnt_and_lit.cur_cmnt += 1;
30233020
}
3024-
_ => ()
30253021
}
30263022
Ok(())
30273023
}

src/libsyntax/util/interner.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ impl<T: Eq + Hash + Clone + 'static> Interner<T> {
4747

4848
pub fn intern(&self, val: T) -> Name {
4949
let mut map = self.map.borrow_mut();
50-
match (*map).get(&val) {
51-
Some(&idx) => return idx,
52-
None => (),
50+
if let Some(&idx) = (*map).get(&val) {
51+
return idx;
5352
}
5453

5554
let mut vect = self.vect.borrow_mut();
@@ -161,9 +160,8 @@ impl StrInterner {
161160

162161
pub fn intern(&self, val: &str) -> Name {
163162
let mut map = self.map.borrow_mut();
164-
match map.get(val) {
165-
Some(&idx) => return idx,
166-
None => (),
163+
if let Some(&idx) = map.get(val) {
164+
return idx;
167165
}
168166

169167
let new_idx = Name(self.len() as u32);

0 commit comments

Comments
 (0)