Skip to content
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

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

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
139 changes: 36 additions & 103 deletions src/librustc/lint/builtin.rs
Expand Up @@ -61,23 +61,13 @@ impl LintPass for WhileTrue {
}

fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
match e.node {
ast::ExprWhile(ref cond, _, _) => {
match cond.node {
ast::ExprLit(ref lit) => {
match lit.node {
ast::LitBool(true) => {
if let ast::ExprWhile(ref cond, _, _) = e.node {
if let ast::ExprLit(ref lit) = cond.node {
if let ast::LitBool(true) = lit.node {
cx.span_lint(WHILE_TRUE, e.span,
"denote infinite loops with loop \
{ ... }");
}
_ => {}
}
}
_ => ()
"denote infinite loops with loop { ... }");
}
}
_ => ()
}
}
}
Expand All @@ -93,15 +83,12 @@ impl LintPass for UnusedCasts {
}

fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
match e.node {
ast::ExprCast(ref expr, ref ty) => {
if let ast::ExprCast(ref expr, ref ty) = e.node {
let t_t = ast_ty_to_ty(cx, &infer::new_infer_ctxt(cx.tcx), &**ty);
if ty::expr_ty(cx.tcx, &**expr) == t_t {
cx.span_lint(UNUSED_TYPECASTS, ty.span, "unnecessary type cast");
}
}
_ => ()
}
}
}

