Skip to content

Commit f1694ea

Browse files
committed
Auto merge of #54533 - ljedrz:cleanup_librustc_typeck_check, r=davidtwco
A few cleanups and minor improvements to typeck/check - turn a `loop` into a `while let` - turn a `push_back` loop into an `extend` - turn a few `push` loops into collected iterators - prefer `vec![x; n]` to `(0..n).map(|_| x).collect()` - combine two loops doing the same thing on 2 data sets using `chain` - use `unwrap_or` where applicable and readable - add a `potentially_plural_count` helper function to simplify several `format!()` calls - prefer `to_owned` to `to_string` for string literals - change `match` to `if let` where only one branch matters - a few other minor improvements - whitespace fixes
2 parents c9865b1 + 3527276 commit f1694ea

17 files changed

+154
-285
lines changed

src/librustc_typeck/check/_match.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
8181
//
8282
// See the examples in `run-pass/match-defbm*.rs`.
8383
let mut pat_adjustments = vec![];
84-
expected = loop {
84+
while let ty::Ref(_, inner_ty, inner_mutability) = exp_ty.sty {
8585
debug!("inspecting {:?} with type {:?}", exp_ty, exp_ty.sty);
86-
match exp_ty.sty {
87-
ty::Ref(_, inner_ty, inner_mutability) => {
86+
8887
debug!("current discriminant is Ref, inserting implicit deref");
8988
// Preserve the reference type. We'll need it later during HAIR lowering.
9089
pat_adjustments.push(exp_ty);
@@ -106,10 +105,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
106105
ty::BindByReference(hir::Mutability::MutMutable) =>
107106
ty::BindByReference(inner_mutability),
108107
};
109-
},
110-
_ => break exp_ty,
111108
}
112-
};
109+
expected = exp_ty;
110+
113111
if pat_adjustments.len() > 0 {
114112
debug!("default binding mode is now {:?}", def_bm);
115113
self.inh.tables.borrow_mut()

src/librustc_typeck/check/callee.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
166166
None => continue,
167167
};
168168

169-
match self.lookup_method_in_trait(call_expr.span,
169+
if let Some(ok) = self.lookup_method_in_trait(call_expr.span,
170170
method_name,
171171
trait_def_id,
172172
adjusted_ty,
173173
None) {
174-
None => continue,
175-
Some(ok) => {
176174
let method = self.register_infer_ok_obligations(ok);
177175
let mut autoref = None;
178176
if borrow {
@@ -195,7 +193,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
195193
return Some((autoref, method));
196194
}
197195
}
198-
}
199196

200197
None
201198
}

src/librustc_typeck/check/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
445445
self.expr_ty,
446446
fcx.tcx.mk_fn_ptr(f),
447447
AllowTwoPhase::No);
448-
if !res.is_ok() {
448+
if res.is_err() {
449449
return Err(CastError::NonScalar);
450450
}
451451
(FnPtr, t_cast)

src/librustc_typeck/check/closure.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,20 +231,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
231231
obligation.predicate
232232
);
233233

234-
match obligation.predicate {
234+
if let ty::Predicate::Projection(ref proj_predicate) = obligation.predicate {
235235
// Given a Projection predicate, we can potentially infer
236236
// the complete signature.
237-
ty::Predicate::Projection(ref proj_predicate) => {
238237
let trait_ref = proj_predicate.to_poly_trait_ref(self.tcx);
239238
self.self_type_matches_expected_vid(trait_ref, expected_vid)
240239
.and_then(|_| {
241240
self.deduce_sig_from_projection(
242241
Some(obligation.cause.span),
243-
proj_predicate,
242+
proj_predicate
244243
)
245244
})
246-
}
247-
_ => None,
245+
} else {
246+
None
248247
}
249248
})
250249
.next();
@@ -318,9 +317,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
318317

319318
let input_tys = match arg_param_ty.sty {
320319
ty::Tuple(tys) => tys.into_iter(),
321-
_ => {
322-
return None;
323-
}
320+
_ => return None
324321
};
325322

326323
let ret_param_ty = projection.skip_binder().ty;
@@ -638,12 +635,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
638635
self.tcx.types.err
639636
});
640637

