Add `Mutability::{is_mut,is_not}`

This commit is contained in:
Maybe Waffle 2022-11-23 18:22:51 +00:00
parent 9b9c7d0ecc
commit da40965300
14 changed files with 29 additions and 26 deletions

View File

@ -803,6 +803,16 @@ impl Mutability {
Mutability::Mut => "&mut ",
}
}
/// Return `true` if self is mutable
pub fn is_mut(self) -> bool {
matches!(self, Self::Mut)
}
/// Return `true` if self is **not** mutable
pub fn is_not(self) -> bool {
matches!(self, Self::Not)
}
}
/// The kind of borrow in an `AddrOf` expression,

View File

@ -1781,9 +1781,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// Given we are only considering `ImplicitSelf` types, we needn't consider
// the case where we have a mutable pattern to a reference as that would
// no longer be an `ImplicitSelf`.
TyKind::Rptr(_, mt)
if mt.ty.kind.is_implicit_self() && mt.mutbl == ast::Mutability::Mut =>
{
TyKind::Rptr(_, mt) if mt.ty.kind.is_implicit_self() && mt.mutbl.is_mut() => {
hir::ImplicitSelfKind::MutRef
}
TyKind::Rptr(_, mt) if mt.ty.kind.is_implicit_self() => {

View File

@ -578,7 +578,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
err.multipart_suggestion_verbose(
&format!(
"consider {}borrowing {value_name}",
if borrow_level == hir::Mutability::Mut { "mutably " } else { "" }
if borrow_level.is_mut() { "mutably " } else { "" }
),
sugg,
Applicability::MaybeIncorrect,

View File

@ -257,9 +257,9 @@ pub(crate) fn data_id_for_alloc_id(
mutability: rustc_hir::Mutability,
) -> DataId {
cx.todo.push(TodoItem::Alloc(alloc_id));
*cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
module.declare_anonymous_data(mutability == rustc_hir::Mutability::Mut, false).unwrap()
})
*cx.anon_allocs
.entry(alloc_id)
.or_insert_with(|| module.declare_anonymous_data(mutability.is_mut(), false).unwrap())
}
fn data_id_for_static(
@ -343,12 +343,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
}
};
let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
module
.declare_anonymous_data(
alloc.inner().mutability == rustc_hir::Mutability::Mut,
false,
)
.unwrap()
module.declare_anonymous_data(alloc.inner().mutability.is_mut(), false).unwrap()
});
(data_id, alloc, None)
}

View File

@ -1500,7 +1500,7 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
let (_, element_ty1) = arg_tys[1].simd_size_and_type(bx.tcx());
let (_, element_ty2) = arg_tys[2].simd_size_and_type(bx.tcx());
let (pointer_count, underlying_ty) = match element_ty1.kind() {
ty::RawPtr(p) if p.ty == in_elem && p.mutbl == hir::Mutability::Mut => {
ty::RawPtr(p) if p.ty == in_elem && p.mutbl.is_mut() => {
(ptr_count(element_ty1), non_ptr(element_ty1))
}
_ => {

View File

@ -292,7 +292,7 @@ fn emit_newtype_suggestion_for_raw_ptr(
diag: &mut Diagnostic,
) {
if !self_ty.needs_subst() {
let mut_key = if ptr_ty.mutbl == rustc_middle::mir::Mutability::Mut { "mut " } else { "" };
let mut_key = ptr_ty.mutbl.prefix_str();
let msg_sugg = "consider introducing a new wrapper type".to_owned();
let sugg = vec![
(

View File

@ -398,7 +398,7 @@ impl<'a> State<'a> {
}
hir::ForeignItemKind::Static(t, m) => {
self.head("static");
if m == hir::Mutability::Mut {
if m.is_mut() {
self.word_space("mut");
}
self.print_ident(item.ident);
@ -519,7 +519,7 @@ impl<'a> State<'a> {
}
hir::ItemKind::Static(ty, m, expr) => {
self.head("static");
if m == hir::Mutability::Mut {
if m.is_mut() {
self.word_space("mut");
}
self.print_ident(item.ident);

View File

@ -197,7 +197,7 @@ fn check_panic_info_fn(
let arg_is_panic_info = match *inputs[0].kind() {
ty::Ref(region, ty, mutbl) => match *ty.kind() {
ty::Adt(ref adt, _) => {
adt.did() == panic_info_did && mutbl == hir::Mutability::Not && !region.is_static()
adt.did() == panic_info_did && mutbl.is_not() && !region.is_static()
}
_ => false,
},

View File

@ -452,7 +452,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
return Err(err);
};
if ty == a && mt_a.mutbl == hir::Mutability::Not && autoderef.step_count() == 1 {
if ty == a && mt_a.mutbl.is_not() && autoderef.step_count() == 1 {
// As a special case, if we would produce `&'a *x`, that's
// a total no-op. We end up with the type `&'a T` just as
// we started with. In that case, just skip it

View File

@ -856,7 +856,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
..
})) = self.tcx.hir().find(self.tcx.hir().get_parent_node(expr.hir_id))
{
if mutability == hir::Mutability::Mut {
if mutability.is_mut() {
// Suppressing this diagnostic, we'll properly print it in `check_expr_assign`
return None;
}

View File

@ -702,7 +702,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};
let mut_var_suggestion = 'block: {
if !matches!(mutbl, ast::Mutability::Mut) {
if mutbl.is_not() {
break 'block None;
}

View File

@ -169,7 +169,7 @@ impl<'hir> Sig for hir::Ty<'hir> {
let mut prefix = "&".to_owned();
prefix.push_str(&lifetime.name.ident().to_string());
prefix.push(' ');
if let hir::Mutability::Mut = mt.mutbl {
if mt.mutbl.is_mut() {
prefix.push_str("mut ");
};
@ -332,7 +332,7 @@ impl<'hir> Sig for hir::Item<'hir> {
match self.kind {
hir::ItemKind::Static(ref ty, m, ref body) => {
let mut text = "static ".to_owned();
if m == hir::Mutability::Mut {
if m.is_mut() {
text.push_str("mut ");
}
let name = self.ident.to_string();

View File

@ -621,7 +621,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
});
match inner_ty.kind() {
ty::Str if *mutbl == hir::Mutability::Not => {
ty::Str if mutbl.is_not() => {
match ct.kind() {
ty::ConstKind::Value(valtree) => {
let slice =

View File

@ -1059,7 +1059,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
{
(
mk_result(old_pred.map_bound(|trait_pred| (trait_pred, *ty))),
matches!(mutability, hir::Mutability::Mut),
mutability.is_mut(),
)
} else {
(false, false)
@ -1348,7 +1348,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
.sess
.source_map()
.span_take_while(span, |c| c.is_whitespace() || *c == '&');
if points_at_arg && mutability == hir::Mutability::Not && refs_number > 0 {
if points_at_arg && mutability.is_not() && refs_number > 0 {
err.span_suggestion_verbose(
sp,
"consider changing this borrow's mutability",