Support other types of pluralization in pluralize macro

This commit is contained in:
est31 2022-03-14 17:55:14 +01:00
parent bce19cf7f1
commit 3bf9124f14
8 changed files with 26 additions and 23 deletions

View file

@ -20,6 +20,12 @@ macro_rules! pluralize {
($x:expr) => { ($x:expr) => {
if $x != 1 { "s" } else { "" } if $x != 1 { "s" } else { "" }
}; };
("is", $x:expr) => {
if $x == 1 { "is" } else { "are" }
};
("this", $x:expr) => {
if $x == 1 { "this" } else { "these" }
};
} }
/// Indicates the confidence in the correctness of a suggestion. /// Indicates the confidence in the correctness of a suggestion.

View file

@ -400,7 +400,7 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
Pointer::new(*alloc, access.access_offset), Pointer::new(*alloc, access.access_offset),
access.uninit_size.bytes(), access.uninit_size.bytes(),
pluralize!(access.uninit_size.bytes()), pluralize!(access.uninit_size.bytes()),
if access.uninit_size.bytes() != 1 { "are" } else { "is" }, pluralize!("is", access.uninit_size.bytes()),
Pointer::new(*alloc, access.uninit_offset), Pointer::new(*alloc, access.uninit_offset),
), ),
InvalidUninitBytes(None) => write!( InvalidUninitBytes(None) => write!(

View file

@ -847,7 +847,7 @@ fn foo(&self) -> Self::T { String::new() }
"{some} method{s} {are} available that return{r} `{ty}`", "{some} method{s} {are} available that return{r} `{ty}`",
some = if methods.len() == 1 { "a" } else { "some" }, some = if methods.len() == 1 { "a" } else { "some" },
s = pluralize!(methods.len()), s = pluralize!(methods.len()),
are = if methods.len() == 1 { "is" } else { "are" }, are = pluralize!("is", methods.len()),
r = if methods.len() == 1 { "s" } else { "" }, r = if methods.len() == 1 { "s" } else { "" },
ty = expected ty = expected
); );

View file

@ -1410,7 +1410,7 @@ impl CheckAttrVisitor<'_> {
span, span,
format!( format!(
"there {} only {} argument{}", "there {} only {} argument{}",
if arg_count != 1 { "are" } else { "is" }, pluralize!("is", arg_count),
arg_count, arg_count,
pluralize!(arg_count) pluralize!(arg_count)
), ),

View file

@ -504,9 +504,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
for sp in spans { for sp in spans {
let msg = if sp == last_bound_span { let msg = if sp == last_bound_span {
format!( format!(
"...because of {} bound{}", "...because of {these} bound{s}",
if bounds.len() <= 2 { "this" } else { "these" }, these = pluralize!("this", bounds.len() - 1),
if bounds.len() <= 2 { "" } else { "s" }, s = pluralize!(bounds.len() - 1),
) )
} else { } else {
String::new() String::new()

View file

@ -1761,7 +1761,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
sp, sp,
&format!( &format!(
"include the missing field{} in the pattern{}", "include the missing field{} in the pattern{}",
if len == 1 { "" } else { "s" }, pluralize!(len),
if have_inaccessible_fields { " and ignore the inaccessible fields" } else { "" } if have_inaccessible_fields { " and ignore the inaccessible fields" } else { "" }
), ),
format!( format!(
@ -1780,10 +1780,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.span_suggestion( err.span_suggestion(
sp, sp,
&format!( &format!(
"if you don't care about {} missing field{}, you can explicitly ignore {}", "if you don't care about {these} missing field{s}, you can explicitly ignore {them}",
if len == 1 { "this" } else { "these" }, these = pluralize!("this", len),
if len == 1 { "" } else { "s" }, s = pluralize!(len),
if len == 1 { "it" } else { "them" }, them = if len == 1 { "it" } else { "them" },
), ),
format!("{}..{}", prefix, postfix), format!("{}..{}", prefix, postfix),
Applicability::MachineApplicable, Applicability::MachineApplicable,

View file

@ -7,7 +7,7 @@
//! `tcx.inherent_impls(def_id)`). That value, however, //! `tcx.inherent_impls(def_id)`). That value, however,
//! is computed by selecting an idea from this table. //! is computed by selecting an idea from this table.
use rustc_errors::struct_span_err; use rustc_errors::{pluralize, struct_span_err};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_hir::itemlikevisit::ItemLikeVisitor;
@ -410,7 +410,6 @@ impl<'tcx> InherentCollect<'tcx> {
let to_implement = if assoc_items.is_empty() { let to_implement = if assoc_items.is_empty() {
String::new() String::new()
} else { } else {
let plural = assoc_items.len() > 1;
let assoc_items_kind = { let assoc_items_kind = {
let item_types = assoc_items.iter().map(|x| x.kind); let item_types = assoc_items.iter().map(|x| x.kind);
if item_types.clone().all(|x| x == hir::AssocItemKind::Const) { if item_types.clone().all(|x| x == hir::AssocItemKind::Const) {
@ -427,9 +426,9 @@ impl<'tcx> InherentCollect<'tcx> {
format!( format!(
" to implement {} {}{}", " to implement {} {}{}",
if plural { "these" } else { "this" }, pluralize!("this", assoc_items.len()),
assoc_items_kind, assoc_items_kind,
if plural { "s" } else { "" } pluralize!(assoc_items.len()),
) )
}; };

View file

@ -657,10 +657,9 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
let num_redundant_lt_args = lt_arg_spans.len() - self.num_expected_lifetime_args(); let num_redundant_lt_args = lt_arg_spans.len() - self.num_expected_lifetime_args();
let msg_lifetimes = format!( let msg_lifetimes = format!(
"remove {} {} argument{}", "remove {these} lifetime argument{s}",
if num_redundant_lt_args == 1 { "this" } else { "these" }, these = pluralize!("this", num_redundant_lt_args),
"lifetime", s = pluralize!(num_redundant_lt_args),
pluralize!(num_redundant_lt_args),
); );
err.span_suggestion( err.span_suggestion(
@ -700,10 +699,9 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
let num_redundant_gen_args = let num_redundant_gen_args =
gen_arg_spans.len() - self.num_expected_type_or_const_args(); gen_arg_spans.len() - self.num_expected_type_or_const_args();
let msg_types_or_consts = format!( let msg_types_or_consts = format!(
"remove {} {} argument{}", "remove {these} generic argument{s}",
if num_redundant_gen_args == 1 { "this" } else { "these" }, these = pluralize!("this", num_redundant_gen_args),
"generic", s = pluralize!(num_redundant_gen_args),
pluralize!(num_redundant_type_or_const_args),
); );
err.span_suggestion( err.span_suggestion(