rustc/middle: improve some patterns

This commit is contained in:
ljedrz 2018-10-02 18:05:06 +02:00
parent ac841e7450
commit c2b3aa9939
10 changed files with 56 additions and 95 deletions

View File

@ -131,12 +131,10 @@ fn handle_field_pattern_match(&mut self, lhs: &hir::Pat, def: Def,
fn mark_live_symbols(&mut self) {
let mut scanned = FxHashSet();
while !self.worklist.is_empty() {
let id = self.worklist.pop().unwrap();
if scanned.contains(&id) {
while let Some(id) = self.worklist.pop() {
if !scanned.insert(id) {
continue
}
scanned.insert(id);
if let Some(ref node) = self.tcx.hir.find(id) {
self.live_symbols.insert(id);

View File

@ -253,7 +253,7 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
}
}
return ret;
ret
}
fn add_library(tcx: TyCtxt<'_, '_, '_>,

View File

@ -35,12 +35,8 @@ pub enum SymbolExportLevel {
impl SymbolExportLevel {
pub fn is_below_threshold(self, threshold: SymbolExportLevel) -> bool {
if threshold == SymbolExportLevel::Rust {
// We export everything from Rust dylibs
true
} else {
self == SymbolExportLevel::C
}
threshold == SymbolExportLevel::Rust // export everything from Rust dylibs
|| self == SymbolExportLevel::C
}
}

View File

@ -801,10 +801,8 @@ fn walk_arm(&mut self, discr_cmt: mc::cmt<'tcx>, arm: &hir::Arm, mode: MatchMode
self.walk_pat(discr_cmt.clone(), &pat, mode);
}
if let Some(ref guard) = arm.guard {
match guard {
hir::Guard::If(ref e) => self.consume_expr(e),
}
if let Some(hir::Guard::If(ref e)) = arm.guard {
self.consume_expr(e)
}
self.consume_expr(&arm.body);

View File

@ -107,7 +107,7 @@ fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>) {
}
Err(LayoutError::Unknown(bad)) => {
if bad == ty {
"this type's size can vary".to_string()
"this type's size can vary".to_owned()
} else {
format!("size can vary because of {}", bad)
}

View File

@ -145,8 +145,8 @@ fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LanguageItemCollector<'a, 'tcx> {
fn collect_item(&mut self, item_index: usize, item_def_id: DefId) {
// Check for duplicates.
match self.items.items[item_index] {
Some(original_def_id) if original_def_id != item_def_id => {
if let Some(original_def_id) = self.items.items[item_index] {
if original_def_id != item_def_id {
let name = LangItem::from_u32(item_index as u32).unwrap().name();
let mut err = match self.tcx.hir.span_if_local(item_def_id) {
Some(span) => struct_span_err!(
@ -169,9 +169,6 @@ fn collect_item(&mut self, item_index: usize, item_def_id: DefId) {
}
err.emit();
}
_ => {
// OK.
}
}
// Matched.
@ -194,7 +191,7 @@ pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> {
}
}
return None;
None
}
pub fn collect<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> LanguageItems {

View File

@ -170,7 +170,7 @@ fn live_node_kind_to_string(lnk: LiveNodeKind, tcx: TyCtxt<'_, '_, '_>) -> Strin
VarDefNode(s) => {
format!("Var def node [{}]", cm.span_to_string(s))
}
ExitNode => "Exit node".to_string(),
ExitNode => "Exit node".to_owned(),
}
}
@ -330,7 +330,7 @@ fn variable_name(&self, var: Variable) -> String {
Local(LocalInfo { name, .. }) | Arg(_, name) => {
name.to_string()
},
CleanExit => "<clean-exit>".to_string()
CleanExit => "<clean-exit>".to_owned()
}
}
@ -1049,12 +1049,9 @@ fn propagate_through_expr(&mut self, expr: &Expr, succ: LiveNode)
// the construction of a closure itself is not important,
// but we have to consider the closed over variables.
let caps = match self.ir.capture_info_map.get(&expr.id) {
Some(caps) => caps.clone(),
None => {
span_bug!(expr.span, "no registered caps");
}
};
let caps = self.ir.capture_info_map.get(&expr.id).cloned().unwrap_or_else(||
span_bug!(expr.span, "no registered caps"));
caps.iter().rev().fold(succ, |succ, cap| {
self.init_from_succ(cap.ln, succ);
let var = self.variable(cap.var_hid, expr.span);
@ -1114,15 +1111,12 @@ fn propagate_through_expr(&mut self, expr: &Expr, succ: LiveNode)
self.init_empty(ln, succ);
let mut first_merge = true;
for arm in arms {
let body_succ =
self.propagate_through_expr(&arm.body, succ);
let guard_succ =
self.propagate_through_opt_expr(
arm.guard.as_ref().map(|g|
match g {
hir::Guard::If(e) => &**e,
}),
body_succ);
let body_succ = self.propagate_through_expr(&arm.body, succ);
let guard_succ = self.propagate_through_opt_expr(
arm.guard.as_ref().map(|hir::Guard::If(e)| &**e),
body_succ
);
// only consider the first pattern; any later patterns must have
// the same bindings, and we also consider the first pattern to be
// the "authoritative" set of ids
@ -1146,10 +1140,9 @@ fn propagate_through_expr(&mut self, expr: &Expr, succ: LiveNode)
let target = match label.target_id {
Ok(node_id) => self.break_ln.get(&node_id),
Err(err) => span_bug!(expr.span, "loop scope error: {}", err),
}.map(|x| *x);
// Now that we know the label we're going to,
// look it up in the break loop nodes table
}.cloned();
match target {
Some(b) => self.propagate_through_opt_expr(opt_expr.as_ref().map(|e| &**e), b),
@ -1159,18 +1152,13 @@ fn propagate_through_expr(&mut self, expr: &Expr, succ: LiveNode)
hir::ExprKind::Continue(label) => {
// Find which label this expr continues to
let sc = match label.target_id {
Ok(node_id) => node_id,
Err(err) => span_bug!(expr.span, "loop scope error: {}", err),
};
let sc = label.target_id.unwrap_or_else(|err|
span_bug!(expr.span, "loop scope error: {}", err));
// Now that we know the label we're going to,
// look it up in the continue loop nodes table
match self.cont_ln.get(&sc) {
Some(&b) => b,
None => span_bug!(expr.span, "continue to unknown label")
}
self.cont_ln.get(&sc).cloned().unwrap_or_else(||
span_bug!(expr.span, "continue to unknown label"))
}
hir::ExprKind::Assign(ref l, ref r) => {
@ -1450,8 +1438,8 @@ fn propagate_through_loop(&mut self,
self.propagate_through_expr(&cond, ln)
}
};
assert!(cond_ln == new_cond_ln);
assert!(body_ln == self.propagate_through_block(body, cond_ln));
assert_eq!(cond_ln, new_cond_ln);
assert_eq!(body_ln, self.propagate_through_block(body, cond_ln));
}
cond_ln
@ -1576,7 +1564,7 @@ fn check_place(&mut self, expr: &'tcx Expr) {
fn should_warn(&self, var: Variable) -> Option<String> {
let name = self.ir.variable_name(var);
if name.is_empty() || name.as_bytes()[0] == ('_' as u8) {
if name.is_empty() || name.as_bytes()[0] == b'_' {
None
} else {
Some(name)

View File

@ -515,10 +515,8 @@ pub fn encl_scope(&self, id: Scope) -> Scope {
/// Returns the lifetime of the local variable `var_id`
pub fn var_scope(&self, var_id: hir::ItemLocalId) -> Scope {
match self.var_map.get(&var_id) {
Some(&r) => r,
None => { bug!("no enclosing scope for id {:?}", var_id); }
}
self.var_map.get(&var_id).cloned().unwrap_or_else(||
bug!("no enclosing scope for id {:?}", var_id))
}
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:
fn resolve_arm<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, arm: &'tcx hir::Arm) {
visitor.terminating_scopes.insert(arm.body.hir_id.local_id);
if let Some(ref g) = arm.guard {
match g {
hir::Guard::If(ref expr) => visitor.terminating_scopes.insert(expr.hir_id.local_id),
};
if let Some(hir::Guard::If(ref expr)) = arm.guard {
visitor.terminating_scopes.insert(expr.hir_id.local_id);
}
intravisit::walk_arm(visitor, arm);

View File

@ -1872,18 +1872,15 @@ fn visit_fn_like_elision(
node: hir::TraitItemKind::Method(_, ref m),
..
}) => {
match self.tcx
if let hir::ItemKind::Trait(.., ref trait_items) = self.tcx
.hir
.expect_item(self.tcx.hir.get_parent(parent))
.node
{
hir::ItemKind::Trait(.., ref trait_items) => {
assoc_item_kind = trait_items
.iter()
.find(|ti| ti.id.node_id == parent)
.map(|ti| ti.kind);
}
_ => {}
assoc_item_kind = trait_items
.iter()
.find(|ti| ti.id.node_id == parent)
.map(|ti| ti.kind);
}
match *m {
hir::TraitMethod::Required(_) => None,
@ -1895,19 +1892,16 @@ fn visit_fn_like_elision(
node: hir::ImplItemKind::Method(_, body),
..
}) => {
match self.tcx
if let hir::ItemKind::Impl(.., ref self_ty, ref impl_items) = self.tcx
.hir
.expect_item(self.tcx.hir.get_parent(parent))
.node
{
hir::ItemKind::Impl(.., ref self_ty, ref impl_items) => {
impl_self = Some(self_ty);
assoc_item_kind = impl_items
.iter()
.find(|ii| ii.id.node_id == parent)
.map(|ii| ii.kind);
}
_ => {}
impl_self = Some(self_ty);
assoc_item_kind = impl_items
.iter()
.find(|ii| ii.id.node_id == parent)
.map(|ii| ii.kind);
}
Some(body)
}
@ -2541,15 +2535,12 @@ fn insert_late_bound_lifetimes(
appears_in_where_clause.visit_generics(generics);
for param in &generics.params {
match param.kind {
hir::GenericParamKind::Lifetime { .. } => {
if !param.bounds.is_empty() {
// `'a: 'b` means both `'a` and `'b` are referenced
appears_in_where_clause
.regions.insert(hir::LifetimeName::Param(param.name.modern()));
}
if let hir::GenericParamKind::Lifetime { .. } = param.kind {
if !param.bounds.is_empty() {
// `'a: 'b` means both `'a` and `'b` are referenced
appears_in_where_clause
.regions.insert(hir::LifetimeName::Param(param.name.modern()));
}
hir::GenericParamKind::Type { .. } => {}
}
}

View File

@ -523,15 +523,12 @@ fn skip_stability_check_due_to_privacy(self, mut def_id: DefId) -> bool {
Some(Def::Method(_)) |
Some(Def::AssociatedTy(_)) |
Some(Def::AssociatedConst(_)) => {
match self.associated_item(def_id).container {
ty::TraitContainer(trait_def_id) => {
// Trait methods do not declare visibility (even
// for visibility info in cstore). Use containing
// trait instead, so methods of pub traits are
// themselves considered pub.
def_id = trait_def_id;
}
_ => {}
if let ty::TraitContainer(trait_def_id) = self.associated_item(def_id).container {
// Trait methods do not declare visibility (even
// for visibility info in cstore). Use containing
// trait instead, so methods of pub traits are
// themselves considered pub.
def_id = trait_def_id;
}
}
_ => {}