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

+2-5
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

+2-3
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

+4-7
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

+2-5
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

+4-7
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

+2-3
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

+6-9
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

+17-20
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

+2-5
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

+2-3
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 {

src/librustc/middle/weak_lang_items.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,8 @@ impl<'a> Context<'a> {
123123

124124
impl<'a, 'v> Visitor<'v> for Context<'a> {
125125
fn visit_foreign_item(&mut self, i: &hir::ForeignItem) {
126-
match lang_items::extract(&i.attrs) {
127-
None => {}
128-
Some(lang_item) => self.register(&lang_item, i.span),
126+
if let Some(lang_item) = lang_items::extract(&i.attrs) {
127+
self.register(&lang_item, i.span);
129128
}
130129
intravisit::walk_foreign_item(self, i)
131130
}

src/librustc/session/mod.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,12 @@ impl Session {
250250
msg: String) {
251251
let lint_id = lint::LintId::of(lint);
252252
let mut lints = self.lints.borrow_mut();
253-
match lints.get_mut(&id) {
254-
Some(arr) => {
255-
let tuple = (lint_id, sp, msg);
256-
if !arr.contains(&tuple) {
257-
arr.push(tuple);
258-
}
259-
return;
253+
if let Some(arr) = lints.get_mut(&id) {
254+
let tuple = (lint_id, sp, msg);
255+
if !arr.contains(&tuple) {
256+
arr.push(tuple);
260257
}
261-
None => {}
258+
return;
262259
}
263260
lints.insert(id, vec!((lint_id, sp, msg)));
264261
}

src/librustc/ty/contents.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,12 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
168168
// which is incorrect. This value was computed based on the crutch
169169
// value for the type contents of list. The correct value is
170170
// TC::OwnsOwned. This manifested as issue #4821.
171-
match cache.get(&ty) {
172-
Some(tc) => { return *tc; }
173-
None => {}
171+
if let Some(tc) = cache.get(&ty) {
172+
return *tc;
174173
}
175-
match tcx.tc_cache.borrow().get(&ty) { // Must check both caches!
176-
Some(tc) => { return *tc; }
177-
None => {}
174+
// Must check both caches!
175+
if let Some(tc) = tcx.tc_cache.borrow().get(&ty) {
176+
return *tc;
178177
}
179178
cache.insert(ty, TC::None);
180179

src/librustc/ty/fold.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
521521
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> { self.0 }
522522

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

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

src/librustc/ty/util.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -712,16 +712,13 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
712712
// struct Foo;
713713
// struct Bar<T> { x: Bar<Foo> }
714714

715-
match iter.next() {
716-
Some(&seen_type) => {
717-
if same_struct_or_enum(seen_type, def) {
718-
debug!("SelfRecursive: {:?} contains {:?}",
719-
seen_type,
720-
ty);
721-
return Representability::SelfRecursive;
722-
}
715+
if let Some(&seen_type) = iter.next() {
716+
if same_struct_or_enum(seen_type, def) {
717+
debug!("SelfRecursive: {:?} contains {:?}",
718+
seen_type,
719+
ty);
720+
return Representability::SelfRecursive;
723721
}
724-
None => {}
725722
}
726723

727724
// We also need to know whether the first item contains other types

src/librustc_borrowck/borrowck/move_data.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,8 @@ impl<'a, 'tcx> MoveData<'tcx> {
274274
/// `lp` and any of its base paths that do not yet have an index.
275275
pub fn move_path(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
276276
lp: Rc<LoanPath<'tcx>>) -> MovePathIndex {
277-
match self.path_map.borrow().get(&lp) {
278-
Some(&index) => {
279-
return index;
280-
}
281-
None => {}
277+
if let Some(&index) = self.path_map.borrow().get(&lp) {
278+
return index;
282279
}
283280

284281
let index = match lp.kind {

src/librustc_const_eval/check_match.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,8 @@ fn check_expr(cx: &mut MatchCheckCtxt, ex: &hir::Expr) {
176176

177177
// Second, if there is a guard on each arm, make sure it isn't
178178
// assigning or borrowing anything mutably.
179-
match arm.guard {
180-
Some(ref guard) => check_for_mutation_in_guard(cx, &guard),
181-
None => {}
179+
if let Some(ref guard) = arm.guard {
180+
check_for_mutation_in_guard(cx, &guard);
182181
}
183182
}
184183

src/librustc_lint/unused.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,9 @@ impl LateLintPass for UnusedResults {
150150
if attr.check_name("must_use") {
151151
let mut msg = "unused result which must be used".to_string();
152152
// check for #[must_use="..."]
153-
match attr.value_str() {
154-
None => {}
155-
Some(s) => {
156-
msg.push_str(": ");
157-
msg.push_str(&s);
158-
}
153+
if let Some(s) = attr.value_str() {
154+
msg.push_str(": ");
155+
msg.push_str(&s);
159156
}
160157
cx.span_lint(UNUSED_MUST_USE, sp, &msg);
161158
return true;

src/librustc_llvm/build.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,17 @@ fn main() {
2424
let llvm_config = env::var_os("LLVM_CONFIG")
2525
.map(PathBuf::from)
2626
.unwrap_or_else(|| {
27-
match env::var_os("CARGO_TARGET_DIR").map(PathBuf::from) {
28-
Some(dir) => {
29-
let to_test = dir.parent()
30-
.unwrap()
31-
.parent()
32-
.unwrap()
33-
.join(&target)
34-
.join("llvm/bin/llvm-config");
35-
if Command::new(&to_test).output().is_ok() {
36-
return to_test;
37-
}
27+
if let Some(dir) = env::var_os("CARGO_TARGET_DIR")
28+
.map(PathBuf::from) {
29+
let to_test = dir.parent()
30+
.unwrap()
31+
.parent()
32+
.unwrap()
33+
.join(&target)
34+
.join("llvm/bin/llvm-config");
35+
if Command::new(&to_test).output().is_ok() {
36+
return to_test;
3837
}
39-
None => {}
4038
}
4139
PathBuf::from("llvm-config")
4240
});

src/librustc_metadata/decoder.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -682,15 +682,12 @@ fn each_child_of_item_or_crate<F, G>(intr: Rc<IdentInterner>,
682682
};
683683

684684
// Get the item.
685-
match crate_data.get_item(child_def_id.index) {
686-
None => {}
687-
Some(child_item_doc) => {
688-
// Hand off the item to the callback.
689-
let child_name = item_name(&intr, child_item_doc);
690-
let def_like = item_to_def_like(crate_data, child_item_doc, child_def_id);
691-
let visibility = item_visibility(child_item_doc);
692-
callback(def_like, child_name, visibility);
693-
}
685+
if let Some(child_item_doc) = crate_data.get_item(child_def_id.index) {
686+
// Hand off the item to the callback.
687+
let child_name = item_name(&intr, child_item_doc);
688+
let def_like = item_to_def_like(crate_data, child_item_doc, child_def_id);
689+
let visibility = item_visibility(child_item_doc);
690+
callback(def_like, child_name, visibility);
694691
}
695692
}
696693

0 commit comments

Comments
 (0)