Skip to content

Commit c2b3aa9

Browse files
committed
rustc/middle: improve some patterns
1 parent ac841e7 commit c2b3aa9

File tree

10 files changed

+56
-95
lines changed

10 files changed

+56
-95
lines changed

src/librustc/middle/dead.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,10 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
131131

132132
fn mark_live_symbols(&mut self) {
133133
let mut scanned = FxHashSet();
134-
while !self.worklist.is_empty() {
135-
let id = self.worklist.pop().unwrap();
136-
if scanned.contains(&id) {
134+
while let Some(id) = self.worklist.pop() {
135+
if !scanned.insert(id) {
137136
continue
138137
}
139-
scanned.insert(id);
140138

141139
if let Some(ref node) = self.tcx.hir.find(id) {
142140
self.live_symbols.insert(id);

src/librustc/middle/dependency_format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
253253
}
254254
}
255255

256-
return ret;
256+
ret
257257
}
258258

259259
fn add_library(tcx: TyCtxt<'_, '_, '_>,

src/librustc/middle/exported_symbols.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,8 @@ impl_stable_hash_for!(enum self::SymbolExportLevel {
3535

3636
impl SymbolExportLevel {
3737
pub fn is_below_threshold(self, threshold: SymbolExportLevel) -> bool {
38-
if threshold == SymbolExportLevel::Rust {
39-
// We export everything from Rust dylibs
40-
true
41-
} else {
42-
self == SymbolExportLevel::C
43-
}
38+
threshold == SymbolExportLevel::Rust // export everything from Rust dylibs
39+
|| self == SymbolExportLevel::C
4440
}
4541
}
4642

src/librustc/middle/expr_use_visitor.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -801,10 +801,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
801801
self.walk_pat(discr_cmt.clone(), &pat, mode);
802802
}
803803

804-
if let Some(ref guard) = arm.guard {
805-
match guard {
806-
hir::Guard::If(ref e) => self.consume_expr(e),
807-
}
804+
if let Some(hir::Guard::If(ref e)) = arm.guard {
805+
self.consume_expr(e)
808806
}
809807

810808
self.consume_expr(&arm.body);

src/librustc/middle/intrinsicck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
107107
}
108108
Err(LayoutError::Unknown(bad)) => {
109109
if bad == ty {
110-
"this type's size can vary".to_string()
110+
"this type's size can vary".to_owned()
111111
} else {
112112
format!("size can vary because of {}", bad)
113113
}

src/librustc/middle/lang_items.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
145145

146146
fn collect_item(&mut self, item_index: usize, item_def_id: DefId) {
147147
// Check for duplicates.
148-
match self.items.items[item_index] {
149-
Some(original_def_id) if original_def_id != item_def_id => {
148+
if let Some(original_def_id) = self.items.items[item_index] {
149+
if original_def_id != item_def_id {
150150
let name = LangItem::from_u32(item_index as u32).unwrap().name();
151151
let mut err = match self.tcx.hir.span_if_local(item_def_id) {
152152
Some(span) => struct_span_err!(
@@ -169,9 +169,6 @@ impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
169169
}
170170
err.emit();
171171
}
172-
_ => {
173-
// OK.
174-
}
175172
}
176173

177174
// Matched.
@@ -194,7 +191,7 @@ pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> {
194191
}
195192
}
196193

197-
return None;
194+
None
198195
}
199196

200197
pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LanguageItems {

src/librustc/middle/liveness.rs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ fn live_node_kind_to_string(lnk: LiveNodeKind, tcx: TyCtxt<'_, '_, '_>) -> Strin
170170
VarDefNode(s) => {
171171
format!("Var def node [{}]", cm.span_to_string(s))
172172
}
173-
ExitNode => "Exit node".to_string(),
173+
ExitNode => "Exit node".to_owned(),
174174
}
175175
}
176176

@@ -330,7 +330,7 @@ impl<'a, 'tcx> IrMaps<'a, 'tcx> {
330330
Local(LocalInfo { name, .. }) | Arg(_, name) => {
331331
name.to_string()
332332
},
333-
CleanExit => "<clean-exit>".to_string()
333+
CleanExit => "<clean-exit>".to_owned()
334334
}
335335
}
336336

@@ -1049,12 +1049,9 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
10491049

10501050
// the construction of a closure itself is not important,
10511051
// but we have to consider the closed over variables.
1052-
let caps = match self.ir.capture_info_map.get(&expr.id) {
1053-
Some(caps) => caps.clone(),
1054-
None => {
1055-
span_bug!(expr.span, "no registered caps");
1056-
}
1057-
};
1052+
let caps = self.ir.capture_info_map.get(&expr.id).cloned().unwrap_or_else(||
1053+
span_bug!(expr.span, "no registered caps"));
1054+
10581055
caps.iter().rev().fold(succ, |succ, cap| {
10591056
self.init_from_succ(cap.ln, succ);
10601057
let var = self.variable(cap.var_hid, expr.span);
@@ -1114,15 +1111,12 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
11141111
self.init_empty(ln, succ);
11151112
let mut first_merge = true;
11161113
for arm in arms {
1117-
let body_succ =
1118-
self.propagate_through_expr(&arm.body, succ);
1119-
let guard_succ =
1120-
self.propagate_through_opt_expr(
1121-
arm.guard.as_ref().map(|g|
1122-
match g {
1123-
hir::Guard::If(e) => &**e,
1124-
}),
1125-
body_succ);
1114+
let body_succ = self.propagate_through_expr(&arm.body, succ);
1115+
1116+
let guard_succ = self.propagate_through_opt_expr(
1117+
arm.guard.as_ref().map(|hir::Guard::If(e)| &**e),
1118+
body_succ
1119+
);
11261120
// only consider the first pattern; any later patterns must have
11271121
// the same bindings, and we also consider the first pattern to be
11281122
// the "authoritative" set of ids
@@ -1146,10 +1140,9 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
11461140
let target = match label.target_id {
11471141
Ok(node_id) => self.break_ln.get(&node_id),
11481142
Err(err) => span_bug!(expr.span, "loop scope error: {}", err),
1149-
}.map(|x| *x);
1150-
11511143
// Now that we know the label we're going to,
11521144
// look it up in the break loop nodes table
1145+
}.cloned();
11531146

11541147
match target {
11551148
Some(b) => self.propagate_through_opt_expr(opt_expr.as_ref().map(|e| &**e), b),
@@ -1159,18 +1152,13 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
11591152

11601153
hir::ExprKind::Continue(label) => {
11611154
// Find which label this expr continues to
1162-
let sc = match label.target_id {
1163-
Ok(node_id) => node_id,
1164-
Err(err) => span_bug!(expr.span, "loop scope error: {}", err),
1165-
};
1155+
let sc = label.target_id.unwrap_or_else(|err|
1156+
span_bug!(expr.span, "loop scope error: {}", err));
11661157

11671158
// Now that we know the label we're going to,
11681159
// look it up in the continue loop nodes table
1169-
1170-
match self.cont_ln.get(&sc) {
1171-
Some(&b) => b,
1172-
None => span_bug!(expr.span, "continue to unknown label")
1173-
}
1160+
self.cont_ln.get(&sc).cloned().unwrap_or_else(||
1161+
span_bug!(expr.span, "continue to unknown label"))
11741162
}
11751163

11761164
hir::ExprKind::Assign(ref l, ref r) => {
@@ -1450,8 +1438,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
14501438
self.propagate_through_expr(&cond, ln)
14511439
}
14521440
};
1453-
assert!(cond_ln == new_cond_ln);
1454-
assert!(body_ln == self.propagate_through_block(body, cond_ln));
1441+
assert_eq!(cond_ln, new_cond_ln);
1442+
assert_eq!(body_ln, self.propagate_through_block(body, cond_ln));
14551443
}
14561444

14571445
cond_ln
@@ -1576,7 +1564,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
15761564

15771565
fn should_warn(&self, var: Variable) -> Option<String> {
15781566
let name = self.ir.variable_name(var);
1579-
if name.is_empty() || name.as_bytes()[0] == ('_' as u8) {
1567+
if name.is_empty() || name.as_bytes()[0] == b'_' {
15801568
None
15811569
} else {
15821570
Some(name)

src/librustc/middle/region.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -515,10 +515,8 @@ impl<'tcx> ScopeTree {
515515

516516
/// Returns the lifetime of the local variable `var_id`
517517
pub fn var_scope(&self, var_id: hir::ItemLocalId) -> Scope {
518-
match self.var_map.get(&var_id) {
519-
Some(&r) => r,
520-
None => { bug!("no enclosing scope for id {:?}", var_id); }
521-
}
518+
self.var_map.get(&var_id).cloned().unwrap_or_else(||
519+
bug!("no enclosing scope for id {:?}", var_id))
522520
}
523521

524522
pub fn temporary_scope(&self, expr_id: hir::ItemLocalId) -> Option<Scope> {
@@ -828,10 +826,8 @@ fn resolve_block<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, blk:
828826
fn resolve_arm<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, arm: &'tcx hir::Arm) {
829827
visitor.terminating_scopes.insert(arm.body.hir_id.local_id);
830828

831-
if let Some(ref g) = arm.guard {
832-
match g {
833-
hir::Guard::If(ref expr) => visitor.terminating_scopes.insert(expr.hir_id.local_id),
834-
};
829+
if let Some(hir::Guard::If(ref expr)) = arm.guard {
830+
visitor.terminating_scopes.insert(expr.hir_id.local_id);
835831
}
836832

837833
intravisit::walk_arm(visitor, arm);

src/librustc/middle/resolve_lifetime.rs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,18 +1872,15 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
18721872
node: hir::TraitItemKind::Method(_, ref m),
18731873
..
18741874
}) => {
1875-
match self.tcx
1875+
if let hir::ItemKind::Trait(.., ref trait_items) = self.tcx
18761876
.hir
18771877
.expect_item(self.tcx.hir.get_parent(parent))
18781878
.node
18791879
{
1880-
hir::ItemKind::Trait(.., ref trait_items) => {
1881-
assoc_item_kind = trait_items
1882-
.iter()
1883-
.find(|ti| ti.id.node_id == parent)
1884-
.map(|ti| ti.kind);
1885-
}
1886-
_ => {}
1880+
assoc_item_kind = trait_items
1881+
.iter()
1882+
.find(|ti| ti.id.node_id == parent)
1883+
.map(|ti| ti.kind);
18871884
}
18881885
match *m {
18891886
hir::TraitMethod::Required(_) => None,
@@ -1895,19 +1892,16 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
18951892
node: hir::ImplItemKind::Method(_, body),
18961893
..
18971894
}) => {
1898-
match self.tcx
1895+
if let hir::ItemKind::Impl(.., ref self_ty, ref impl_items) = self.tcx
18991896
.hir
19001897
.expect_item(self.tcx.hir.get_parent(parent))
19011898
.node
19021899
{
1903-
hir::ItemKind::Impl(.., ref self_ty, ref impl_items) => {
1904-
impl_self = Some(self_ty);
1905-
assoc_item_kind = impl_items
1906-
.iter()
1907-
.find(|ii| ii.id.node_id == parent)
1908-
.map(|ii| ii.kind);
1909-
}
1910-
_ => {}
1900+
impl_self = Some(self_ty);
1901+
assoc_item_kind = impl_items
1902+
.iter()
1903+
.find(|ii| ii.id.node_id == parent)
1904+
.map(|ii| ii.kind);
19111905
}
19121906
Some(body)
19131907
}
@@ -2541,15 +2535,12 @@ fn insert_late_bound_lifetimes(
25412535
appears_in_where_clause.visit_generics(generics);
25422536

25432537
for param in &generics.params {
2544-
match param.kind {
2545-
hir::GenericParamKind::Lifetime { .. } => {
2546-
if !param.bounds.is_empty() {
2547-
// `'a: 'b` means both `'a` and `'b` are referenced
2548-
appears_in_where_clause
2549-
.regions.insert(hir::LifetimeName::Param(param.name.modern()));
2550-
}
2538+
if let hir::GenericParamKind::Lifetime { .. } = param.kind {
2539+
if !param.bounds.is_empty() {
2540+
// `'a: 'b` means both `'a` and `'b` are referenced
2541+
appears_in_where_clause
2542+
.regions.insert(hir::LifetimeName::Param(param.name.modern()));
25512543
}
2552-
hir::GenericParamKind::Type { .. } => {}
25532544
}
25542545
}
25552546

src/librustc/middle/stability.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -523,15 +523,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
523523
Some(Def::Method(_)) |
524524
Some(Def::AssociatedTy(_)) |
525525
Some(Def::AssociatedConst(_)) => {
526-
match self.associated_item(def_id).container {
527-
ty::TraitContainer(trait_def_id) => {
528-
// Trait methods do not declare visibility (even
529-
// for visibility info in cstore). Use containing
530-
// trait instead, so methods of pub traits are
531-
// themselves considered pub.
532-
def_id = trait_def_id;
533-
}
534-
_ => {}
526+
if let ty::TraitContainer(trait_def_id) = self.associated_item(def_id).container {
527+
// Trait methods do not declare visibility (even
528+
// for visibility info in cstore). Use containing
529+
// trait instead, so methods of pub traits are
530+
// themselves considered pub.
531+
def_id = trait_def_id;
535532
}
536533
}
537534
_ => {}

0 commit comments

Comments
 (0)