Skip to content

Commit ece6b97

Browse files
authored
Rollup merge of #95810 - TaKO8Ki:use-format-args-capture-and-remove-unnecessary-nesting-in-rustc-borrowck, r=davidtwco
Use `format-args-capture` and remove unnecessary nested blocks
2 parents af895b0 + 470b4fc commit ece6b97

File tree

5 files changed

+116
-133
lines changed

5 files changed

+116
-133
lines changed

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
157157
// Error with the match place
158158
LookupResult::Parent(_) => {
159159
for ge in &mut *grouped_errors {
160-
if let GroupedMoveError::MovesFromPlace { span, binds_to, .. } = ge {
161-
if match_span == *span {
162-
debug!("appending local({:?}) to list", bind_to);
163-
if !binds_to.is_empty() {
164-
binds_to.push(bind_to);
165-
}
166-
return;
160+
if let GroupedMoveError::MovesFromPlace { span, binds_to, .. } = ge
161+
&& match_span == *span
162+
{
163+
debug!("appending local({:?}) to list", bind_to);
164+
if !binds_to.is_empty() {
165+
binds_to.push(bind_to);
167166
}
167+
return;
168168
}
169169
}
170170
debug!("found a new move error location");
@@ -353,7 +353,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
353353
None => bug!("closure kind not inferred by borrowck"),
354354
};
355355
let capture_description =
356-
format!("captured variable in an `{}` closure", closure_kind);
356+
format!("captured variable in an `{closure_kind}` closure");
357357

358358
let upvar = &self.upvars[upvar_field.unwrap().index()];
359359
let upvar_hir_id = upvar.place.get_root_variable();
@@ -364,9 +364,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
364364

365365
let place_description =
366366
if self.is_upvar_field_projection(move_place.as_ref()).is_some() {
367-
format!("{}, a {}", place_name, capture_description)
367+
format!("{place_name}, a {capture_description}")
368368
} else {
369-
format!("{}, as `{}` is a {}", place_name, upvar_name, capture_description)
369+
format!("{place_name}, as `{upvar_name}` is a {capture_description}")
370370
};
371371

372372
debug!(
@@ -379,7 +379,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
379379
diag.span_label(upvar_span, "captured outer variable");
380380
diag.span_label(
381381
self.body.span,
382-
format!("captured by this `{}` closure", closure_kind),
382+
format!("captured by this `{closure_kind}` closure"),
383383
);
384384

385385
diag
@@ -390,7 +390,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
390390
{
391391
(Some(place_desc), Some(source_desc)) => self.cannot_move_out_of(
392392
span,
393-
&format!("`{}` which is behind a {}", place_desc, source_desc),
393+
&format!("`{place_desc}` which is behind a {source_desc}"),
394394
),
395395
(_, _) => self.cannot_move_out_of(
396396
span,
@@ -435,15 +435,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
435435
err.span_suggestion(
436436
span,
437437
"consider borrowing here",
438-
format!("&{}", snippet),
438+
format!("&{snippet}"),
439439
Applicability::Unspecified,
440440
);
441441
}
442442

443443
if binds_to.is_empty() {
444444
let place_ty = move_from.ty(self.body, self.infcx.tcx).ty;
445445
let place_desc = match self.describe_place(move_from.as_ref()) {
446-
Some(desc) => format!("`{}`", desc),
446+
Some(desc) => format!("`{desc}`"),
447447
None => "value".to_string(),
448448
};
449449

@@ -472,12 +472,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
472472
let span = use_spans.var_or_use();
473473
let place_ty = original_path.ty(self.body, self.infcx.tcx).ty;
474474
let place_desc = match self.describe_place(original_path.as_ref()) {
475-
Some(desc) => format!("`{}`", desc),
475+
Some(desc) => format!("`{desc}`"),
476476
None => "value".to_string(),
477477
};
478478
self.note_type_does_not_implement_copy(err, &place_desc, place_ty, Some(span), "");
479479

480-
use_spans.args_span_label(err, format!("move out of {} occurs here", place_desc));
480+
use_spans.args_span_label(err, format!("move out of {place_desc} occurs here"));
481481
}
482482
}
483483
}
@@ -511,7 +511,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
511511
for (span, to_remove, suggestion) in suggestions {
512512
err.span_suggestion(
513513
span,
514-
&format!("consider removing the `{}`", to_remove),
514+
&format!("consider removing the `{to_remove}`"),
515515
suggestion,
516516
Applicability::MachineApplicable,
517517
);

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+72-78
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
5555
reason = ", as it is not declared as mutable".to_string();
5656
} else {
5757
let name = self.local_names[local].expect("immutable unnamed local");
58-
reason = format!(", as `{}` is not declared as mutable", name);
58+
reason = format!(", as `{name}` is not declared as mutable");
5959
}
6060
}
6161

@@ -88,7 +88,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
8888
reason = ", as it is not declared as mutable".to_string();
8989
} else {
9090
let name = self.upvars[upvar_index.index()].place.to_string(self.infcx.tcx);
91-
reason = format!(", as `{}` is not declared as mutable", name);
91+
reason = format!(", as `{name}` is not declared as mutable");
9292
}
9393
}
9494
}
@@ -103,14 +103,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
103103
if self.body.local_decls[local].is_ref_to_static() =>
104104
{
105105
if access_place.projection.len() == 1 {
106-
item_msg = format!("immutable static item {}", access_place_desc);
106+
item_msg = format!("immutable static item {access_place_desc}");
107107
reason = String::new();
108108
} else {
109109
item_msg = access_place_desc;
110110
let local_info = &self.body.local_decls[local].local_info;
111111
if let Some(box LocalInfo::StaticRef { def_id, .. }) = *local_info {
112112
let static_name = &self.infcx.tcx.item_name(def_id);
113-
reason = format!(", as `{}` is an immutable static item", static_name);
113+
reason = format!(", as `{static_name}` is an immutable static item");
114114
} else {
115115
bug!("is_ref_to_static return true, but not ref to static?");
116116
}
@@ -148,15 +148,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
148148
let pointer_type = source.describe_for_immutable_place(self.infcx.tcx);
149149
opt_source = Some(source);
150150
if let Some(desc) = self.describe_place(access_place.as_ref()) {
151-
item_msg = format!("`{}`", desc);
151+
item_msg = format!("`{desc}`");
152152
reason = match error_access {
153-
AccessKind::Mutate => format!(", which is behind {}", pointer_type),
153+
AccessKind::Mutate => format!(", which is behind {pointer_type}"),
154154
AccessKind::MutableBorrow => {
155-
format!(", as it is behind {}", pointer_type)
155+
format!(", as it is behind {pointer_type}")
156156
}
157157
}
158158
} else {
159-
item_msg = format!("data in {}", pointer_type);
159+
item_msg = format!("data in {pointer_type}");
160160
reason = String::new();
161161
}
162162
}
@@ -362,29 +362,27 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
362362

363363
let upvar_hir_id = captured_place.get_root_variable();
364364

365-
if let Some(Node::Binding(pat)) = self.infcx.tcx.hir().find(upvar_hir_id) {
366-
if let hir::PatKind::Binding(
365+
if let Some(Node::Binding(pat)) = self.infcx.tcx.hir().find(upvar_hir_id)
366+
&& let hir::PatKind::Binding(
367367
hir::BindingAnnotation::Unannotated,
368368
_,
369369
upvar_ident,
370370
_,
371371
) = pat.kind
372-
{
373-
err.span_suggestion(
374-
upvar_ident.span,
375-
"consider changing this to be mutable",
376-
format!("mut {}", upvar_ident.name),
377-
Applicability::MachineApplicable,
378-
);
379-
}
372+
{
373+
err.span_suggestion(
374+
upvar_ident.span,
375+
"consider changing this to be mutable",
376+
format!("mut {}", upvar_ident.name),
377+
Applicability::MachineApplicable,
378+
);
380379
}
381380

382381
let tcx = self.infcx.tcx;
383382
if let ty::Ref(_, ty, Mutability::Mut) = the_place_err.ty(self.body, tcx).ty.kind()
383+
&& let ty::Closure(id, _) = *ty.kind()
384384
{
385-
if let ty::Closure(id, _) = *ty.kind() {
386-
self.show_mutating_upvar(tcx, id, the_place_err, &mut err);
387-
}
385+
self.show_mutating_upvar(tcx, id, the_place_err, &mut err);
388386
}
389387
}
390388

@@ -544,8 +542,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
544542
err.span_suggestion(
545543
err_help_span,
546544
&format!(
547-
"consider changing this to be a mutable {}",
548-
pointer_desc
545+
"consider changing this to be a mutable {pointer_desc}"
549546
),
550547
suggested_code,
551548
Applicability::MachineApplicable,
@@ -554,8 +551,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
554551
err.span_suggestion(
555552
x,
556553
&format!(
557-
"consider changing that to be a mutable {}",
558-
pointer_desc
554+
"consider changing that to be a mutable {pointer_desc}"
559555
),
560556
suggested_code,
561557
Applicability::MachineApplicable,
@@ -606,15 +602,13 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
606602
Some(BorrowedContentSource::OverloadedDeref(ty)) => {
607603
err.help(&format!(
608604
"trait `DerefMut` is required to modify through a dereference, \
609-
but it is not implemented for `{}`",
610-
ty,
605+
but it is not implemented for `{ty}`",
611606
));
612607
}
613608
Some(BorrowedContentSource::OverloadedIndex(ty)) => {
614609
err.help(&format!(
615610
"trait `IndexMut` is required to modify indexed content, \
616-
but it is not implemented for `{}`",
617-
ty,
611+
but it is not implemented for `{ty}`",
618612
));
619613
}
620614
_ => (),
@@ -724,18 +718,18 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
724718
ty::UpvarCapture::ByRef(
725719
ty::BorrowKind::MutBorrow | ty::BorrowKind::UniqueImmBorrow,
726720
) => {
727-
capture_reason = format!("mutable borrow of `{}`", upvar);
721+
capture_reason = format!("mutable borrow of `{upvar}`");
728722
}
729723
ty::UpvarCapture::ByValue => {
730-
capture_reason = format!("possible mutation of `{}`", upvar);
724+
capture_reason = format!("possible mutation of `{upvar}`");
731725
}
732-
_ => bug!("upvar `{}` borrowed, but not mutably", upvar),
726+
_ => bug!("upvar `{upvar}` borrowed, but not mutably"),
733727
}
734728
break;
735729
}
736730
}
737731
if capture_reason.is_empty() {
738-
bug!("upvar `{}` borrowed, but cannot find reason", upvar);
732+
bug!("upvar `{upvar}` borrowed, but cannot find reason");
739733
}
740734
capture_reason
741735
} else {
@@ -829,27 +823,27 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
829823
.as_str()
830824
.starts_with(&original_method_ident.name.to_string())
831825
})
832-
.map(|ident| format!("{}()", ident))
826+
.map(|ident| format!("{ident}()"))
833827
.peekable()
834828
});
835829

