1
Fork 0

Use correct article in help message for conversion or cast

Before it always used `an`; now it uses the correct article for the type.
This commit is contained in:
Camelid 2020-09-27 13:36:48 -07:00
parent 1d216fef3e
commit 549f861f7d
14 changed files with 169 additions and 147 deletions

View file

@ -210,6 +210,18 @@ impl TyKind<'tcx> {
_ => false,
}
}
/// Get the article ("a" or "an") to use with this type.
///
/// **Panics if `self` is [`TyKind::Error`].**
pub fn article(&self) -> &'static str {
match self {
Int(_) | Float(_) | Array(_, _) => "an",
Adt(def, _) if def.is_enum() => "an",
Error(_) => panic!(),
_ => "a",
}
}
}
// `TyKind` is used a lot. Make sure it doesn't unintentionally get bigger.

View file

@ -752,8 +752,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
let msg = format!("you can convert an `{}` to `{}`", checked_ty, expected_ty);
let cast_msg = format!("you can cast an `{} to `{}`", checked_ty, expected_ty);
let msg = format!(
"you can convert {} `{}` to `{}`",
checked_ty.kind().article(),
checked_ty,
expected_ty
);
let cast_msg = format!(
"you can cast {} `{} to `{}`",
checked_ty.kind().article(),
checked_ty,
expected_ty
);
let lit_msg = format!(
"change the type of the numeric literal from `{}` to `{}`",
checked_ty, expected_ty,