Skip to content

Commit fbfe70e

Browse files
committed
#31820 - Utilize if..let instead of single match branch
1 parent 6ffd7cd commit fbfe70e

File tree

9 files changed

+28
-42
lines changed

9 files changed

+28
-42
lines changed

src/libgetopts/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,8 @@ impl Matches {
331331
/// Returns the string argument supplied to one of several matching options or `None`.
332332
pub fn opts_str(&self, names: &[String]) -> Option<String> {
333333
for nm in names {
334-
match self.opt_val(&nm[..]) {
335-
Some(Val(ref s)) => return Some(s.clone()),
336-
_ => (),
334+
if let Some(Val(ref s)) = self.opt_val(&nm[..]) {
335+
return Some(s.clone())
337336
}
338337
}
339338
None

src/librustc_trans/back/link.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,8 @@ fn symbol_hash<'tcx>(tcx: &ty::ctxt<'tcx>,
226226
}
227227

228228
fn get_symbol_hash<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> String {
229-
match ccx.type_hashcodes().borrow().get(&t) {
230-
Some(h) => return h.to_string(),
231-
None => {}
229+
if let Some(h) = ccx.type_hashcodes().borrow().get(&t) {
230+
return h.to_string()
232231
}
233232

234233
let mut symbol_hasher = ccx.symbol_hasher().borrow_mut();
@@ -315,9 +314,8 @@ pub fn mangle<PI: Iterator<Item=InternedString>>(path: PI, hash: Option<&str>) -
315314
push(&mut n, &data);
316315
}
317316

318-
match hash {
319-
Some(s) => push(&mut n, s),
320-
None => {}
317+
if let Some(s) = hash {
318+
push(&mut n, s)
321319
}
322320

323321
n.push('E'); // End name-sequence.

src/librustc_trans/trans/base.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,8 @@ impl Drop for _InsnCtxt {
150150
pub fn push_ctxt(s: &'static str) -> _InsnCtxt {
151151
debug!("new InsnCtxt: {}", s);
152152
TASK_LOCAL_INSN_KEY.with(|slot| {
153-
match slot.borrow_mut().as_mut() {
154-
Some(ctx) => ctx.push(s),
155-
None => {}
153+
if let Some(ctx) = slot.borrow_mut().as_mut() {
154+
ctx.push(s)
156155
}
157156
});
158157
_InsnCtxt {
@@ -198,9 +197,8 @@ fn get_extern_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
198197
name: &str,
199198
did: DefId)
200199
-> ValueRef {
201-
match ccx.externs().borrow().get(name) {
202-
Some(n) => return *n,
203-
None => (),
200+
if let Some(n) = ccx.externs().borrow().get(name) {
201+
return *n;
204202
}
205203

206204
let f = declare::declare_rust_fn(ccx, name, fn_ty);
@@ -238,9 +236,8 @@ pub fn get_extern_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
238236
-> ValueRef {
239237
let name = ccx.sess().cstore.item_symbol(did);
240238
let ty = type_of(ccx, t);
241-
match ccx.externs().borrow_mut().get(&name) {
242-
Some(n) => return *n,
243-
None => (),
239+
if let Some(n) = ccx.externs().borrow_mut().get(&name) {
240+
return *n;
244241
}
245242
// FIXME(nagisa): perhaps the map of externs could be offloaded to llvm somehow?
246243
// FIXME(nagisa): investigate whether it can be changed into define_global
@@ -2755,9 +2752,8 @@ fn contains_null(s: &str) -> bool {
27552752
pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef {
27562753
debug!("get_item_val(id=`{}`)", id);
27572754

2758-
match ccx.item_vals().borrow().get(&id).cloned() {
2759-
Some(v) => return v,
2760-
None => {}
2755+
if let Some(v) = ccx.item_vals().borrow().get(&id).cloned() {
2756+
return v;
27612757
}
27622758

27632759
let item = ccx.tcx().map.get(id);

src/librustc_trans/trans/common.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -947,9 +947,8 @@ pub fn C_u8(ccx: &CrateContext, i: u8) -> ValueRef {
947947
// our boxed-and-length-annotated strings.
948948
pub fn C_cstr(cx: &CrateContext, s: InternedString, null_terminated: bool) -> ValueRef {
949949
unsafe {
950-
match cx.const_cstr_cache().borrow().get(&s) {
951-
Some(&llval) => return llval,
952-
None => ()
950+
if let Some(&llval) = cx.const_cstr_cache().borrow().get(&s) {
951+
return llval;
953952
}
954953

955954
let sc = llvm::LLVMConstStringInContext(cx.llcx(),

src/librustc_trans/trans/type_of.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,8 @@ pub fn type_of_fn_from_ty<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, fty: Ty<'tcx>)
182182
// recursive types. For example, enum types rely on this behavior.
183183

184184
pub fn sizing_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Type {
185-
match cx.llsizingtypes().borrow().get(&t).cloned() {
186-
Some(t) => return t,
187-
None => ()
185+
if let Some(t) = cx.llsizingtypes().borrow().get(&t).cloned() {
186+
return t;
188187
}
189188

190189
debug!("sizing_type_of {:?}", t);
@@ -317,9 +316,8 @@ pub fn type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> Type {
317316
/// NB: If you update this, be sure to update `sizing_type_of()` as well.
318317
pub fn in_memory_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Type {
319318
// Check the cache.
320-
match cx.lltypes().borrow().get(&t) {
321-
Some(&llty) => return llty,
322-
None => ()
319+
if let Some(&llty) = cx.lltypes().borrow().get(&t) {
320+
return llty;
323321
}
324322

325323
debug!("type_of {:?}", t);

src/librustdoc/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,8 @@ fn rust_input(cratefile: &str, externs: core::Externs, matches: &getopts::Matche
385385
*s.borrow_mut() = analysis.take();
386386
});
387387

388-
match matches.opt_str("crate-name") {
389-
Some(name) => krate.name = name,
390-
None => {}
388+
if let Some(name) = matches.opt_str("crate-name") {
389+
krate.name = name
391390
}
392391

393392
// Process all of the crate attributes, extracting plugin metadata along

src/libstd/net/addr.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,8 @@ impl ToSocketAddrs for str {
467467
type Iter = vec::IntoIter<SocketAddr>;
468468
fn to_socket_addrs(&self) -> io::Result<vec::IntoIter<SocketAddr>> {
469469
// try to parse as a regular SocketAddr first
470-
match self.parse().ok() {
471-
Some(addr) => return Ok(vec![addr].into_iter()),
472-
None => {}
470+
if let Some(addr) = self.parse().ok() {
471+
return Ok(vec![addr].into_iter());
473472
}
474473

475474
macro_rules! try_opt {

src/libstd/net/parser.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ impl<'a> Parser<'a> {
6666
fn read_or<T>(&mut self, parsers: &mut [Box<FnMut(&mut Parser) -> Option<T> + 'static>])
6767
-> Option<T> {
6868
for pf in parsers {
69-
match self.read_atomically(|p: &mut Parser| pf(p)) {
70-
Some(r) => return Some(r),
71-
None => {}
69+
if let Some(r) = self.read_atomically(|p: &mut Parser| pf(p)) {
70+
return Some(r);
7271
}
7372
}
7473
None

src/libstd/sys/windows/thread_local.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ static mut DTORS: *mut Vec<(Key, Dtor)> = ptr::null_mut();
6969
pub unsafe fn create(dtor: Option<Dtor>) -> Key {
7070
let key = c::TlsAlloc();
7171
assert!(key != c::TLS_OUT_OF_INDEXES);
72-
match dtor {
73-
Some(f) => register_dtor(key, f),
74-
None => {}
72+
if let Some(f) = dtor {
73+
register_dtor(key, f);
7574
}
7675
return key;
7776
}

0 commit comments

Comments
 (0)