diff --git a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs index a58c788eb57..e7fd89c140f 100644 --- a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs @@ -157,14 +157,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { // Error with the match place LookupResult::Parent(_) => { for ge in &mut *grouped_errors { - if let GroupedMoveError::MovesFromPlace { span, binds_to, .. } = ge { - if match_span == *span { - debug!("appending local({:?}) to list", bind_to); - if !binds_to.is_empty() { - binds_to.push(bind_to); - } - return; + if let GroupedMoveError::MovesFromPlace { span, binds_to, .. } = ge + && match_span == *span + { + debug!("appending local({:?}) to list", bind_to); + if !binds_to.is_empty() { + binds_to.push(bind_to); } + return; } } debug!("found a new move error location"); @@ -353,7 +353,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { None => bug!("closure kind not inferred by borrowck"), }; let capture_description = - format!("captured variable in an `{}` closure", closure_kind); + format!("captured variable in an `{closure_kind}` closure"); let upvar = &self.upvars[upvar_field.unwrap().index()]; let upvar_hir_id = upvar.place.get_root_variable(); @@ -364,9 +364,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { let place_description = if self.is_upvar_field_projection(move_place.as_ref()).is_some() { - format!("{}, a {}", place_name, capture_description) + format!("{place_name}, a {capture_description}") } else { - format!("{}, as `{}` is a {}", place_name, upvar_name, capture_description) + format!("{place_name}, as `{upvar_name}` is a {capture_description}") }; debug!( @@ -379,7 +379,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { diag.span_label(upvar_span, "captured outer variable"); diag.span_label( self.body.span, - format!("captured by this `{}` closure", closure_kind), + format!("captured by this `{closure_kind}` closure"), ); diag @@ -390,7 +390,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { { (Some(place_desc), Some(source_desc)) => self.cannot_move_out_of( span, - &format!("`{}` which is behind a {}", place_desc, source_desc), + &format!("`{place_desc}` which is behind a {source_desc}"), ), (_, _) => self.cannot_move_out_of( span, @@ -435,7 +435,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { err.span_suggestion( span, "consider borrowing here", - format!("&{}", snippet), + format!("&{snippet}"), Applicability::Unspecified, ); } @@ -443,7 +443,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { if binds_to.is_empty() { let place_ty = move_from.ty(self.body, self.infcx.tcx).ty; let place_desc = match self.describe_place(move_from.as_ref()) { - Some(desc) => format!("`{}`", desc), + Some(desc) => format!("`{desc}`"), None => "value".to_string(), }; @@ -472,12 +472,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { let span = use_spans.var_or_use(); let place_ty = original_path.ty(self.body, self.infcx.tcx).ty; let place_desc = match self.describe_place(original_path.as_ref()) { - Some(desc) => format!("`{}`", desc), + Some(desc) => format!("`{desc}`"), None => "value".to_string(), }; self.note_type_does_not_implement_copy(err, &place_desc, place_ty, Some(span), ""); - use_spans.args_span_label(err, format!("move out of {} occurs here", place_desc)); + use_spans.args_span_label(err, format!("move out of {place_desc} occurs here")); } } } @@ -511,7 +511,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { for (span, to_remove, suggestion) in suggestions { err.span_suggestion( span, - &format!("consider removing the `{}`", to_remove), + &format!("consider removing the `{to_remove}`"), suggestion, Applicability::MachineApplicable, ); diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index 6e551e95cbb..8b12db071b6 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -55,7 +55,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { reason = ", as it is not declared as mutable".to_string(); } else { let name = self.local_names[local].expect("immutable unnamed local"); - reason = format!(", as `{}` is not declared as mutable", name); + reason = format!(", as `{name}` is not declared as mutable"); } } @@ -88,7 +88,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { reason = ", as it is not declared as mutable".to_string(); } else { let name = self.upvars[upvar_index.index()].place.to_string(self.infcx.tcx); - reason = format!(", as `{}` is not declared as mutable", name); + reason = format!(", as `{name}` is not declared as mutable"); } } } @@ -103,14 +103,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { if self.body.local_decls[local].is_ref_to_static() => { if access_place.projection.len() == 1 { - item_msg = format!("immutable static item {}", access_place_desc); + item_msg = format!("immutable static item {access_place_desc}"); reason = String::new(); } else { item_msg = access_place_desc; let local_info = &self.body.local_decls[local].local_info; if let Some(box LocalInfo::StaticRef { def_id, .. }) = *local_info { let static_name = &self.infcx.tcx.item_name(def_id); - reason = format!(", as `{}` is an immutable static item", static_name); + reason = format!(", as `{static_name}` is an immutable static item"); } else { bug!("is_ref_to_static return true, but not ref to static?"); } @@ -148,15 +148,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { let pointer_type = source.describe_for_immutable_place(self.infcx.tcx); opt_source = Some(source); if let Some(desc) = self.describe_place(access_place.as_ref()) { - item_msg = format!("`{}`", desc); + item_msg = format!("`{desc}`"); reason = match error_access { - AccessKind::Mutate => format!(", which is behind {}", pointer_type), + AccessKind::Mutate => format!(", which is behind {pointer_type}"), AccessKind::MutableBorrow => { - format!(", as it is behind {}", pointer_type) + format!(", as it is behind {pointer_type}") } } } else { - item_msg = format!("data in {}", pointer_type); + item_msg = format!("data in {pointer_type}"); reason = String::new(); } } @@ -362,29 +362,27 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { let upvar_hir_id = captured_place.get_root_variable(); - if let Some(Node::Binding(pat)) = self.infcx.tcx.hir().find(upvar_hir_id) { - if let hir::PatKind::Binding( + if let Some(Node::Binding(pat)) = self.infcx.tcx.hir().find(upvar_hir_id) + && let hir::PatKind::Binding( hir::BindingAnnotation::Unannotated, _, upvar_ident, _, ) = pat.kind - { - err.span_suggestion( - upvar_ident.span, - "consider changing this to be mutable", - format!("mut {}", upvar_ident.name), - Applicability::MachineApplicable, - ); - } + { + err.span_suggestion( + upvar_ident.span, + "consider changing this to be mutable", + format!("mut {}", upvar_ident.name), + Applicability::MachineApplicable, + ); } let tcx = self.infcx.tcx; if let ty::Ref(_, ty, Mutability::Mut) = the_place_err.ty(self.body, tcx).ty.kind() + && let ty::Closure(id, _) = *ty.kind() { - if let ty::Closure(id, _) = *ty.kind() { - self.show_mutating_upvar(tcx, id, the_place_err, &mut err); - } + self.show_mutating_upvar(tcx, id, the_place_err, &mut err); } } @@ -544,8 +542,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { err.span_suggestion( err_help_span, &format!( - "consider changing this to be a mutable {}", - pointer_desc + "consider changing this to be a mutable {pointer_desc}" ), suggested_code, Applicability::MachineApplicable, @@ -554,8 +551,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { err.span_suggestion( x, &format!( - "consider changing that to be a mutable {}", - pointer_desc + "consider changing that to be a mutable {pointer_desc}" ), suggested_code, Applicability::MachineApplicable, @@ -606,15 +602,13 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { Some(BorrowedContentSource::OverloadedDeref(ty)) => { err.help(&format!( "trait `DerefMut` is required to modify through a dereference, \ - but it is not implemented for `{}`", - ty, + but it is not implemented for `{ty}`", )); } Some(BorrowedContentSource::OverloadedIndex(ty)) => { err.help(&format!( "trait `IndexMut` is required to modify indexed content, \ - but it is not implemented for `{}`", - ty, + but it is not implemented for `{ty}`", )); } _ => (), @@ -724,18 +718,18 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { ty::UpvarCapture::ByRef( ty::BorrowKind::MutBorrow | ty::BorrowKind::UniqueImmBorrow, ) => { - capture_reason = format!("mutable borrow of `{}`", upvar); + capture_reason = format!("mutable borrow of `{upvar}`"); } ty::UpvarCapture::ByValue => { - capture_reason = format!("possible mutation of `{}`", upvar); + capture_reason = format!("possible mutation of `{upvar}`"); } - _ => bug!("upvar `{}` borrowed, but not mutably", upvar), + _ => bug!("upvar `{upvar}` borrowed, but not mutably"), } break; } } if capture_reason.is_empty() { - bug!("upvar `{}` borrowed, but cannot find reason", upvar); + bug!("upvar `{upvar}` borrowed, but cannot find reason"); } capture_reason } else { @@ -829,19 +823,19 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { .as_str() .starts_with(&original_method_ident.name.to_string()) }) - .map(|ident| format!("{}()", ident)) + .map(|ident| format!("{ident}()")) .peekable() }); - if let Some(mut suggestions) = opt_suggestions { - if suggestions.peek().is_some() { - err.span_suggestions( - *span, - "use mutable method", - suggestions, - Applicability::MaybeIncorrect, - ); - } + if let Some(mut suggestions) = opt_suggestions + && suggestions.peek().is_some() + { + err.span_suggestions( + *span, + "use mutable method", + suggestions, + Applicability::MaybeIncorrect, + ); } } }; @@ -849,7 +843,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { /// Targeted error when encountering an `FnMut` closure where an `Fn` closure was expected. fn expected_fn_found_fn_mut_call(&self, err: &mut Diagnostic, sp: Span, act: &str) { - err.span_label(sp, format!("cannot {}", act)); + err.span_label(sp, format!("cannot {act}")); let hir = self.infcx.tcx.hir(); let closure_id = self.mir_hir_id(); @@ -1011,35 +1005,35 @@ fn suggest_ampmut<'tcx>( opt_assignment_rhs_span: Option, opt_ty_info: Option, ) -> (Span, String) { - if let Some(assignment_rhs_span) = opt_assignment_rhs_span { - if let Ok(src) = tcx.sess.source_map().span_to_snippet(assignment_rhs_span) { - let is_mutbl = |ty: &str| -> bool { - if let Some(rest) = ty.strip_prefix("mut") { - match rest.chars().next() { - // e.g. `&mut x` - Some(c) if c.is_whitespace() => true, - // e.g. `&mut(x)` - Some('(') => true, - // e.g. `&mut{x}` - Some('{') => true, - // e.g. `&mutablevar` - _ => false, - } - } else { - false - } - }; - if let (true, Some(ws_pos)) = (src.starts_with("&'"), src.find(char::is_whitespace)) { - let lt_name = &src[1..ws_pos]; - let ty = src[ws_pos..].trim_start(); - if !is_mutbl(ty) { - return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty)); - } - } else if let Some(stripped) = src.strip_prefix('&') { - let stripped = stripped.trim_start(); - if !is_mutbl(stripped) { - return (assignment_rhs_span, format!("&mut {}", stripped)); + if let Some(assignment_rhs_span) = opt_assignment_rhs_span + && let Ok(src) = tcx.sess.source_map().span_to_snippet(assignment_rhs_span) + { + let is_mutbl = |ty: &str| -> bool { + if let Some(rest) = ty.strip_prefix("mut") { + match rest.chars().next() { + // e.g. `&mut x` + Some(c) if c.is_whitespace() => true, + // e.g. `&mut(x)` + Some('(') => true, + // e.g. `&mut{x}` + Some('{') => true, + // e.g. `&mutablevar` + _ => false, } + } else { + false + } + }; + if let (true, Some(ws_pos)) = (src.starts_with("&'"), src.find(char::is_whitespace)) { + let lt_name = &src[1..ws_pos]; + let ty = src[ws_pos..].trim_start(); + if !is_mutbl(ty) { + return (assignment_rhs_span, format!("&{lt_name} mut {ty}")); + } + } else if let Some(stripped) = src.strip_prefix('&') { + let stripped = stripped.trim_start(); + if !is_mutbl(stripped) { + return (assignment_rhs_span, format!("&mut {stripped}")); } } } @@ -1054,12 +1048,12 @@ fn suggest_ampmut<'tcx>( None => local_decl.source_info.span, }; - if let Ok(src) = tcx.sess.source_map().span_to_snippet(highlight_span) { - if let (true, Some(ws_pos)) = (src.starts_with("&'"), src.find(char::is_whitespace)) { - let lt_name = &src[1..ws_pos]; - let ty = &src[ws_pos..]; - return (highlight_span, format!("&{} mut{}", lt_name, ty)); - } + if let Ok(src) = tcx.sess.source_map().span_to_snippet(highlight_span) + && let (true, Some(ws_pos)) = (src.starts_with("&'"), src.find(char::is_whitespace)) + { + let lt_name = &src[1..ws_pos]; + let ty = &src[ws_pos..]; + return (highlight_span, format!("&{} mut{}", lt_name, ty)); } let ty_mut = local_decl.ty.builtin_deref(true).unwrap(); diff --git a/compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs b/compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs index c1c325adb7f..ab9c206a46f 100644 --- a/compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs +++ b/compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs @@ -168,13 +168,12 @@ impl OutlivesSuggestionBuilder { let fr_name = self.region_vid_to_name(mbcx, errci.fr); let outlived_fr_name = self.region_vid_to_name(mbcx, errci.outlived_fr); - if let (Some(fr_name), Some(outlived_fr_name)) = (fr_name, outlived_fr_name) { - if !matches!(outlived_fr_name.source, RegionNameSource::Static) { - diag.help(&format!( - "consider adding the following bound: `{}: {}`", - fr_name, outlived_fr_name - )); - } + if let (Some(fr_name), Some(outlived_fr_name)) = (fr_name, outlived_fr_name) + && !matches!(outlived_fr_name.source, RegionNameSource::Static) + { + diag.help(&format!( + "consider adding the following bound: `{fr_name}: {outlived_fr_name}`", + )); } } diff --git a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs index 1798c525f6d..5fd9ecf4513 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs @@ -337,7 +337,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { ty::Ref(_, inner_ty, mutbl) => { assert_eq!(*mutbl, rustc_hir::Mutability::Mut); ( - format!("a mutable reference to `{}`", inner_ty), + format!("a mutable reference to `{inner_ty}`"), "mutable references are invariant over their type parameter" .to_string(), ) @@ -523,10 +523,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { if let Some((Some(outlived_fr_name), outlived_fr_span)) = outlived_fr_name_and_span { diag.span_label( outlived_fr_span, - format!( - "`{}` declared here, outside of the {} body", - outlived_fr_name, escapes_from - ), + format!("`{outlived_fr_name}` declared here, outside of the {escapes_from} body",), ); } @@ -534,12 +531,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { diag.span_label( fr_span, format!( - "`{}` is a reference that is only valid in the {} body", - fr_name, escapes_from + "`{fr_name}` is a reference that is only valid in the {escapes_from} body", ), ); - diag.span_label(*span, format!("`{}` escapes the {} body here", fr_name, escapes_from)); + diag.span_label(*span, format!("`{fr_name}` escapes the {escapes_from} body here")); } // Only show an extra note if we can find an 'error region' for both of the region @@ -611,9 +607,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { diag.span_label( *span, format!( - "{} was supposed to return data with lifetime `{}` but it is returning \ - data with lifetime `{}`", - mir_def_name, outlived_fr_name, fr_name + "{mir_def_name} was supposed to return data with lifetime `{outlived_fr_name}` but it is returning \ + data with lifetime `{fr_name}`", ), ); } @@ -698,7 +693,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { // If there is a static predicate, then the only sensible suggestion is to replace // fr with `'static`. if has_static_predicate { - diag.help(&format!("consider replacing `{}` with `{}`", fr_name, static_str)); + diag.help(&format!("consider replacing `{fr_name}` with `{static_str}`")); } else { // Otherwise, we should suggest adding a constraint on the return type. let span = self.infcx.tcx.def_span(did); @@ -714,14 +709,13 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { } else { span }; - let suggestion = format!(" + {}", suggestable_fr_name); + let suggestion = format!(" + {suggestable_fr_name}"); let span = span.shrink_to_hi(); diag.span_suggestion( span, &format!( "to allow this `impl Trait` to capture borrowed data with lifetime \ - `{}`, add `{}` as a bound", - fr_name, suggestable_fr_name, + `{fr_name}`, add `{suggestable_fr_name}` as a bound", ), suggestion, Applicability::MachineApplicable, diff --git a/compiler/rustc_borrowck/src/diagnostics/region_name.rs b/compiler/rustc_borrowck/src/diagnostics/region_name.rs index 723adb8da1b..7742813888e 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_name.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_name.rs @@ -102,27 +102,24 @@ impl RegionName { match &self.source { RegionNameSource::NamedFreeRegion(span) | RegionNameSource::NamedEarlyBoundRegion(span) => { - diag.span_label(*span, format!("lifetime `{}` defined here", self)); + diag.span_label(*span, format!("lifetime `{self}` defined here")); } RegionNameSource::SynthesizedFreeEnvRegion(span, note) => { - diag.span_label( - *span, - format!("lifetime `{}` represents this closure's body", self), - ); + diag.span_label(*span, format!("lifetime `{self}` represents this closure's body")); diag.note(note); } RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::CannotMatchHirTy( span, type_name, )) => { - diag.span_label(*span, format!("has type `{}`", type_name)); + diag.span_label(*span, format!("has type `{type_name}`")); } RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::MatchedHirTy(span)) | RegionNameSource::AnonRegionFromOutput(RegionNameHighlight::MatchedHirTy(span), _) | RegionNameSource::AnonRegionFromAsyncFn(span) => { diag.span_label( *span, - format!("let's call the lifetime of this reference `{}`", self), + format!("let's call the lifetime of this reference `{self}`"), ); } RegionNameSource::AnonRegionFromArgument( @@ -132,7 +129,7 @@ impl RegionName { RegionNameHighlight::MatchedAdtAndSegment(span), _, ) => { - diag.span_label(*span, format!("let's call this `{}`", self)); + diag.span_label(*span, format!("let's call this `{self}`")); } RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::Occluded( span, @@ -140,7 +137,7 @@ impl RegionName { )) => { diag.span_label( *span, - format!("lifetime `{}` appears in the type {}", self, type_name), + format!("lifetime `{self}` appears in the type {type_name}"), ); } RegionNameSource::AnonRegionFromOutput( @@ -150,25 +147,24 @@ impl RegionName { diag.span_label( *span, format!( - "return type{} `{}` contains a lifetime `{}`", - mir_description, type_name, self + "return type{mir_description} `{type_name}` contains a lifetime `{self}`" ), ); } RegionNameSource::AnonRegionFromUpvar(span, upvar_name) => { diag.span_label( *span, - format!("lifetime `{}` appears in the type of `{}`", self, upvar_name), + format!("lifetime `{self}` appears in the type of `{upvar_name}`"), ); } RegionNameSource::AnonRegionFromOutput( RegionNameHighlight::CannotMatchHirTy(span, type_name), mir_description, ) => { - diag.span_label(*span, format!("return type{} is {}", mir_description, type_name)); + diag.span_label(*span, format!("return type{mir_description} is {type_name}")); } RegionNameSource::AnonRegionFromYieldTy(span, type_name) => { - diag.span_label(*span, format!("yield type is {}", type_name)); + diag.span_label(*span, format!("yield type is {type_name}")); } RegionNameSource::Static => {} } @@ -442,7 +438,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { "highlight_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}", type_name, needle_fr ); - if type_name.contains(&format!("'{}", counter)) { + if type_name.contains(&format!("'{counter}")) { // Only add a label if we can confirm that a region was labelled. RegionNameHighlight::CannotMatchHirTy(span, type_name) } else { @@ -809,7 +805,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { // Note: generators from `async fn` yield `()`, so we don't have to // worry about them here. let yield_ty = self.regioncx.universal_regions().yield_ty?; - debug!("give_name_if_anonymous_region_appears_in_yield_ty: yield_ty = {:?}", yield_ty,); + debug!("give_name_if_anonymous_region_appears_in_yield_ty: yield_ty = {:?}", yield_ty); let tcx = self.infcx.tcx;