836-
if let Some(mut suggestions) = opt_suggestions {
837-
if suggestions.peek().is_some() {
838-
err.span_suggestions(
839-
*span,
840-
"use mutable method",
841-
suggestions,
842-
Applicability::MaybeIncorrect,
843-
);
844-
}
830+
if let Some(mut suggestions) = opt_suggestions
831+
&& suggestions.peek().is_some()
832+
{
833+
err.span_suggestions(
834+
*span,
835+
"use mutable method",
836+
suggestions,
837+
Applicability::MaybeIncorrect,
838+
);
845839
}
846840
}
847841
};
848842
}
849843

850844
/// Targeted error when encountering an `FnMut` closure where an `Fn` closure was expected.
851845
fn expected_fn_found_fn_mut_call(&self, err: &mut Diagnostic, sp: Span, act: &str) {
852-
err.span_label(sp, format!("cannot {}", act));
846+
err.span_label(sp, format!("cannot {act}"));
853847

854848
let hir = self.infcx.tcx.hir();
855849
let closure_id = self.mir_hir_id();
@@ -1011,35 +1005,35 @@ fn suggest_ampmut<'tcx>(
10111005
opt_assignment_rhs_span: Option<Span>,
10121006
opt_ty_info: Option<Span>,
10131007
) -> (Span, String) {
1014-
if let Some(assignment_rhs_span) = opt_assignment_rhs_span {
1015-
if let Ok(src) = tcx.sess.source_map().span_to_snippet(assignment_rhs_span) {
1016-
let is_mutbl = |ty: &str| -> bool {
1017-
if let Some(rest) = ty.strip_prefix("mut") {
1018-
match rest.chars().next() {
1019-
// e.g. `&mut x`
1020-
Some(c) if c.is_whitespace() => true,
1021-
// e.g. `&mut(x)`
1022-
Some('(') => true,
1023-
// e.g. `&mut{x}`
1024-
Some('{') => true,
1025-
// e.g. `&mutablevar`
1026-
_ => false,
1027-
}
1028-
} else {
1029-
false
1030-
}
1031-
};
1032-
if let (true, Some(ws_pos)) = (src.starts_with("&'"), src.find(char::is_whitespace)) {
1033-
let lt_name = &src[1..ws_pos];
1034-
let ty = src[ws_pos..].trim_start();
1035-
if !is_mutbl(ty) {
1036-
return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty));
1037-
}
1038-
} else if let Some(stripped) = src.strip_prefix('&') {
1039-
let stripped = stripped.trim_start();
1040-
if !is_mutbl(stripped) {
1041-
return (assignment_rhs_span, format!("&mut {}", stripped));
1008+
if let Some(assignment_rhs_span) = opt_assignment_rhs_span
1009+
&& let Ok(src) = tcx.sess.source_map().span_to_snippet(assignment_rhs_span)
1010+
{
1011+
let is_mutbl = |ty: &str| -> bool {
1012+
if let Some(rest) = ty.strip_prefix("mut") {
1013+
match rest.chars().next() {
1014+
// e.g. `&mut x`
1015+
Some(c) if c.is_whitespace() => true,
1016+
// e.g. `&mut(x)`
1017+
Some('(') => true,
1018+
// e.g. `&mut{x}`
1019+
Some('{') => true,
1020+
// e.g. `&mutablevar`
1021+
_ => false,
10421022
}
1023+
} else {
1024+
false
1025+
}
1026+
};
1027+
if let (true, Some(ws_pos)) = (src.starts_with("&'"), src.find(char::is_whitespace)) {
1028+
let lt_name = &src[1..ws_pos];
1029+
let ty = src[ws_pos..].trim_start();
1030+
if !is_mutbl(ty) {
1031+
return (assignment_rhs_span, format!("&{lt_name} mut {ty}"));
1032+
}
1033+
} else if let Some(stripped) = src.strip_prefix('&') {
1034+
let stripped = stripped.trim_start();
1035+
if !is_mutbl(stripped) {
1036+
return (assignment_rhs_span, format!("&mut {stripped}"));
10431037
}
10441038
}
10451039
}
@@ -1054,12 +1048,12 @@ fn suggest_ampmut<'tcx>(
10541048
None => local_decl.source_info.span,
10551049
};
10561050

1057-
if let Ok(src) = tcx.sess.source_map().span_to_snippet(highlight_span) {
1058-
if let (true, Some(ws_pos)) = (src.starts_with("&'"), src.find(char::is_whitespace)) {
1059-
let lt_name = &src[1..ws_pos];
1060-
let ty = &src[ws_pos..];
1061-
return (highlight_span, format!("&{} mut{}", lt_name, ty));
1062-
}
1051+
if let Ok(src) = tcx.sess.source_map().span_to_snippet(highlight_span)
1052+
&& let (true, Some(ws_pos)) = (src.starts_with("&'"), src.find(char::is_whitespace))
1053+
{
1054+
let lt_name = &src[1..ws_pos];
1055+
let ty = &src[ws_pos..];
1056+
return (highlight_span, format!("&{} mut{}", lt_name, ty));
10631057
}
10641058

10651059
let ty_mut = local_decl.ty.builtin_deref(true).unwrap();

0 commit comments

Comments
 (0)