Expand Down Expand Up @@ -540,9 +527,8 @@ struct RawPtrDerivingVisitor<'a, 'tcx: 'a> {
impl<'a, 'tcx, 'v> Visitor<'v> for RawPtrDerivingVisitor<'a, 'tcx> {
fn visit_ty(&mut self, ty: &ast::Ty) {
static MSG: &'static str = "use of `#[deriving]` with a raw pointer";
match ty.node {
ast::TyPtr(..) => self.cx.span_lint(RAW_POINTER_DERIVING, ty.span, MSG),
_ => {}
if let ast::TyPtr(..) = ty.node {
self.cx.span_lint(RAW_POINTER_DERIVING, ty.span, MSG);
}
visit::walk_ty(self, ty);
}
Expand Down Expand Up @@ -720,9 +706,8 @@ impl LintPass for UnusedResults {
_ => return
};

match expr.node {
ast::ExprRet(..) => return,
_ => {}
if let ast::ExprRet(..) = expr.node {
return;
}

let t = ty::expr_ty(cx.tcx, expr);
Expand All @@ -733,12 +718,9 @@ impl LintPass for UnusedResults {
ty::ty_struct(did, _) |
ty::ty_enum(did, _) => {
if ast_util::is_local(did) {
match cx.tcx.map.get(did.node) {
ast_map::NodeItem(it) => {
if let ast_map::NodeItem(it) = cx.tcx.map.get(did.node) {
warned |= check_must_use(cx, it.attrs.as_slice(), s.span);
}
_ => {}
}
} else {
csearch::get_item_attrs(&cx.sess().cstore, did, |attrs| {
warned |= check_must_use(cx, attrs.as_slice(), s.span);
Expand Down Expand Up @@ -969,12 +951,9 @@ impl LintPass for NonSnakeCase {
}

fn check_item(&mut self, cx: &Context, it: &ast::Item) {
match it.node {
ast::ItemMod(_) => {
if let ast::ItemMod(_) = it.node {
self.check_snake_case(cx, "module", it.ident, it.span);
}
_ => {}
}
}

fn check_ty_method(&mut self, cx: &Context, t: &ast::TypeMethod) {
Expand All @@ -986,28 +965,19 @@ impl LintPass for NonSnakeCase {
}

fn check_pat(&mut self, cx: &Context, p: &ast::Pat) {
match &p.node {
&ast::PatIdent(_, ref path1, _) => {
match cx.tcx.def_map.borrow().get(&p.id) {
Some(&def::DefLocal(_)) => {
if let &ast::PatIdent(_, ref path1, _) = &p.node {
if let Some(&def::DefLocal(_)) = cx.tcx.def_map.borrow().get(&p.id) {
self.check_snake_case(cx, "variable", path1.node, p.span);
}
_ => {}
}
}
_ => {}
}
}

fn check_struct_def(&mut self, cx: &Context, s: &ast::StructDef,
_: ast::Ident, _: &ast::Generics, _: ast::NodeId) {
for sf in s.fields.iter() {
match sf.node {
ast::StructField_ { kind: ast::NamedField(ident, _), .. } => {
if let ast::StructField_ { kind: ast::NamedField(ident, _), .. } = sf.node {
self.check_snake_case(cx, "structure field", ident, sf.span);
}
_ => {}
}
}
}
}
Expand Down Expand Up @@ -1069,17 +1039,14 @@ pub struct UnusedParens;
impl UnusedParens {
fn check_unused_parens_core(&self, cx: &Context, value: &ast::Expr, msg: &str,
struct_lit_needs_parens: bool) {
match value.node {
ast::ExprParen(ref inner) => {
if let ast::ExprParen(ref inner) = value.node {
let necessary = struct_lit_needs_parens && contains_exterior_struct_lit(&**inner);
if !necessary {
cx.span_lint(UNUSED_PARENS, value.span,
format!("unnecessary parentheses around {}",
msg).as_slice())
}
}
_ => {}
}

/// Expressions that syntactically contain an "exterior" struct
/// literal i.e. not surrounded by any parens or other
Expand Down Expand Up @@ -1201,24 +1168,19 @@ impl LintPass for NonShorthandFieldPatterns {

fn check_pat(&mut self, cx: &Context, pat: &ast::Pat) {
let def_map = cx.tcx.def_map.borrow();
match pat.node {
ast::PatStruct(_, ref v, _) => {
if let ast::PatStruct(_, ref v, _) = pat.node {
for fieldpat in v.iter()
.filter(|fieldpat| !fieldpat.node.is_shorthand)
.filter(|fieldpat| def_map.get(&fieldpat.node.pat.id)
== Some(&def::DefLocal(fieldpat.node.pat.id))) {
match fieldpat.node.pat.node {
ast::PatIdent(_, ident, None) if ident.node.as_str()
== fieldpat.node.ident.as_str() => {
if let ast::PatIdent(_, ident, None) = fieldpat.node.pat.node {
if ident.node.as_str() == fieldpat.node.ident.as_str() {
cx.span_lint(NON_SHORTHAND_FIELD_PATTERNS, fieldpat.span,
format!("the `{}:` in this pattern is redundant and can \
be removed", ident.node.as_str()).as_slice())
},
_ => {},
}
}
},
_ => {}
}
}
}
}
Expand All @@ -1234,16 +1196,13 @@ impl LintPass for UnusedUnsafe {
}

fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
match e.node {
if let ast::ExprBlock(ref blk) = e.node {
// Don't warn about generated blocks, that'll just pollute the output.
ast::ExprBlock(ref blk) => {
if blk.rules == ast::UnsafeBlock(ast::UserProvided) &&
!cx.tcx.used_unsafe.borrow().contains(&blk.id) {
cx.span_lint(UNUSED_UNSAFE, blk.span, "unnecessary `unsafe` block");
}
}
_ => ()
}
}
}

Expand All @@ -1258,12 +1217,11 @@ impl LintPass for UnsafeBlocks {
}

fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
match e.node {
if let ast::ExprBlock(ref blk) = e.node {
// Don't warn about generated blocks, that'll just pollute the output.
ast::ExprBlock(ref blk) if blk.rules == ast::UnsafeBlock(ast::UserProvided) => {
if blk.rules == ast::UnsafeBlock(ast::UserProvided) {
cx.span_lint(UNSAFE_BLOCKS, blk.span, "usage of an `unsafe` block");
}
_ => ()
}
}
}
Expand All @@ -1282,18 +1240,14 @@ impl UnusedMut {
for p in pats.iter() {
pat_util::pat_bindings(&cx.tcx.def_map, &**p, |mode, id, _, path1| {
let ident = path1.node;
match mode {
ast::BindByValue(ast::MutMutable) => {
if let ast::BindByValue(ast::MutMutable) = mode {
if !token::get_ident(ident).get().starts_with("_") {
match mutables.entry(ident.name.uint()) {
Vacant(entry) => { entry.set(vec![id]); },
Occupied(mut entry) => { entry.get_mut().push(id); },
}
}
}
_ => {
}
}
});
}

Expand All @@ -1313,27 +1267,18 @@ impl LintPass for UnusedMut {
}

fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
match e.node {
ast::ExprMatch(_, ref arms, _) => {
if let ast::ExprMatch(_, ref arms, _) = e.node {
for a in arms.iter() {
self.check_unused_mut_pat(cx, a.pats.as_slice())
}
}
_ => {}
}
}

fn check_stmt(&mut self, cx: &Context, s: &ast::Stmt) {
match s.node {
ast::StmtDecl(ref d, _) => {
match d.node {
ast::DeclLocal(ref l) => {
if let ast::StmtDecl(ref d, _) = s.node {
if let ast::DeclLocal(ref l) = d.node {
self.check_unused_mut_pat(cx, slice::ref_slice(&l.pat));
},
_ => {}
}
},
_ => {}
}
}

Expand Down Expand Up @@ -1362,10 +1307,8 @@ impl LintPass for UnusedAllocation {
_ => return
}

match cx.tcx.adjustments.borrow().get(&e.id) {
Some(adjustment) => {
match *adjustment {
ty::AdjustDerefRef(ty::AutoDerefRef { ref autoref, .. }) => {
if let Some(adjustment) = cx.tcx.adjustments.borrow().get(&e.id) {
if let ty::AdjustDerefRef(ty::AutoDerefRef { ref autoref, .. }) = *adjustment {
match autoref {
&Some(ty::AutoPtr(_, ast::MutImmutable, None)) => {
cx.span_lint(UNUSED_ALLOCATION, e.span,
Expand All @@ -1378,10 +1321,6 @@ impl LintPass for UnusedAllocation {
_ => ()
}
}
_ => {}
}
}
_ => ()
}
}
}
Expand Down Expand Up @@ -1430,9 +1369,10 @@ impl MissingDoc {
// Only check publicly-visible items, using the result from the privacy pass.
// It's an option so the crate root can also use this function (it doesn't
// have a NodeId).
match id {
Some(ref id) if !cx.exported_items.contains(id) => return,
_ => ()
if let Some(ref id) = id {
if !cx.exported_items.contains(id) {
return;
}
}

let has_doc = attrs.iter().any(|a| {
Expand Down Expand Up @@ -1499,8 +1439,7 @@ impl LintPass for MissingDoc {
fn check_fn(&mut self, cx: &Context,
fk: visit::FnKind, _: &ast::FnDecl,
_: &ast::Block, _: Span, _: ast::NodeId) {
match fk {
visit::FkMethod(_, _, m) => {
if let visit::FkMethod(_, _, m) = fk {
// If the method is an impl for a trait, don't doc.
if method_context(cx, m) == TraitImpl { return; }

Expand All @@ -1509,8 +1448,6 @@ impl LintPass for MissingDoc {
self.check_missing_docs_attrs(cx, Some(m.id), m.attrs.as_slice(),
m.span, "a method");
}
_ => {}
}
}

fn check_ty_method(&mut self, cx: &Context, tm: &ast::TypeMethod) {
Expand All @@ -1519,15 +1456,14 @@ impl LintPass for MissingDoc {
}

fn check_struct_field(&mut self, cx: &Context, sf: &ast::StructField) {
match sf.node.kind {
ast::NamedField(_, vis) if vis == ast::Public || self.in_variant => {
if let ast::NamedField(_, vis) = sf.node.kind {
if vis == ast::Public || self.in_variant {
let cur_struct_def = *self.struct_def_stack.last()
.expect("empty struct_def_stack");
self.check_missing_docs_attrs(cx, Some(cur_struct_def),
sf.node.attrs.as_slice(), sf.span,
"a struct field")
}
_ => {}
}
}

Expand Down Expand Up @@ -1693,13 +1629,10 @@ impl LintPass for Stability {
match item.node {
ast::ItemTrait(_, _, ref supertraits, _) => {
for t in supertraits.iter() {
match *t {
ast::TraitTyParamBound(ref t) => {
if let ast::TraitTyParamBound(ref t) = *t {
let id = ty::trait_ref_to_def_id(cx.tcx, &t.trait_ref);
self.lint(cx, id, t.trait_ref.path.span);
}
_ => (/* pass */)
}
}
}
ast::ItemImpl(_, Some(ref t), _, _) => {
Expand Down