Skip to content

Commit 9fd6fad

Browse files
committed
rustc/infer: convert single-branch matches to if-let
1 parent 89cb411 commit 9fd6fad

File tree

7 files changed

+88
-104
lines changed

7 files changed

+88
-104
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
455455
TypeError::Sorts(ref exp_found) => {
456456
// if they are both "path types", there's a chance of ambiguity
457457
// due to different versions of the same crate
458-
match (&exp_found.expected.sty, &exp_found.found.sty) {
459-
(&ty::Adt(exp_adt, _), &ty::Adt(found_adt, _)) => {
460-
report_path_match(err, exp_adt.did, found_adt.did);
461-
}
462-
_ => (),
458+
if let (&ty::Adt(exp_adt, _), &ty::Adt(found_adt, _))
459+
= (&exp_found.expected.sty, &exp_found.found.sty)
460+
{
461+
report_path_match(err, exp_adt.did, found_adt.did);
463462
}
464463
}
465464
TypeError::Traits(ref exp_found) => {

src/librustc/infer/error_reporting/nice_region_error/outlives_closure.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,17 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
5858
&RegionKind::ReFree(ref free_region)) = (&sub_origin, sup_region) {
5959
let hir = &self.tcx.hir;
6060
if let Some(node_id) = hir.as_local_node_id(free_region.scope) {
61-
match hir.get(node_id) {
62-
Node::Expr(Expr {
63-
node: Closure(_, _, _, closure_span, None),
64-
..
65-
}) => {
66-
let sup_sp = sup_origin.span();
67-
let origin_sp = origin.span();
68-
let mut err = self.tcx.sess.struct_span_err(
69-
sup_sp,
70-
"borrowed data cannot be stored outside of its closure");
71-
err.span_label(sup_sp, "cannot be stored outside of its closure");
72-
if origin_sp == sup_sp || origin_sp.contains(sup_sp) {
61+
if let Node::Expr(Expr {
62+
node: Closure(_, _, _, closure_span, None),
63+
..
64+
}) = hir.get(node_id) {
65+
let sup_sp = sup_origin.span();
66+
let origin_sp = origin.span();
67+
let mut err = self.tcx.sess.struct_span_err(
68+
sup_sp,
69+
"borrowed data cannot be stored outside of its closure");
70+
err.span_label(sup_sp, "cannot be stored outside of its closure");
71+
if origin_sp == sup_sp || origin_sp.contains(sup_sp) {
7372
// // sup_sp == origin.span():
7473
//
7574
// let mut x = None;
@@ -87,11 +86,11 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
8786
// ------------ ... because it cannot outlive this closure
8887
// f = Some(x);
8988
// ^ cannot be stored outside of its closure
90-
err.span_label(*external_span,
91-
"borrowed data cannot be stored into here...");
92-
err.span_label(*closure_span,
93-
"...because it cannot outlive this closure");
94-
} else {
89+
err.span_label(*external_span,
90+
"borrowed data cannot be stored into here...");
91+
err.span_label(*closure_span,
92+
"...because it cannot outlive this closure");
93+
} else {
9594
// FIXME: the wording for this case could be much improved
9695
//
9796
// let mut lines_to_use: Vec<&CrateId> = Vec::new();
@@ -102,18 +101,16 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
102101
// ...so that variable is valid at time of its declaration
103102
// lines_to_use.push(installed_id);
104103
// ^^^^^^^^^^^^ cannot be stored outside of its closure
105-
err.span_label(origin_sp,
106-
"cannot infer an appropriate lifetime...");
107-
err.span_label(*external_span,
108-
"...so that variable is valid at time of its \
109-
declaration");
110-
err.span_label(*closure_span,
111-
"borrowed data cannot outlive this closure");
112-
}
113-
err.emit();
114-
return Some(ErrorReported);
104+
err.span_label(origin_sp,
105+
"cannot infer an appropriate lifetime...");
106+
err.span_label(*external_span,
107+
"...so that variable is valid at time of its \
108+
declaration");
109+
err.span_label(*closure_span,
110+
"borrowed data cannot outlive this closure");
115111
}
116-
_ => {}
112+
err.emit();
113+
return Some(ErrorReported);
117114
}
118115
}
119116
}

src/librustc/infer/error_reporting/nice_region_error/static_impl_trait.rs

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,64 +20,62 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
2020
/// Print the error message for lifetime errors when the return type is a static impl Trait.
2121
pub(super) fn try_report_static_impl_trait(&self) -> Option<ErrorReported> {
2222
if let Some(ref error) = self.error {
23-
match error.clone() {
24-
RegionResolutionError::SubSupConflict(
23+
if let RegionResolutionError::SubSupConflict(
2524
var_origin,
2625
sub_origin,
2726
sub_r,
2827
sup_origin,
2928
sup_r,
30-
) => {
31-
let anon_reg_sup = self.tcx.is_suitable_region(sup_r)?;
32-
if sub_r == &RegionKind::ReStatic &&
33-
self.tcx.return_type_impl_trait(anon_reg_sup.def_id).is_some()
34-
{
35-
let sp = var_origin.span();
36-
let return_sp = sub_origin.span();
37-
let mut err = self.tcx.sess.struct_span_err(
38-
sp,
39-
"cannot infer an appropriate lifetime",
29+
) = error.clone()
30+
{
31+
let anon_reg_sup = self.tcx.is_suitable_region(sup_r)?;
32+
if sub_r == &RegionKind::ReStatic &&
33+
self.tcx.return_type_impl_trait(anon_reg_sup.def_id).is_some()
34+
{
35+
let sp = var_origin.span();
36+
let return_sp = sub_origin.span();
37+
let mut err = self.tcx.sess.struct_span_err(
38+
sp,
39+
"cannot infer an appropriate lifetime",
40+
);
41+
err.span_label(
42+
return_sp,
43+
"this return type evaluates to the `'static` lifetime...",
44+
);
45+
err.span_label(
46+
sup_origin.span(),
47+
"...but this borrow...",
48+
);
49+
50+
let (lifetime, lt_sp_opt) = self.tcx.msg_span_from_free_region(sup_r);
51+
if let Some(lifetime_sp) = lt_sp_opt {
52+
err.span_note(
53+
lifetime_sp,
54+
&format!("...can't outlive {}", lifetime),
4055
);
41-
err.span_label(
56+
}
57+
58+
let lifetime_name = match sup_r {
59+
RegionKind::ReFree(FreeRegion {
60+
bound_region: BoundRegion::BrNamed(_, ref name), ..
61+
}) => name.to_string(),
62+
_ => "'_".to_owned(),
63+
};
64+
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(return_sp) {
65+
err.span_suggestion_with_applicability(
4266
return_sp,
43-
"this return type evaluates to the `'static` lifetime...",
67+
&format!(
68+
"you can add a constraint to the return type to make it last \
69+
less than `'static` and match {}",
70+
lifetime,
71+
),
72+
format!("{} + {}", snippet, lifetime_name),
73+
Applicability::Unspecified,
4474
);
45-
err.span_label(
46-
sup_origin.span(),
47-
"...but this borrow...",
48-
);
49-
50-
let (lifetime, lt_sp_opt) = self.tcx.msg_span_from_free_region(sup_r);
51-
if let Some(lifetime_sp) = lt_sp_opt {
52-
err.span_note(
53-
lifetime_sp,
54-
&format!("...can't outlive {}", lifetime),
55-
);
56-
}
57-
58-
let lifetime_name = match sup_r {
59-
RegionKind::ReFree(FreeRegion {
60-
bound_region: BoundRegion::BrNamed(_, ref name), ..
61-
}) => name.to_string(),
62-
_ => "'_".to_owned(),
63-
};
64-
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(return_sp) {
65-
err.span_suggestion_with_applicability(
66-
return_sp,
67-
&format!(
68-
"you can add a constraint to the return type to make it last \
69-
less than `'static` and match {}",
70-
lifetime,
71-
),
72-
format!("{} + {}", snippet, lifetime_name),
73-
Applicability::Unspecified,
74-
);
75-
}
76-
err.emit();
77-
return Some(ErrorReported);
7875
}
76+
err.emit();
77+
return Some(ErrorReported);
7978
}
80-
_ => {}
8179
}
8280
}
8381
None

src/librustc/infer/error_reporting/nice_region_error/util.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,13 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
119119
decl: &hir::FnDecl,
120120
) -> Option<Span> {
121121
let ret_ty = self.tcx.type_of(scope_def_id);
122-
match ret_ty.sty {
123-
ty::FnDef(_, _) => {
124-
let sig = ret_ty.fn_sig(self.tcx);
125-
let late_bound_regions = self.tcx
126-
.collect_referenced_late_bound_regions(&sig.output());
127-
if late_bound_regions.iter().any(|r| *r == br) {
128-
return Some(decl.output.span());
129-
}
122+
if let ty::FnDef(_, _) = ret_ty.sty {
123+
let sig = ret_ty.fn_sig(self.tcx);
124+
let late_bound_regions = self.tcx
125+
.collect_referenced_late_bound_regions(&sig.output());
126+
if late_bound_regions.iter().any(|r| *r == br) {
127+
return Some(decl.output.span());
130128
}
131-
_ => {}
132129
}
133130
None
134131
}

src/librustc/infer/lexical_region_resolve/graphviz.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,9 @@ pub fn maybe_print_constraints_for<'a, 'gcx, 'tcx>(
112112
}
113113
};
114114

115-
match dump_region_data_to(region_rels, &region_data.constraints, &output_path) {
116-
Ok(()) => {}
117-
Err(e) => {
118-
let msg = format!("io error dumping region constraints: {}", e);
119-
tcx.sess.err(&msg)
120-
}
115+
if let Err(e) = dump_region_data_to(region_rels, &region_data.constraints, &output_path) {
116+
let msg = format!("io error dumping region constraints: {}", e);
117+
tcx.sess.err(&msg)
121118
}
122119
}
123120

src/librustc/infer/region_constraints/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -661,11 +661,10 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
661661
debug!("RegionConstraintCollector: add_verify({:?})", verify);
662662

663663
// skip no-op cases known to be satisfied
664-
match verify.bound {
665-
VerifyBound::AllBounds(ref bs) if bs.len() == 0 => {
664+
if let VerifyBound::AllBounds(ref bs) = verify.bound {
665+
if bs.len() == 0 {
666666
return;
667667
}
668-
_ => {}
669668
}
670669

671670
let index = self.data.verifys.len();

src/librustc/infer/type_variable.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,8 @@ impl<'tcx> TypeVariableTable<'tcx> {
273273
pub fn rollback_to(&mut self, s: Snapshot<'tcx>) {
274274
debug!("rollback_to{:?}", {
275275
for action in self.values.actions_since_snapshot(&s.snapshot) {
276-
match *action {
277-
sv::UndoLog::NewElem(index) => {
278-
debug!("inference variable _#{}t popped", index)
279-
}
280-
_ => { }
276+
if let sv::UndoLog::NewElem(index) = *action {
277+
debug!("inference variable _#{}t popped", index)
281278
}
282279
}
283280
});

0 commit comments

Comments
 (0)