From: Maybe Waffle Date: Wed, 23 Nov 2022 19:13:57 +0000 (+0000) Subject: Add `Mutability::mutably_str` X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=4439f1f6a6ac2e7b7a303717855addf505a9e591;p=rust.git Add `Mutability::mutably_str` --- diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index dc57f278df8..714fca6cc0b 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -804,6 +804,14 @@ pub fn ref_prefix_str(self) -> &'static str { } } + /// Returns `""` (empty string) or `"mutably "` depending on the mutability. + pub fn mutably_str(self) -> &'static str { + match self { + Mutability::Not => "", + Mutability::Mut => "mutably ", + } + } + /// Return `true` if self is mutable pub fn is_mut(self) -> bool { matches!(self, Self::Mut) diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index 3922c637a8c..c9e0986532d 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -576,10 +576,7 @@ fn suggest_borrow_fn_like( }) .collect(); err.multipart_suggestion_verbose( - &format!( - "consider {}borrowing {value_name}", - if borrow_level.is_mut() { "mutably " } else { "" } - ), + format!("consider {}borrowing {value_name}", borrow_level.mutably_str()), sugg, Applicability::MaybeIncorrect, ); diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index f60ceb94733..2106dce6f40 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -863,24 +863,14 @@ pub fn check_ref( } let sugg_expr = if needs_parens { format!("({src})") } else { src }; - return Some(match mutability { - hir::Mutability::Mut => ( - sp, - "consider mutably borrowing here".to_string(), - format!("{prefix}&mut {sugg_expr}"), - Applicability::MachineApplicable, - false, - false, - ), - hir::Mutability::Not => ( - sp, - "consider borrowing here".to_string(), - format!("{prefix}&{sugg_expr}"), - Applicability::MachineApplicable, - false, - false, - ), - }); + return Some(( + sp, + format!("consider {}borrowing here", mutability.mutably_str()), + format!("{prefix}{}{sugg_expr}", mutability.ref_prefix_str()), + Applicability::MachineApplicable, + false, + false, + )); } } }