Skip to content

Replace some verbose match statements with their if let equivalent. #19405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 86 additions & 153 deletions src/librustc/lint/builtin.rs

Large diffs are not rendered by default.

71 changes: 27 additions & 44 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,41 +514,27 @@ fn each_child_of_item_or_crate(intr: Rc<IdentInterner>,
let inherent_impl_def_id = item_def_id(inherent_impl_def_id_doc,
cdata);
let items = reader::get_doc(rbml::Doc::new(cdata.data()), tag_items);
match maybe_find_item(inherent_impl_def_id.node, items) {
None => {}
Some(inherent_impl_doc) => {
let _ = reader::tagged_docs(inherent_impl_doc,
tag_item_impl_item,
|impl_item_def_id_doc| {
let impl_item_def_id = item_def_id(impl_item_def_id_doc,
cdata);
match maybe_find_item(impl_item_def_id.node, items) {
None => {}
Some(impl_method_doc) => {
match item_family(impl_method_doc) {
StaticMethod => {
// Hand off the static method
// to the callback.
let static_method_name =
item_name(&*intr, impl_method_doc);
let static_method_def_like =
item_to_def_like(impl_method_doc,
impl_item_def_id,
cdata.cnum);
callback(static_method_def_like,
static_method_name,
item_visibility(impl_method_doc));
}
_ => {}
}
}
if let Some(inherent_impl_doc) = maybe_find_item(inherent_impl_def_id.node, items) {
let _ = reader::tagged_docs(inherent_impl_doc,
tag_item_impl_item,
|impl_item_def_id_doc| {
let impl_item_def_id = item_def_id(impl_item_def_id_doc,
cdata);
if let Some(impl_method_doc) = maybe_find_item(impl_item_def_id.node, items) {
if let StaticMethod = item_family(impl_method_doc) {
// Hand off the static method to the callback.
let static_method_name = item_name(&*intr, impl_method_doc);
let static_method_def_like = item_to_def_like(impl_method_doc,
impl_item_def_id,
cdata.cnum);
callback(static_method_def_like,
static_method_name,
item_visibility(impl_method_doc));
}

true
});
}
}
true
});
}

true
});

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

// Get the item.
match maybe_find_item(child_def_id.node, other_crates_items) {
None => {}
Some(child_item_doc) => {
// Hand off the item to the callback.
let def_like = item_to_def_like(child_item_doc,
child_def_id,
child_def_id.krate);
// These items have a public visibility because they're part of
// a public re-export.
callback(def_like, token::intern(name), ast::Public);
}
if let Some(child_item_doc) = maybe_find_item(child_def_id.node, other_crates_items) {
// Hand off the item to the callback.
let def_like = item_to_def_like(child_item_doc,
child_def_id,
child_def_id.krate);
// These items have a public visibility because they're part of
// a public re-export.
callback(def_like, token::intern(name), ast::Public);
}

true
Expand Down
149 changes: 63 additions & 86 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,17 +433,13 @@ fn encode_reexported_static_trait_methods(ecx: &EncodeContext,
match ecx.tcx.trait_items_cache.borrow().get(&exp.def_id) {
Some(trait_items) => {
for trait_item in trait_items.iter() {
match *trait_item {
ty::MethodTraitItem(ref m) => {
encode_reexported_static_method(rbml_w,
exp,
m.def_id,
m.name);
}
_ => {}
if let ty::MethodTraitItem(ref m) = *trait_item {
encode_reexported_static_method(rbml_w,
exp,
m.def_id,
m.name);
}
}

true
}
None => { false }
Expand All @@ -454,46 +450,42 @@ fn encode_reexported_static_methods(ecx: &EncodeContext,
rbml_w: &mut Encoder,
mod_path: PathElems,
exp: &middle::resolve::Export2) {
match ecx.tcx.map.find(exp.def_id.node) {
Some(ast_map::NodeItem(item)) => {
let original_name = token::get_ident(item.ident);

let path_differs = ecx.tcx.map.with_path(exp.def_id.node, |path| {
let (mut a, mut b) = (path, mod_path.clone());
loop {
match (a.next(), b.next()) {
(None, None) => return true,
(None, _) | (_, None) => return false,
(Some(x), Some(y)) => if x != y { return false },
}
if let Some(ast_map::NodeItem(item)) = ecx.tcx.map.find(exp.def_id.node) {
let original_name = token::get_ident(item.ident);

let path_differs = ecx.tcx.map.with_path(exp.def_id.node, |path| {
let (mut a, mut b) = (path, mod_path.clone());
loop {
match (a.next(), b.next()) {
(None, None) => return true,
(None, _) | (_, None) => return false,
(Some(x), Some(y)) => if x != y { return false },
}
});
}
});

//
// We don't need to reexport static methods on items
// declared in the same module as our `pub use ...` since
// that's done when we encode the item itself.
//
// The only exception is when the reexport *changes* the
// name e.g. `pub use Foo = self::Bar` -- we have
// encoded metadata for static methods relative to Bar,
// but not yet for Foo.
//
if path_differs || original_name.get() != exp.name.as_slice() {
if !encode_reexported_static_base_methods(ecx, rbml_w, exp) {
if encode_reexported_static_trait_methods(ecx, rbml_w, exp) {
debug!("(encode reexported static methods) {} \
[trait]",
original_name);
}
}
else {
debug!("(encode reexported static methods) {} [base]",
original_name);
//
// We don't need to reexport static methods on items
// declared in the same module as our `pub use ...` since
// that's done when we encode the item itself.
//
// The only exception is when the reexport *changes* the
// name e.g. `pub use Foo = self::Bar` -- we have
// encoded metadata for static methods relative to Bar,
// but not yet for Foo.
//
if path_differs || original_name.get() != exp.name.as_slice() {
if !encode_reexported_static_base_methods(ecx, rbml_w, exp) {
if encode_reexported_static_trait_methods(ecx, rbml_w, exp) {
debug!("(encode reexported static methods) {} [trait]",
original_name);
}
}
else {
debug!("(encode reexported static methods) {} [base]",
original_name);
}
}
_ => {}
}
}

Expand Down Expand Up @@ -581,19 +573,15 @@ fn encode_info_for_mod(ecx: &EncodeContext,
true
});

match item.node {
ast::ItemImpl(..) => {
let (ident, did) = (item.ident, item.id);
debug!("(encoding info for module) ... encoding impl {} \
({}/{})",
token::get_ident(ident),
did, ecx.tcx.map.node_to_string(did));
if let ast::ItemImpl(..) = item.node {
let (ident, did) = (item.ident, item.id);
debug!("(encoding info for module) ... encoding impl {} ({}/{})",
token::get_ident(ident),
did, ecx.tcx.map.node_to_string(did));

rbml_w.start_tag(tag_mod_impl);
rbml_w.wr_str(def_to_string(local_def(did)).as_slice());
rbml_w.end_tag();
}
_ => {}
rbml_w.start_tag(tag_mod_impl);
rbml_w.wr_str(def_to_string(local_def(did)).as_slice());
rbml_w.end_tag();
}
}

Expand Down Expand Up @@ -923,12 +911,9 @@ fn encode_method_argument_names(rbml_w: &mut Encoder,
rbml_w.start_tag(tag_method_argument_names);
for arg in decl.inputs.iter() {
rbml_w.start_tag(tag_method_argument_name);
match arg.pat.node {
ast::PatIdent(_, ref path1, _) => {
let name = token::get_ident(path1.node);
rbml_w.writer.write(name.get().as_bytes());
}
_ => {}
if let ast::PatIdent(_, ref path1, _) = arg.pat.node {
let name = token::get_ident(path1.node);
rbml_w.writer.write(name.get().as_bytes());
}
rbml_w.end_tag();
}
Expand Down Expand Up @@ -1854,22 +1839,19 @@ struct ImplVisitor<'a, 'b:'a, 'c:'a, 'tcx:'b> {

impl<'a, 'b, 'c, 'tcx, 'v> Visitor<'v> for ImplVisitor<'a, 'b, 'c, 'tcx> {
fn visit_item(&mut self, item: &ast::Item) {
match item.node {
ast::ItemImpl(_, Some(ref trait_ref), _, _) => {
let def_map = &self.ecx.tcx.def_map;
let trait_def = def_map.borrow()[trait_ref.ref_id].clone();
let def_id = trait_def.def_id();

// Load eagerly if this is an implementation of the Drop trait
// or if the trait is not defined in this crate.
if Some(def_id) == self.ecx.tcx.lang_items.drop_trait() ||
def_id.krate != ast::LOCAL_CRATE {
self.rbml_w.start_tag(tag_impls_impl);
encode_def_id(self.rbml_w, local_def(item.id));
self.rbml_w.end_tag();
}
if let ast::ItemImpl(_, Some(ref trait_ref), _, _) = item.node {
let def_map = &self.ecx.tcx.def_map;
let trait_def = def_map.borrow()[trait_ref.ref_id].clone();
let def_id = trait_def.def_id();

// Load eagerly if this is an implementation of the Drop trait
// or if the trait is not defined in this crate.
if Some(def_id) == self.ecx.tcx.lang_items.drop_trait() ||
def_id.krate != ast::LOCAL_CRATE {
self.rbml_w.start_tag(tag_impls_impl);
encode_def_id(self.rbml_w, local_def(item.id));
self.rbml_w.end_tag();
}
_ => {}
}
visit::walk_item(self, item);
}
Expand Down Expand Up @@ -1931,17 +1913,12 @@ fn encode_reachable_extern_fns(ecx: &EncodeContext, rbml_w: &mut Encoder) {
rbml_w.start_tag(tag_reachable_extern_fns);

for id in ecx.reachable.iter() {
match ecx.tcx.map.find(*id) {
Some(ast_map::NodeItem(i)) => {
match i.node {
ast::ItemFn(_, _, abi, ref generics, _)
if abi != abi::Rust && !generics.is_type_parameterized() => {
rbml_w.wr_tagged_u32(tag_reachable_extern_fn_id, *id);
}
_ => {}
if let Some(ast_map::NodeItem(i)) = ecx.tcx.map.find(*id) {
if let ast::ItemFn(_, _, abi, ref generics, _) = i.node {
if abi != abi::Rust && !generics.is_type_parameterized() {
rbml_w.wr_tagged_u32(tag_reachable_extern_fn_id, *id);
}
}
_ => {}
}
}

Expand Down
12 changes: 3 additions & 9 deletions src/librustc/middle/borrowck/check_loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,14 +892,9 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
let guarantor = cmt.guarantor();
debug!("check_for_aliasable_mutable_writes(cmt={}, guarantor={})",
cmt.repr(this.tcx()), guarantor.repr(this.tcx()));
match guarantor.cat {
mc::cat_deref(ref b, _, mc::BorrowedPtr(ty::MutBorrow, _)) => {
// Statically prohibit writes to `&mut` when aliasable

check_for_aliasability_violation(this, span, b.clone());
}

_ => {}
if let mc::cat_deref(ref b, _, mc::BorrowedPtr(ty::MutBorrow, _)) = guarantor.cat {
// Statically prohibit writes to `&mut` when aliasable
check_for_aliasability_violation(this, span, b.clone());
}

return true; // no errors reported
Expand Down Expand Up @@ -962,4 +957,3 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
self.bccx.loan_path_to_string(loan_path)).as_slice());
}
}

27 changes: 11 additions & 16 deletions src/librustc/middle/borrowck/gather_loans/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,10 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for GatherLoanCtxt<'a, 'tcx> {
cmt.repr(self.tcx()),
mode);

match cmt.cat {
mc::cat_downcast(..) =>
gather_moves::gather_match_variant(
self.bccx, &self.move_data, &self.move_error_collector,
matched_pat, cmt, mode),
_ => {}
if let mc::cat_downcast(..) = cmt.cat {
gather_moves::gather_match_variant(
self.bccx, &self.move_data, &self.move_error_collector,
matched_pat, cmt, mode);
}
}

Expand Down Expand Up @@ -489,17 +487,14 @@ struct StaticInitializerCtxt<'a, 'tcx: 'a> {

impl<'a, 'tcx, 'v> Visitor<'v> for StaticInitializerCtxt<'a, 'tcx> {
fn visit_expr(&mut self, ex: &Expr) {
match ex.node {
ast::ExprAddrOf(mutbl, ref base) => {
let base_cmt = self.bccx.cat_expr(&**base);
let borrow_kind = ty::BorrowKind::from_mutbl(mutbl);
// Check that we don't allow borrows of unsafe static items.
if check_aliasability(self.bccx, ex.span, euv::AddrOf,
base_cmt, borrow_kind).is_err() {
return; // reported an error, no sense in reporting more.
}
if let ast::ExprAddrOf(mutbl, ref base) = ex.node {
let base_cmt = self.bccx.cat_expr(&**base);
let borrow_kind = ty::BorrowKind::from_mutbl(mutbl);
// Check that we don't allow borrows of unsafe static items.
if check_aliasability(self.bccx, ex.span, euv::AddrOf,
base_cmt, borrow_kind).is_err() {
return; // reported an error, no sense in reporting more.
}
_ => {}
}

visit::walk_expr(self, ex);
Expand Down
14 changes: 5 additions & 9 deletions src/librustc/middle/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,8 @@ impl<'a, 'tcx> ConstEvalVisitor<'a, 'tcx> {

impl<'a, 'tcx, 'v> Visitor<'v> for ConstEvalVisitor<'a, 'tcx> {
fn visit_ty(&mut self, t: &ast::Ty) {
match t.node {
ast::TyFixedLengthVec(_, ref expr) => {
check::check_const_in_type(self.tcx, &**expr, ty::mk_uint());
}
_ => {}
if let ast::TyFixedLengthVec(_, ref expr) = t.node {
check::check_const_in_type(self.tcx, &**expr, ty::mk_uint());
}

visit::walk_ty(self, t);
Expand Down Expand Up @@ -321,10 +318,9 @@ pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr) -> P<ast::Pat> {

ast::ExprCall(ref callee, ref args) => {
let def = tcx.def_map.borrow()[callee.id].clone();
match tcx.def_map.borrow_mut().entry(expr.id) {
Vacant(entry) => { entry.set(def); }
_ => {}
};
if let Vacant(entry) = tcx.def_map.borrow_mut().entry(expr.id) {
entry.set(def);
}
let path = match def {
def::DefStruct(def_id) => def_to_path(tcx, def_id),
def::DefVariant(_, variant_did, _) => def_to_path(tcx, variant_did),
Expand Down
Loading