Skip to content

Commit

Permalink
prefer if let to match with None => {} arm in some places
Browse files Browse the repository at this point in the history
This is a spiritual succesor to #34268/8531d581, in which we replaced a
number of matches of None to the unit value with `if let` conditionals
where it was judged that this made for clearer/simpler code (as would be
recommended by Manishearth/rust-clippy's `single_match` lint). The same
rationale applies to matches of None to the empty block.
  • Loading branch information
zackmdavis committed Jul 3, 2016
1 parent 5e858f3 commit d37edef
Show file tree
Hide file tree
Showing 47 changed files with 215 additions and 349 deletions.
7 changes: 2 additions & 5 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,11 +840,8 @@ pub fn write(output: &mut Write, args: Arguments) -> Result {
}

// There can be only one trailing string piece left.
match pieces.next() {
Some(piece) => {
formatter.buf.write_str(*piece)?;
}
None => {}
if let Some(piece) = pieces.next() {
formatter.buf.write_str(*piece)?;
}

Ok(())
Expand Down
5 changes: 2 additions & 3 deletions src/librand/rand_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,8 @@ impl Rand for char {
// Rejection sampling. About 0.2% of numbers with at most
// 21-bits are invalid codepoints (surrogates), so this
// will succeed first go almost every time.
match char::from_u32(rng.next_u32() & CHAR_MASK) {
Some(c) => return c,
None => {}
if let Some(c) = char::from_u32(rng.next_u32() & CHAR_MASK) {
return c;
}
}
}
Expand Down
11 changes: 4 additions & 7 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1697,13 +1697,10 @@ impl<'a> State<'a> {
self.commasep(Inconsistent, &data.inputs, |s, ty| s.print_type(&ty))?;
word(&mut self.s, ")")?;

match data.output {
None => {}
Some(ref ty) => {
self.space_if_not_bol()?;
self.word_space("->")?;
self.print_type(&ty)?;
}
if let Some(ref ty) = data.output {
self.space_if_not_bol()?;
self.word_space("->")?;
self.print_type(&ty)?;
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/librustc/infer/region_inference/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,11 +842,8 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
where F: FnMut(&RegionVarBindings<'a, 'gcx, 'tcx>, Region, Region)
{
let vars = TwoRegions { a: a, b: b };
match self.combine_map(t).borrow().get(&vars) {
Some(&c) => {
return ReVar(c);
}
None => {}
if let Some(&c) = self.combine_map(t).borrow().get(&vars) {
return ReVar(c);
}
let c = self.new_region_var(MiscVariable(origin.span()));
self.combine_map(t).borrow_mut().insert(vars, c);
Expand Down
11 changes: 4 additions & 7 deletions src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,13 +1055,10 @@ impl<'a> ast_visit::Visitor for EarlyContext<'a> {
// Output any lints that were previously added to the session.
impl<'a, 'tcx> IdVisitingOperation for LateContext<'a, 'tcx> {
fn visit_id(&mut self, id: ast::NodeId) {
match self.sess().lints.borrow_mut().remove(&id) {
None => {}
Some(lints) => {
debug!("LateContext::visit_id: id={:?} lints={:?}", id, lints);
for (lint_id, span, msg) in lints {
self.span_lint(lint_id.lint, span, &msg[..])
}
if let Some(lints) = self.sess().lints.borrow_mut().remove(&id) {
debug!("LateContext::visit_id: id={:?} lints={:?}", id, lints);
for (lint_id, span, msg) in lints {
self.span_lint(lint_id.lint, span, &msg[..])
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/middle/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ fn build_nodeid_to_index(decl: Option<&hir::FnDecl>,
// into cfg itself? i.e. introduce a fn-based flow-graph in
// addition to the current block-based flow-graph, rather than
// have to put traversals like this here?
match decl {
None => {}
Some(decl) => add_entries_from_fn_decl(&mut index, decl, cfg.entry)
if let Some(decl) = decl {
add_entries_from_fn_decl(&mut index, decl, cfg.entry);
}

cfg.graph.each_node(|node_idx, node| {
Expand Down
15 changes: 6 additions & 9 deletions src/librustc/middle/dependency_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ fn calculate_type(sess: &session::Session,
// If the global prefer_dynamic switch is turned off, first attempt
// static linkage (this can fail).
config::CrateTypeExecutable if !sess.opts.cg.prefer_dynamic => {
match attempt_static(sess) {
Some(v) => return v,
None => {}
if let Some(v) = attempt_static(sess) {
return v;
}
}

Expand All @@ -119,9 +118,8 @@ fn calculate_type(sess: &session::Session,
// to be found, we generate some nice pretty errors.
config::CrateTypeStaticlib |
config::CrateTypeCdylib => {
match attempt_static(sess) {
Some(v) => return v,
None => {}
if let Some(v) = attempt_static(sess) {
return v;
}
for cnum in sess.cstore.crates() {
let src = sess.cstore.used_crate_source(cnum);
Expand All @@ -136,9 +134,8 @@ fn calculate_type(sess: &session::Session,
// to try to eagerly statically link all dependencies. This is normally
// done for end-product dylibs, not intermediate products.
config::CrateTypeDylib if !sess.opts.cg.prefer_dynamic => {
match attempt_static(sess) {
Some(v) => return v,
None => {}
if let Some(v) = attempt_static(sess) {
return v;
}
}

Expand Down
37 changes: 17 additions & 20 deletions src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,26 +735,23 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {

for i in 0..autoderefs {
let deref_id = ty::MethodCall::autoderef(expr.id, i as u32);
match self.mc.infcx.node_method_ty(deref_id) {
None => {}
Some(method_ty) => {
let cmt = return_if_err!(self.mc.cat_expr_autoderefd(expr, i));

// the method call infrastructure should have
// replaced all late-bound regions with variables:
let self_ty = method_ty.fn_sig().input(0);
let self_ty = self.tcx().no_late_bound_regions(&self_ty).unwrap();

let (m, r) = match self_ty.sty {
ty::TyRef(r, ref m) => (m.mutbl, r),
_ => span_bug!(expr.span,
"bad overloaded deref type {:?}",
method_ty)
};
let bk = ty::BorrowKind::from_mutbl(m);
self.delegate.borrow(expr.id, expr.span, cmt,
*r, bk, AutoRef);
}
if let Some(method_ty) = self.mc.infcx.node_method_ty(deref_id) {
let cmt = return_if_err!(self.mc.cat_expr_autoderefd(expr, i));

// the method call infrastructure should have
// replaced all late-bound regions with variables:
let self_ty = method_ty.fn_sig().input(0);
let self_ty = self.tcx().no_late_bound_regions(&self_ty).unwrap();

let (m, r) = match self_ty.sty {
ty::TyRef(r, ref m) => (m.mutbl, r),
_ => span_bug!(expr.span,
"bad overloaded deref type {:?}",
method_ty)
};
let bk = ty::BorrowKind::from_mutbl(m);
self.delegate.borrow(expr.id, expr.span, cmt,
*r, bk, AutoRef);
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
fn arm_pats_bindings<F>(&mut self, pat: Option<&hir::Pat>, f: F) where
F: FnMut(&mut Liveness<'a, 'tcx>, LiveNode, Variable, Span, NodeId),
{
match pat {
Some(pat) => {
self.pat_bindings(pat, f);
}
None => {}
if let Some(pat) = pat {
self.pat_bindings(pat, f);
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for LifetimeContext<'a, 'tcx> {
fn visit_generics(&mut self, generics: &hir::Generics) {
for ty_param in generics.ty_params.iter() {
walk_list!(self, visit_ty_param_bound, &ty_param.bounds);
match ty_param.default {
Some(ref ty) => self.visit_ty(&ty),
None => {}
if let Some(ref ty) = ty_param.default {
self.visit_ty(&ty);
}
}
for predicate in &generics.where_clause.predicates {
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/middle/weak_lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ impl<'a> Context<'a> {

impl<'a, 'v> Visitor<'v> for Context<'a> {
fn visit_foreign_item(&mut self, i: &hir::ForeignItem) {
match lang_items::extract(&i.attrs) {
None => {}
Some(lang_item) => self.register(&lang_item, i.span),
if let Some(lang_item) = lang_items::extract(&i.attrs) {
self.register(&lang_item, i.span);
}
intravisit::walk_foreign_item(self, i)
}
Expand Down
13 changes: 5 additions & 8 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,12 @@ impl Session {
msg: String) {
let lint_id = lint::LintId::of(lint);
let mut lints = self.lints.borrow_mut();
match lints.get_mut(&id) {
Some(arr) => {
let tuple = (lint_id, sp, msg);
if !arr.contains(&tuple) {
arr.push(tuple);
}
return;
if let Some(arr) = lints.get_mut(&id) {
let tuple = (lint_id, sp, msg);
if !arr.contains(&tuple) {
arr.push(tuple);
}
None => {}
return;
}
lints.insert(id, vec!((lint_id, sp, msg)));
}
Expand Down
11 changes: 5 additions & 6 deletions src/librustc/ty/contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,12 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
// which is incorrect. This value was computed based on the crutch
// value for the type contents of list. The correct value is
// TC::OwnsOwned. This manifested as issue #4821.
match cache.get(&ty) {
Some(tc) => { return *tc; }
None => {}
if let Some(tc) = cache.get(&ty) {
return *tc;
}
match tcx.tc_cache.borrow().get(&ty) { // Must check both caches!
Some(tc) => { return *tc; }
None => {}
// Must check both caches!
if let Some(tc) = tcx.tc_cache.borrow().get(&ty) {
return *tc;
}
cache.insert(ty, TC::None);

Expand Down
6 changes: 2 additions & 4 deletions src/librustc/ty/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> { self.0 }

fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
match self.tcx().normalized_cache.borrow().get(&ty).cloned() {
None => {}
Some(u) => return u
if let Some(u) = self.tcx().normalized_cache.borrow().get(&ty).cloned() {
return u;
}

// FIXME(eddyb) should local contexts have a cache too?
Expand Down Expand Up @@ -714,4 +713,3 @@ impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector {
false
}
}

15 changes: 6 additions & 9 deletions src/librustc/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,16 +712,13 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
// struct Foo;
// struct Bar<T> { x: Bar<Foo> }

match iter.next() {
Some(&seen_type) => {
if same_struct_or_enum(seen_type, def) {
debug!("SelfRecursive: {:?} contains {:?}",
seen_type,
ty);
return Representability::SelfRecursive;
}
if let Some(&seen_type) = iter.next() {
if same_struct_or_enum(seen_type, def) {
debug!("SelfRecursive: {:?} contains {:?}",
seen_type,
ty);
return Representability::SelfRecursive;
}
None => {}
}

// We also need to know whether the first item contains other types
Expand Down
7 changes: 2 additions & 5 deletions src/librustc_borrowck/borrowck/move_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,8 @@ impl<'a, 'tcx> MoveData<'tcx> {
/// `lp` and any of its base paths that do not yet have an index.
pub fn move_path(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
lp: Rc<LoanPath<'tcx>>) -> MovePathIndex {
match self.path_map.borrow().get(&lp) {
Some(&index) => {
return index;
}
None => {}
if let Some(&index) = self.path_map.borrow().get(&lp) {
return index;
}

let index = match lp.kind {
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_const_eval/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,8 @@ fn check_expr(cx: &mut MatchCheckCtxt, ex: &hir::Expr) {

// Second, if there is a guard on each arm, make sure it isn't
// assigning or borrowing anything mutably.
match arm.guard {
Some(ref guard) => check_for_mutation_in_guard(cx, &guard),
None => {}
if let Some(ref guard) = arm.guard {
check_for_mutation_in_guard(cx, &guard);
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,9 @@ impl LateLintPass for UnusedResults {
if attr.check_name("must_use") {
let mut msg = "unused result which must be used".to_string();
// check for #[must_use="..."]
match attr.value_str() {
None => {}
Some(s) => {
msg.push_str(": ");
msg.push_str(&s);
}
if let Some(s) = attr.value_str() {
msg.push_str(": ");
msg.push_str(&s);
}
cx.span_lint(UNUSED_MUST_USE, sp, &msg);
return true;
Expand Down
22 changes: 10 additions & 12 deletions src/librustc_llvm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,17 @@ fn main() {
let llvm_config = env::var_os("LLVM_CONFIG")
.map(PathBuf::from)
.unwrap_or_else(|| {
match env::var_os("CARGO_TARGET_DIR").map(PathBuf::from) {
Some(dir) => {
let to_test = dir.parent()
.unwrap()
.parent()
.unwrap()
.join(&target)
.join("llvm/bin/llvm-config");
if Command::new(&to_test).output().is_ok() {
return to_test;
}
if let Some(dir) = env::var_os("CARGO_TARGET_DIR")
.map(PathBuf::from) {
let to_test = dir.parent()
.unwrap()
.parent()
.unwrap()
.join(&target)
.join("llvm/bin/llvm-config");
if Command::new(&to_test).output().is_ok() {
return to_test;
}
None => {}
}
PathBuf::from("llvm-config")
});
Expand Down
15 changes: 6 additions & 9 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,15 +682,12 @@ fn each_child_of_item_or_crate<F, G>(intr: Rc<IdentInterner>,
};

// Get the item.
match crate_data.get_item(child_def_id.index) {
None => {}
Some(child_item_doc) => {
// Hand off the item to the callback.
let child_name = item_name(&intr, child_item_doc);
let def_like = item_to_def_like(crate_data, child_item_doc, child_def_id);
let visibility = item_visibility(child_item_doc);
callback(def_like, child_name, visibility);
}
if let Some(child_item_doc) = crate_data.get_item(child_def_id.index) {
// Hand off the item to the callback.
let child_name = item_name(&intr, child_item_doc);
let def_like = item_to_def_like(crate_data, child_item_doc, child_def_id);
let visibility = item_visibility(child_item_doc);
callback(def_like, child_name, visibility);
}
}

Expand Down
Loading

0 comments on commit d37edef

Please # to comment.