Skip to content

Commit 232ffa0

Browse files
committed
Replace some verbose match statements with their if let equivalent.
No semantic changes, no enabling `if let` where it wasn't already enabled.
1 parent 6163581 commit 232ffa0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+882
-1337
lines changed

src/librustc/lint/builtin.rs

+68-122
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,13 @@ impl LintPass for WhileTrue {
6161
}
6262

6363
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
64-
match e.node {
65-
ast::ExprWhile(ref cond, _, _) => {
66-
match cond.node {
67-
ast::ExprLit(ref lit) => {
68-
match lit.node {
69-
ast::LitBool(true) => {
70-
cx.span_lint(WHILE_TRUE, e.span,
71-
"denote infinite loops with loop \
72-
{ ... }");
73-
}
74-
_ => {}
75-
}
76-
}
77-
_ => ()
64+
if let ast::ExprWhile(ref cond, _, _) = e.node {
65+
if let ast::ExprLit(ref lit) = cond.node {
66+
if let ast::LitBool(true) = lit.node {
67+
cx.span_lint(WHILE_TRUE, e.span,
68+
"denote infinite loops with loop { ... }");
7869
}
7970
}
80-
_ => ()
8171
}
8272
}
8373
}
@@ -93,14 +83,11 @@ impl LintPass for UnusedCasts {
9383
}
9484

9585
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
96-
match e.node {
97-
ast::ExprCast(ref expr, ref ty) => {
98-
let t_t = ast_ty_to_ty(cx, &infer::new_infer_ctxt(cx.tcx), &**ty);
99-
if ty::expr_ty(cx.tcx, &**expr) == t_t {
100-
cx.span_lint(UNUSED_TYPECASTS, ty.span, "unnecessary type cast");
101-
}
86+
if let ast::ExprCast(ref expr, ref ty) = e.node {
87+
let t_t = ast_ty_to_ty(cx, &infer::new_infer_ctxt(cx.tcx), &**ty);
88+
if ty::expr_ty(cx.tcx, &**expr) == t_t {
89+
cx.span_lint(UNUSED_TYPECASTS, ty.span, "unnecessary type cast");
10290
}
103-
_ => ()
10491
}
10592
}
10693
}
@@ -540,9 +527,8 @@ struct RawPtrDerivingVisitor<'a, 'tcx: 'a> {
540527
impl<'a, 'tcx, 'v> Visitor<'v> for RawPtrDerivingVisitor<'a, 'tcx> {
541528
fn visit_ty(&mut self, ty: &ast::Ty) {
542529
static MSG: &'static str = "use of `#[deriving]` with a raw pointer";
543-
match ty.node {
544-
ast::TyPtr(..) => self.cx.span_lint(RAW_POINTER_DERIVING, ty.span, MSG),
545-
_ => {}
530+
if let ast::TyPtr(..) = ty.node {
531+
self.cx.span_lint(RAW_POINTER_DERIVING, ty.span, MSG);
546532
}
547533
visit::walk_ty(self, ty);
548534
}
@@ -720,9 +706,8 @@ impl LintPass for UnusedResults {
720706
_ => return
721707
};
722708

723-
match expr.node {
724-
ast::ExprRet(..) => return,
725-
_ => {}
709+
if let ast::ExprRet(..) = expr.node {
710+
return;
726711
}
727712

728713
let t = ty::expr_ty(cx.tcx, expr);
@@ -733,11 +718,8 @@ impl LintPass for UnusedResults {
733718
ty::ty_struct(did, _) |
734719
ty::ty_enum(did, _) => {
735720
if ast_util::is_local(did) {
736-
match cx.tcx.map.get(did.node) {
737-
ast_map::NodeItem(it) => {
738-
warned |= check_must_use(cx, it.attrs.as_slice(), s.span);
739-
}
740-
_ => {}
721+
if let ast_map::NodeItem(it) = cx.tcx.map.get(did.node) {
722+
warned |= check_must_use(cx, it.attrs.as_slice(), s.span);
741723
}
742724
} else {
743725
csearch::get_item_attrs(&cx.sess().cstore, did, |attrs| {
@@ -969,11 +951,8 @@ impl LintPass for NonSnakeCase {
969951
}
970952

971953
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
972-
match it.node {
973-
ast::ItemMod(_) => {
974-
self.check_snake_case(cx, "module", it.ident, it.span);
975-
}
976-
_ => {}
954+
if let ast::ItemMod(_) = it.node {
955+
self.check_snake_case(cx, "module", it.ident, it.span);
977956
}
978957
}
979958

@@ -986,27 +965,18 @@ impl LintPass for NonSnakeCase {
986965
}
987966

988967
fn check_pat(&mut self, cx: &Context, p: &ast::Pat) {
989-
match &p.node {
990-
&ast::PatIdent(_, ref path1, _) => {
991-
match cx.tcx.def_map.borrow().get(&p.id) {
992-
Some(&def::DefLocal(_)) => {
993-
self.check_snake_case(cx, "variable", path1.node, p.span);
994-
}
995-
_ => {}
996-
}
968+
if let &ast::PatIdent(_, ref path1, _) = &p.node {
969+
if let Some(&def::DefLocal(_)) = cx.tcx.def_map.borrow().get(&p.id) {
970+
self.check_snake_case(cx, "variable", path1.node, p.span);
997971
}
998-
_ => {}
999972
}
1000973
}
1001974

1002975
fn check_struct_def(&mut self, cx: &Context, s: &ast::StructDef,
1003976
_: ast::Ident, _: &ast::Generics, _: ast::NodeId) {
1004977
for sf in s.fields.iter() {
1005-
match sf.node {
1006-
ast::StructField_ { kind: ast::NamedField(ident, _), .. } => {
1007-
self.check_snake_case(cx, "structure field", ident, sf.span);
1008-
}
1009-
_ => {}
978+
if let ast::StructField_ { kind: ast::NamedField(ident, _), .. } = sf.node {
979+
self.check_snake_case(cx, "structure field", ident, sf.span);
1010980
}
1011981
}
1012982
}
@@ -1069,16 +1039,13 @@ pub struct UnusedParens;
10691039
impl UnusedParens {
10701040
fn check_unused_parens_core(&self, cx: &Context, value: &ast::Expr, msg: &str,
10711041
struct_lit_needs_parens: bool) {
1072-
match value.node {
1073-
ast::ExprParen(ref inner) => {
1074-
let necessary = struct_lit_needs_parens && contains_exterior_struct_lit(&**inner);
1075-
if !necessary {
1076-
cx.span_lint(UNUSED_PARENS, value.span,
1077-
format!("unnecessary parentheses around {}",
1078-
msg).as_slice())
1079-
}
1042+
if let ast::ExprParen(ref inner) = value.node {
1043+
let necessary = struct_lit_needs_parens && contains_exterior_struct_lit(&**inner);
1044+
if !necessary {
1045+
cx.span_lint(UNUSED_PARENS, value.span,
1046+
format!("unnecessary parentheses around {}",
1047+
msg).as_slice())
10801048
}
1081-
_ => {}
10821049
}
10831050

10841051
/// Expressions that syntactically contain an "exterior" struct
@@ -1201,24 +1168,21 @@ impl LintPass for NonShorthandFieldPatterns {
12011168

12021169
fn check_pat(&mut self, cx: &Context, pat: &ast::Pat) {
12031170
let def_map = cx.tcx.def_map.borrow();
1204-
match pat.node {
1205-
ast::PatStruct(_, ref v, _) => {
1206-
for fieldpat in v.iter()
1207-
.filter(|fieldpat| !fieldpat.node.is_shorthand)
1208-
.filter(|fieldpat| def_map.get(&fieldpat.node.pat.id)
1209-
== Some(&def::DefLocal(fieldpat.node.pat.id))) {
1210-
match fieldpat.node.pat.node {
1211-
ast::PatIdent(_, ident, None) if ident.node.as_str()
1212-
== fieldpat.node.ident.as_str() => {
1213-
cx.span_lint(NON_SHORTHAND_FIELD_PATTERNS, fieldpat.span,
1214-
format!("the `{}:` in this pattern is redundant and can \
1215-
be removed", ident.node.as_str()).as_slice())
1216-
},
1217-
_ => {},
1218-
}
1171+
if let ast::PatStruct(_, ref v, _) = pat.node {
1172+
for fieldpat in v.iter()
1173+
.filter(|fieldpat| !fieldpat.node.is_shorthand)
1174+
.filter(|fieldpat| def_map.get(&fieldpat.node.pat.id)
1175+
== Some(&def::DefLocal(fieldpat.node.pat.id))) {
1176+
match fieldpat.node.pat.node {
1177+
ast::PatIdent(_, ident, None) if ident.node.as_str()
1178+
== fieldpat.node.ident.as_str() => {
1179+
cx.span_lint(NON_SHORTHAND_FIELD_PATTERNS, fieldpat.span,
1180+
format!("the `{}:` in this pattern is redundant and can \
1181+
be removed", ident.node.as_str()).as_slice())
1182+
},
1183+
_ => {},
12191184
}
1220-
},
1221-
_ => {}
1185+
}
12221186
}
12231187
}
12241188
}
@@ -1313,27 +1277,18 @@ impl LintPass for UnusedMut {
13131277
}
13141278

13151279
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
1316-
match e.node {
1317-
ast::ExprMatch(_, ref arms, _) => {
1318-
for a in arms.iter() {
1319-
self.check_unused_mut_pat(cx, a.pats.as_slice())
1320-
}
1280+
if let ast::ExprMatch(_, ref arms, _) = e.node {
1281+
for a in arms.iter() {
1282+
self.check_unused_mut_pat(cx, a.pats.as_slice())
13211283
}
1322-
_ => {}
13231284
}
13241285
}
13251286

13261287
fn check_stmt(&mut self, cx: &Context, s: &ast::Stmt) {
1327-
match s.node {
1328-
ast::StmtDecl(ref d, _) => {
1329-
match d.node {
1330-
ast::DeclLocal(ref l) => {
1331-
self.check_unused_mut_pat(cx, slice::ref_slice(&l.pat));
1332-
},
1333-
_ => {}
1334-
}
1335-
},
1336-
_ => {}
1288+
if let ast::StmtDecl(ref d, _) = s.node {
1289+
if let ast::DeclLocal(ref l) = d.node {
1290+
self.check_unused_mut_pat(cx, slice::ref_slice(&l.pat));
1291+
}
13371292
}
13381293
}
13391294

@@ -1362,26 +1317,20 @@ impl LintPass for UnusedAllocation {
13621317
_ => return
13631318
}
13641319

1365-
match cx.tcx.adjustments.borrow().get(&e.id) {
1366-
Some(adjustment) => {
1367-
match *adjustment {
1368-
ty::AdjustDerefRef(ty::AutoDerefRef { ref autoref, .. }) => {
1369-
match autoref {
1370-
&Some(ty::AutoPtr(_, ast::MutImmutable, None)) => {
1371-
cx.span_lint(UNUSED_ALLOCATION, e.span,
1372-
"unnecessary allocation, use & instead");
1373-
}
1374-
&Some(ty::AutoPtr(_, ast::MutMutable, None)) => {
1375-
cx.span_lint(UNUSED_ALLOCATION, e.span,
1376-
"unnecessary allocation, use &mut instead");
1377-
}
1378-
_ => ()
1379-
}
1320+
if let Some(adjustment) = cx.tcx.adjustments.borrow().get(&e.id) {
1321+
if let ty::AdjustDerefRef(ty::AutoDerefRef { ref autoref, .. }) = *adjustment {
1322+
match autoref {
1323+
&Some(ty::AutoPtr(_, ast::MutImmutable, None)) => {
1324+
cx.span_lint(UNUSED_ALLOCATION, e.span,
1325+
"unnecessary allocation, use & instead");
13801326
}
1381-
_ => {}
1327+
&Some(ty::AutoPtr(_, ast::MutMutable, None)) => {
1328+
cx.span_lint(UNUSED_ALLOCATION, e.span,
1329+
"unnecessary allocation, use &mut instead");
1330+
}
1331+
_ => ()
13821332
}
13831333
}
1384-
_ => ()
13851334
}
13861335
}
13871336
}
@@ -1499,17 +1448,14 @@ impl LintPass for MissingDoc {
14991448
fn check_fn(&mut self, cx: &Context,
15001449
fk: visit::FnKind, _: &ast::FnDecl,
15011450
_: &ast::Block, _: Span, _: ast::NodeId) {
1502-
match fk {
1503-
visit::FkMethod(_, _, m) => {
1504-
// If the method is an impl for a trait, don't doc.
1505-
if method_context(cx, m) == TraitImpl { return; }
1506-
1507-
// Otherwise, doc according to privacy. This will also check
1508-
// doc for default methods defined on traits.
1509-
self.check_missing_docs_attrs(cx, Some(m.id), m.attrs.as_slice(),
1510-
m.span, "a method");
1511-
}
1512-
_ => {}
1451+
if let visit::FkMethod(_, _, m) = fk {
1452+
// If the method is an impl for a trait, don't doc.
1453+
if method_context(cx, m) == TraitImpl { return; }
1454+
1455+
// Otherwise, doc according to privacy. This will also check
1456+
// doc for default methods defined on traits.
1457+
self.check_missing_docs_attrs(cx, Some(m.id), m.attrs.as_slice(),
1458+
m.span, "a method");
15131459
}
15141460
}
15151461

src/librustc/metadata/decoder.rs

+32-43
Original file line numberDiff line numberDiff line change
@@ -514,41 +514,33 @@ fn each_child_of_item_or_crate(intr: Rc<IdentInterner>,
514514
let inherent_impl_def_id = item_def_id(inherent_impl_def_id_doc,
515515
cdata);
516516
let items = reader::get_doc(rbml::Doc::new(cdata.data()), tag_items);
517-
match maybe_find_item(inherent_impl_def_id.node, items) {
518-
None => {}
519-
Some(inherent_impl_doc) => {
520-
let _ = reader::tagged_docs(inherent_impl_doc,
521-
tag_item_impl_item,
522-
|impl_item_def_id_doc| {
523-
let impl_item_def_id = item_def_id(impl_item_def_id_doc,
524-
cdata);
525-
match maybe_find_item(impl_item_def_id.node, items) {
526-
None => {}
527-
Some(impl_method_doc) => {
528-
match item_family(impl_method_doc) {
529-
StaticMethod => {
530-
// Hand off the static method
531-
// to the callback.
532-
let static_method_name =
533-
item_name(&*intr, impl_method_doc);
534-
let static_method_def_like =
535-
item_to_def_like(impl_method_doc,
536-
impl_item_def_id,
537-
cdata.cnum);
538-
callback(static_method_def_like,
539-
static_method_name,
540-
item_visibility(impl_method_doc));
541-
}
542-
_ => {}
543-
}
517+
if let Some(inherent_impl_doc) = maybe_find_item(inherent_impl_def_id.node, items) {
518+
let _ = reader::tagged_docs(inherent_impl_doc,
519+
tag_item_impl_item,
520+
|impl_item_def_id_doc| {
521+
let impl_item_def_id = item_def_id(impl_item_def_id_doc,
522+
cdata);
523+
if let Some(impl_method_doc) = maybe_find_item(impl_item_def_id.node, items) {
524+
match item_family(impl_method_doc) {
525+
StaticMethod => {
526+
// Hand off the static method
527+
// to the callback.
528+
let static_method_name =
529+
item_name(&*intr, impl_method_doc);
530+
let static_method_def_like =
531+
item_to_def_like(impl_method_doc,
532+
impl_item_def_id,
533+
cdata.cnum);
534+
callback(static_method_def_like,
535+
static_method_name,
536+
item_visibility(impl_method_doc));
544537
}
538+
_ => {}
545539
}
546-
547-
true
548-
});
549-
}
540+
}
541+
true
542+
});
550543
}
551-
552544
true
553545
});
554546

@@ -578,17 +570,14 @@ fn each_child_of_item_or_crate(intr: Rc<IdentInterner>,
578570
let other_crates_items = reader::get_doc(rbml::Doc::new(crate_data.data()), tag_items);
579571

580572
// Get the item.
581-
match maybe_find_item(child_def_id.node, other_crates_items) {
582-
None => {}
583-
Some(child_item_doc) => {
584-
// Hand off the item to the callback.
585-
let def_like = item_to_def_like(child_item_doc,
586-
child_def_id,
587-
child_def_id.krate);
588-
// These items have a public visibility because they're part of
589-
// a public re-export.
590-
callback(def_like, token::intern(name), ast::Public);
591-
}
573+
if let Some(child_item_doc) = maybe_find_item(child_def_id.node, other_crates_items) {
574+
// Hand off the item to the callback.
575+
let def_like = item_to_def_like(child_item_doc,
576+
child_def_id,
577+
child_def_id.krate);
578+
// These items have a public visibility because they're part of
579+
// a public re-export.
580+
callback(def_like, token::intern(name), ast::Public);
592581
}
593582

594583
true

0 commit comments

Comments
 (0)