]> git.lizzy.rs Git - rust.git/commitdiff
Add `Mutability::mutably_str`
authorMaybe Waffle <waffle.lapkin@gmail.com>
Wed, 23 Nov 2022 19:13:57 +0000 (19:13 +0000)
committerMaybe Waffle <waffle.lapkin@gmail.com>
Wed, 23 Nov 2022 20:39:16 +0000 (20:39 +0000)
compiler/rustc_ast/src/ast.rs
compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
compiler/rustc_hir_typeck/src/demand.rs

index dc57f278df84f0bd58d6108f555dcad773a9ec9b..714fca6cc0b09299c2f4c0e1b37263d1ee8f95d8 100644 (file)
@@ -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)
index 3922c637a8c051b4aa39b14423b23d70cecdfd7e..c9e0986532d28d86a7dde7afb274212a5c09e8ec 100644 (file)
@@ -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,
         );
index f60ceb94733754c24d6e12cd6fb55736f77860d3..2106dce6f407ed2e03dd901732798e2e08e4a28b 100644 (file)
@@ -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,
+                        ));
                     }
                 }
             }