Skip to content

Commit d508de6

Browse files
authored
Auto merge of #34638 - zackmdavis:if_let_over_none_empty_block_arm, r=jseyfried
prefer `if let` to match with `None => {}` arm in some places This is a spiritual succesor to #34268 / 8531d58, 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. ---- r? @jseyfried
2 parents d98da85 + d37edef commit d508de6

File tree

47 files changed

+215
-349
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+215
-349
lines changed

src/libcore/fmt/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -840,11 +840,8 @@ pub fn write(output: &mut Write, args: Arguments) -> Result {
840840
}
841841

842842
// There can be only one trailing string piece left.
843-
match pieces.next() {
844-
Some(piece) => {
845-
formatter.buf.write_str(*piece)?;
846-
}
847-
None => {}
843+
if let Some(piece) = pieces.next() {
844+
formatter.buf.write_str(*piece)?;
848845
}
849846

850847
Ok(())

src/librand/rand_impls.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,8 @@ impl Rand for char {
144144
// Rejection sampling. About 0.2% of numbers with at most
145145
// 21-bits are invalid codepoints (surrogates), so this
146146
// will succeed first go almost every time.
147-
match char::from_u32(rng.next_u32() & CHAR_MASK) {
148-
Some(c) => return c,
149-
None => {}
147+
if let Some(c) = char::from_u32(rng.next_u32() & CHAR_MASK) {
148+
return c;
150149
}
151150
}
152151
}

src/librustc/hir/print.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,13 +1697,10 @@ impl<'a> State<'a> {
16971697
self.commasep(Inconsistent, &data.inputs, |s, ty| s.print_type(&ty))?;
16981698
word(&mut self.s, ")")?;
16991699

1700-
match data.output {
1701-
None => {}
1702-
Some(ref ty) => {
1703-
self.space_if_not_bol()?;
1704-
self.word_space("->")?;
1705-
self.print_type(&ty)?;
1706-
}
1700+
if let Some(ref ty) = data.output {
1701+
self.space_if_not_bol()?;
1702+
self.word_space("->")?;
1703+
self.print_type(&ty)?;
17071704
}
17081705
}
17091706
}

src/librustc/infer/region_inference/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -842,11 +842,8 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
842842
where F: FnMut(&RegionVarBindings<'a, 'gcx, 'tcx>, Region, Region)
843843
{
844844
let vars = TwoRegions { a: a, b: b };
845-
match self.combine_map(t).borrow().get(&vars) {
846-
Some(&c) => {
847-
return ReVar(c);
848-
}
849-
None => {}
845+
if let Some(&c) = self.combine_map(t).borrow().get(&vars) {
846+
return ReVar(c);
850847
}
851848
let c = self.new_region_var(MiscVariable(origin.span()));
852849
self.combine_map(t).borrow_mut().insert(vars, c);

src/librustc/lint/context.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,13 +1055,10 @@ impl<'a> ast_visit::Visitor for EarlyContext<'a> {
10551055
// Output any lints that were previously added to the session.
10561056
impl<'a, 'tcx> IdVisitingOperation for LateContext<'a, 'tcx> {
10571057
fn visit_id(&mut self, id: ast::NodeId) {
1058-
match self.sess().lints.borrow_mut().remove(&id) {
1059-
None => {}
1060-
Some(lints) => {
1061-
debug!("LateContext::visit_id: id={:?} lints={:?}", id, lints);
1062-
for (lint_id, span, msg) in lints {
1063-
self.span_lint(lint_id.lint, span, &msg[..])
1064-
}
1058+
if let Some(lints) = self.sess().lints.borrow_mut().remove(&id) {
1059+
debug!("LateContext::visit_id: id={:?} lints={:?}", id, lints);
1060+
for (lint_id, span, msg) in lints {
1061+
self.span_lint(lint_id.lint, span, &msg[..])
10651062
}
10661063
}
10671064
}

src/librustc/middle/dataflow.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,8 @@ fn build_nodeid_to_index(decl: Option<&hir::FnDecl>,
168168
// into cfg itself? i.e. introduce a fn-based flow-graph in
169169
// addition to the current block-based flow-graph, rather than
170170
// have to put traversals like this here?
171-
match decl {
172-
None => {}
173-
Some(decl) => add_entries_from_fn_decl(&mut index, decl, cfg.entry)
171+
if let Some(decl) = decl {
172+
add_entries_from_fn_decl(&mut index, decl, cfg.entry);
174173
}
175174

176175
cfg.graph.each_node(|node_idx, node| {

src/librustc/middle/dependency_format.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,8 @@ fn calculate_type(sess: &session::Session,
105105
// If the global prefer_dynamic switch is turned off, first attempt
106106
// static linkage (this can fail).
107107
config::CrateTypeExecutable if !sess.opts.cg.prefer_dynamic => {
108-
match attempt_static(sess) {
109-
Some(v) => return v,
110-
None => {}
108+
if let Some(v) = attempt_static(sess) {
109+
return v;
111110
}
112111
}
113112

@@ -119,9 +118,8 @@ fn calculate_type(sess: &session::Session,
119118
// to be found, we generate some nice pretty errors.
120119
config::CrateTypeStaticlib |
121120
config::CrateTypeCdylib => {
122-
match attempt_static(sess) {
123-
Some(v) => return v,
124-
None => {}
121+
if let Some(v) = attempt_static(sess) {
122+
return v;
125123
}
126124
for cnum in sess.cstore.crates() {
127125
let src = sess.cstore.used_crate_source(cnum);
@@ -136,9 +134,8 @@ fn calculate_type(sess: &session::Session,
136134
// to try to eagerly statically link all dependencies. This is normally
137135
// done for end-product dylibs, not intermediate products.
138136
config::CrateTypeDylib if !sess.opts.cg.prefer_dynamic => {
139-
match attempt_static(sess) {
140-
Some(v) => return v,
141-
None => {}
137+
if let Some(v) = attempt_static(sess) {
138+
return v;
142139
}
143140
}
144141

src/librustc/middle/expr_use_visitor.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -735,26 +735,23 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
735735

736736
for i in 0..autoderefs {
737737
let deref_id = ty::MethodCall::autoderef(expr.id, i as u32);
738-
match self.mc.infcx.node_method_ty(deref_id) {
739-
None => {}
740-
Some(method_ty) => {
741-
let cmt = return_if_err!(self.mc.cat_expr_autoderefd(expr, i));
742-
743-
// the method call infrastructure should have
744-
// replaced all late-bound regions with variables:
745-
let self_ty = method_ty.fn_sig().input(0);
746-
let self_ty = self.tcx().no_late_bound_regions(&self_ty).unwrap();
747-
748-
let (m, r) = match self_ty.sty {
749-
ty::TyRef(r, ref m) => (m.mutbl, r),
750-
_ => span_bug!(expr.span,
751-
"bad overloaded deref type {:?}",
752-
method_ty)
753-
};
754-
let bk = ty::BorrowKind::from_mutbl(m);
755-
self.delegate.borrow(expr.id, expr.span, cmt,
756-
*r, bk, AutoRef);
757-
}
738+
if let Some(method_ty) = self.mc.infcx.node_method_ty(deref_id) {
739+
let cmt = return_if_err!(self.mc.cat_expr_autoderefd(expr, i));
740+
741+
// the method call infrastructure should have
742+
// replaced all late-bound regions with variables:
743+
let self_ty = method_ty.fn_sig().input(0);
744+
let self_ty = self.tcx().no_late_bound_regions(&self_ty).unwrap();
745+
746+
let (m, r) = match self_ty.sty {
747+
ty::TyRef(r, ref m) => (m.mutbl, r),
748+
_ => span_bug!(expr.span,
749+
"bad overloaded deref type {:?}",
750+
method_ty)
751+
};
752+
let bk = ty::BorrowKind::from_mutbl(m);
753+
self.delegate.borrow(expr.id, expr.span, cmt,
754+
*r, bk, AutoRef);
758755
}
759756
}
760757
}

src/librustc/middle/liveness.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
598598
fn arm_pats_bindings<F>(&mut self, pat: Option<&hir::Pat>, f: F) where
599599
F: FnMut(&mut Liveness<'a, 'tcx>, LiveNode, Variable, Span, NodeId),
600600
{
601-
match pat {
602-
Some(pat) => {
603-
self.pat_bindings(pat, f);
604-
}
605-
None => {}
601+
if let Some(pat) = pat {
602+
self.pat_bindings(pat, f);
606603
}
607604
}
608605

src/librustc/middle/resolve_lifetime.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for LifetimeContext<'a, 'tcx> {
284284
fn visit_generics(&mut self, generics: &hir::Generics) {
285285
for ty_param in generics.ty_params.iter() {
286286
walk_list!(self, visit_ty_param_bound, &ty_param.bounds);
287-
match ty_param.default {
288-
Some(ref ty) => self.visit_ty(&ty),
289-
None => {}
287+
if let Some(ref ty) = ty_param.default {
288+
self.visit_ty(&ty);
290289
}
291290
}
292291
for predicate in &generics.where_clause.predicates {

0 commit comments

Comments
 (0)