641-
match decl.output {
642-
hir::Return(ref output) => {
638+
if let hir::Return(ref output) = decl.output {
643639
astconv.ast_ty_to_ty(&output);
644640
}
645-
hir::DefaultReturn(_) => {}
646-
}
647641

648642
let result = ty::Binder::bind(self.tcx.mk_fn_sig(
649643
supplied_arguments,

src/librustc_typeck/check/coercion.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
144144
fn unify(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {
145145
self.commit_if_ok(|_| {
146146
if self.use_lub {
147-
self.at(&self.cause, self.fcx.param_env)
148-
.lub(b, a)
147+
self.at(&self.cause, self.fcx.param_env).lub(b, a)
149148
} else {
150149
self.at(&self.cause, self.fcx.param_env)
151150
.sup(b, a)
@@ -256,8 +255,8 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
256255
b: Ty<'tcx>,
257256
r_b: ty::Region<'tcx>,
258257
mt_b: TypeAndMut<'tcx>)
259-
-> CoerceResult<'tcx> {
260-
258+
-> CoerceResult<'tcx>
259+
{
261260
debug!("coerce_borrowed_pointer(a={:?}, b={:?})", a, b);
262261

263262
// If we have a parameter of type `&M T_a` and the value
@@ -591,9 +590,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
591590
}
592591

593592
Ok(Some(vtable)) => {
594-
for obligation in vtable.nested_obligations() {
595-
queue.push_back(obligation);
596-
}
593+
queue.extend(vtable.nested_obligations())
597594
}
598595
}
599596
}
@@ -620,13 +617,12 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
620617
G: FnOnce(Ty<'tcx>) -> Vec<Adjustment<'tcx>>
621618
{
622619
if let ty::FnPtr(fn_ty_b) = b.sty {
623-
match (fn_ty_a.unsafety(), fn_ty_b.unsafety()) {
624-
(hir::Unsafety::Normal, hir::Unsafety::Unsafe) => {
620+
if let (hir::Unsafety::Normal, hir::Unsafety::Unsafe)
621+
= (fn_ty_a.unsafety(), fn_ty_b.unsafety())
622+
{
625623
let unsafe_a = self.tcx.safe_to_unsafe_fn_ty(fn_ty_a);
626624
return self.unify_and(unsafe_a, b, to_unsafe);
627625
}
628-
_ => {}
629-
}
630626
}
631627
self.unify_and(a, b, normal)
632628
}
@@ -653,7 +649,6 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
653649
-> CoerceResult<'tcx> {
654650
//! Attempts to coerce from the type of a Rust function item
655651
//! into a closure or a `proc`.
656-
//!
657652
658653
let b = self.shallow_resolve(b);
659654
debug!("coerce_from_fn_item(a={:?}, b={:?})", a, b);
@@ -724,9 +719,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
724719
let (is_ref, mt_a) = match a.sty {
725720
ty::Ref(_, ty, mutbl) => (true, ty::TypeAndMut { ty, mutbl }),
726721
ty::RawPtr(mt) => (false, mt),
727-
_ => {
728-
return self.unify_and(a, b, identity);
729-
}
722+
_ => return self.unify_and(a, b, identity)
730723
};
731724

732725
// Check that the types which they point at are compatible.
@@ -896,10 +889,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
896889
};
897890

898891
if !noop {
899-
return self.commit_if_ok(|_| {
892+
return self.commit_if_ok(|_|
900893
self.at(cause, self.param_env)
901894
.lub(prev_ty, new_ty)
902-
}).map(|ok| self.register_infer_ok_obligations(ok));
895+
).map(|ok| self.register_infer_ok_obligations(ok));
903896
}
904897
}
905898

@@ -909,10 +902,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
909902
if let Some(e) = first_error {
910903
Err(e)
911904
} else {
912-
self.commit_if_ok(|_| {
905+
self.commit_if_ok(|_|
913906
self.at(cause, self.param_env)
914907
.lub(prev_ty, new_ty)
915-
}).map(|ok| self.register_infer_ok_obligations(ok))
908+
).map(|ok| self.register_infer_ok_obligations(ok))
916909
}
917910
}
918911
Ok(ok) => {

0 commit comments

Comments
 (0)