Skip to content

Commit f6cb227

Browse files
committed
rustc_borrowck: Convert suggest_ampmut() 4-tuple to struct for readability
1 parent 2d2c6f2 commit f6cb227

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+46-13
Original file line numberDiff line numberDiff line change
@@ -1100,12 +1100,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11001100
}
11011101
let decl_span = local_decl.source_info.span;
11021102

1103-
let label = match *local_decl.local_info() {
1103+
let amp_mut_sugg = match *local_decl.local_info() {
11041104
LocalInfo::User(mir::BindingForm::ImplicitSelf(_)) => {
11051105
let suggestion = suggest_ampmut_self(self.infcx.tcx, decl_span);
11061106
let additional =
11071107
local_trait.map(|span| (span, suggest_ampmut_self(self.infcx.tcx, span)));
1108-
Some((true, decl_span, suggestion, additional))
1108+
Some(AmpMutSugg { has_sugg: true, span: decl_span, suggestion, additional })
11091109
}
11101110

11111111
LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {
@@ -1165,7 +1165,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11651165
..
11661166
})) => {
11671167
let sugg = suggest_ampmut_self(self.infcx.tcx, decl_span);
1168-
Some((true, decl_span, sugg, None))
1168+
Some(AmpMutSugg {
1169+
has_sugg: true,
1170+
span: decl_span,
1171+
suggestion: sugg,
1172+
additional: None,
1173+
})
11691174
}
11701175
// explicit self (eg `self: &'a Self`)
11711176
_ => suggest_ampmut(
@@ -1186,15 +1191,24 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11861191
..
11871192
})) => {
11881193
let pattern_span: Span = local_decl.source_info.span;
1189-
suggest_ref_mut(self.infcx.tcx, pattern_span)
1190-
.map(|span| (true, span, "mut ".to_owned(), None))
1194+
suggest_ref_mut(self.infcx.tcx, pattern_span).map(|span| AmpMutSugg {
1195+
has_sugg: true,
1196+
span,
1197+
suggestion: "mut ".to_owned(),
1198+
additional: None,
1199+
})
11911200
}
11921201

11931202
_ => unreachable!(),
11941203
};
11951204

1196-
match label {
1197-
Some((true, err_help_span, suggested_code, additional)) => {
1205+
match amp_mut_sugg {
1206+
Some(AmpMutSugg {
1207+
has_sugg: true,
1208+
span: err_help_span,
1209+
suggestion: suggested_code,
1210+
additional,
1211+
}) => {
11981212
let mut sugg = vec![(err_help_span, suggested_code)];
11991213
if let Some(s) = additional {
12001214
sugg.push(s);
@@ -1216,7 +1230,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
12161230
);
12171231
}
12181232
}
1219-
Some((false, err_label_span, message, _)) => {
1233+
Some(AmpMutSugg {
1234+
has_sugg: false, span: err_label_span, suggestion: message, ..
1235+
}) => {
12201236
let def_id = self.body.source.def_id();
12211237
let hir_id = if let Some(local_def_id) = def_id.as_local()
12221238
&& let Some(body) = self.infcx.tcx.hir().maybe_body_owned_by(local_def_id)
@@ -1421,6 +1437,13 @@ fn suggest_ampmut_self<'tcx>(tcx: TyCtxt<'tcx>, span: Span) -> String {
14211437
}
14221438
}
14231439

1440+
struct AmpMutSugg {
1441+
has_sugg: bool,
1442+
span: Span,
1443+
suggestion: String,
1444+
additional: Option<(Span, String)>,
1445+
}
1446+
14241447
// When we want to suggest a user change a local variable to be a `&mut`, there
14251448
// are three potential "obvious" things to highlight:
14261449
//
@@ -1442,7 +1465,7 @@ fn suggest_ampmut<'tcx>(
14421465
decl_span: Span,
14431466
opt_assignment_rhs_span: Option<Span>,
14441467
opt_ty_info: Option<Span>,
1445-
) -> Option<(bool, Span, String, Option<(Span, String)>)> {
1468+
) -> Option<AmpMutSugg> {
14461469
// if there is a RHS and it starts with a `&` from it, then check if it is
14471470
// mutable, and if not, put suggest putting `mut ` to make it mutable.
14481471
// we don't have to worry about lifetime annotations here because they are
@@ -1483,7 +1506,12 @@ fn suggest_ampmut<'tcx>(
14831506

14841507
// FIXME(Ezrashaw): returning is bad because we still might want to
14851508
// update the annotated type, see #106857.
1486-
return Some((true, span, "mut ".to_owned(), None));
1509+
return Some(AmpMutSugg {
1510+
has_sugg: true,
1511+
span,
1512+
suggestion: "mut ".to_owned(),
1513+
additional: None,
1514+
});
14871515
}
14881516
}
14891517

@@ -1508,18 +1536,23 @@ fn suggest_ampmut<'tcx>(
15081536
&& let Some(ws_pos) = src.find(char::is_whitespace)
15091537
{
15101538
let span = span.with_lo(span.lo() + BytePos(ws_pos as u32)).shrink_to_lo();
1511-
Some((true, span, " mut".to_owned(), None))
1539+
Some(AmpMutSugg { has_sugg: true, span, suggestion: " mut".to_owned(), additional: None })
15121540
// if there is already a binding, we modify it to be `mut`
15131541
} else if binding_exists {
15141542
// shrink the span to just after the `&` in `&variable`
15151543
let span = span.with_lo(span.lo() + BytePos(1)).shrink_to_lo();
1516-
Some((true, span, "mut ".to_owned(), None))
1544+
Some(AmpMutSugg { has_sugg: true, span, suggestion: "mut ".to_owned(), additional: None })
15171545
} else {
15181546
// otherwise, suggest that the user annotates the binding; we provide the
15191547
// type of the local.
15201548
let ty = decl_ty.builtin_deref(true).unwrap();
15211549

1522-
Some((false, span, format!("{}mut {}", if decl_ty.is_ref() { "&" } else { "*" }, ty), None))
1550+
Some(AmpMutSugg {
1551+
has_sugg: false,
1552+
span,
1553+
suggestion: format!("{}mut {}", if decl_ty.is_ref() { "&" } else { "*" }, ty),
1554+
additional: None,
1555+
})
15231556
}
15241557
}
15251558

0 commit comments

Comments
 (0)