Skip to content

Commit 4439f1f

Browse files
committed
Add Mutability::mutably_str
1 parent e51cd6e commit 4439f1f

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,14 @@ impl Mutability {
804804
}
805805
}
806806

807+
/// Returns `""` (empty string) or `"mutably "` depending on the mutability.
808+
pub fn mutably_str(self) -> &'static str {
809+
match self {
810+
Mutability::Not => "",
811+
Mutability::Mut => "mutably ",
812+
}
813+
}
814+
807815
/// Return `true` if self is mutable
808816
pub fn is_mut(self) -> bool {
809817
matches!(self, Self::Mut)

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -576,10 +576,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
576576
})
577577
.collect();
578578
err.multipart_suggestion_verbose(
579-
&format!(
580-
"consider {}borrowing {value_name}",
581-
if borrow_level.is_mut() { "mutably " } else { "" }
582-
),
579+
format!("consider {}borrowing {value_name}", borrow_level.mutably_str()),
583580
sugg,
584581
Applicability::MaybeIncorrect,
585582
);

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -863,24 +863,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
863863
}
864864

865865
let sugg_expr = if needs_parens { format!("({src})") } else { src };
866-
return Some(match mutability {
867-
hir::Mutability::Mut => (
868-
sp,
869-
"consider mutably borrowing here".to_string(),
870-
format!("{prefix}&mut {sugg_expr}"),
871-
Applicability::MachineApplicable,
872-
false,
873-
false,
874-
),
875-
hir::Mutability::Not => (
876-
sp,
877-
"consider borrowing here".to_string(),
878-
format!("{prefix}&{sugg_expr}"),
879-
Applicability::MachineApplicable,
880-
false,
881-
false,
882-
),
883-
});
866+
return Some((
867+
sp,
868+
format!("consider {}borrowing here", mutability.mutably_str()),
869+
format!("{prefix}{}{sugg_expr}", mutability.ref_prefix_str()),
870+
Applicability::MachineApplicable,
871+
false,
872+
false,
873+
));
884874
}
885875
}
886876
}

0 commit comments

Comments
 (0)