Skip to content

Commit 3bf9124

Browse files
committed
Support other types of pluralization in pluralize macro
1 parent bce19cf commit 3bf9124

File tree

8 files changed

+26
-23
lines changed

8 files changed

+26
-23
lines changed

Diff for: compiler/rustc_lint_defs/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ macro_rules! pluralize {
2020
($x:expr) => {
2121
if $x != 1 { "s" } else { "" }
2222
};
23+
("is", $x:expr) => {
24+
if $x == 1 { "is" } else { "are" }
25+
};
26+
("this", $x:expr) => {
27+
if $x == 1 { "this" } else { "these" }
28+
};
2329
}
2430

2531
/// Indicates the confidence in the correctness of a suggestion.

Diff for: compiler/rustc_middle/src/mir/interpret/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
400400
Pointer::new(*alloc, access.access_offset),
401401
access.uninit_size.bytes(),
402402
pluralize!(access.uninit_size.bytes()),
403-
if access.uninit_size.bytes() != 1 { "are" } else { "is" },
403+
pluralize!("is", access.uninit_size.bytes()),
404404
Pointer::new(*alloc, access.uninit_offset),
405405
),
406406
InvalidUninitBytes(None) => write!(

Diff for: compiler/rustc_middle/src/ty/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ fn foo(&self) -> Self::T { String::new() }
847847
"{some} method{s} {are} available that return{r} `{ty}`",
848848
some = if methods.len() == 1 { "a" } else { "some" },
849849
s = pluralize!(methods.len()),
850-
are = if methods.len() == 1 { "is" } else { "are" },
850+
are = pluralize!("is", methods.len()),
851851
r = if methods.len() == 1 { "s" } else { "" },
852852
ty = expected
853853
);

Diff for: compiler/rustc_passes/src/check_attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,7 @@ impl CheckAttrVisitor<'_> {
14101410
span,
14111411
format!(
14121412
"there {} only {} argument{}",
1413-
if arg_count != 1 { "are" } else { "is" },
1413+
pluralize!("is", arg_count),
14141414
arg_count,
14151415
pluralize!(arg_count)
14161416
),

Diff for: compiler/rustc_resolve/src/late/diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,9 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
504504
for sp in spans {
505505
let msg = if sp == last_bound_span {
506506
format!(
507-
"...because of {} bound{}",
508-
if bounds.len() <= 2 { "this" } else { "these" },
509-
if bounds.len() <= 2 { "" } else { "s" },
507+
"...because of {these} bound{s}",
508+
these = pluralize!("this", bounds.len() - 1),
509+
s = pluralize!(bounds.len() - 1),
510510
)
511511
} else {
512512
String::new()

Diff for: compiler/rustc_typeck/src/check/pat.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17611761
sp,
17621762
&format!(
17631763
"include the missing field{} in the pattern{}",
1764-
if len == 1 { "" } else { "s" },
1764+
pluralize!(len),
17651765
if have_inaccessible_fields { " and ignore the inaccessible fields" } else { "" }
17661766
),
17671767
format!(
@@ -1780,10 +1780,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17801780
err.span_suggestion(
17811781
sp,
17821782
&format!(
1783-
"if you don't care about {} missing field{}, you can explicitly ignore {}",
1784-
if len == 1 { "this" } else { "these" },
1785-
if len == 1 { "" } else { "s" },
1786-
if len == 1 { "it" } else { "them" },
1783+
"if you don't care about {these} missing field{s}, you can explicitly ignore {them}",
1784+
these = pluralize!("this", len),
1785+
s = pluralize!(len),
1786+
them = if len == 1 { "it" } else { "them" },
17871787
),
17881788
format!("{}..{}", prefix, postfix),
17891789
Applicability::MachineApplicable,

Diff for: compiler/rustc_typeck/src/coherence/inherent_impls.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! `tcx.inherent_impls(def_id)`). That value, however,
88
//! is computed by selecting an idea from this table.
99
10-
use rustc_errors::struct_span_err;
10+
use rustc_errors::{pluralize, struct_span_err};
1111
use rustc_hir as hir;
1212
use rustc_hir::def_id::{DefId, LocalDefId};
1313
use rustc_hir::itemlikevisit::ItemLikeVisitor;
@@ -410,7 +410,6 @@ impl<'tcx> InherentCollect<'tcx> {
410410
let to_implement = if assoc_items.is_empty() {
411411
String::new()
412412
} else {
413-
let plural = assoc_items.len() > 1;
414413
let assoc_items_kind = {
415414
let item_types = assoc_items.iter().map(|x| x.kind);
416415
if item_types.clone().all(|x| x == hir::AssocItemKind::Const) {
@@ -427,9 +426,9 @@ impl<'tcx> InherentCollect<'tcx> {
427426

428427
format!(
429428
" to implement {} {}{}",
430-
if plural { "these" } else { "this" },
429+
pluralize!("this", assoc_items.len()),
431430
assoc_items_kind,
432-
if plural { "s" } else { "" }
431+
pluralize!(assoc_items.len()),
433432
)
434433
};
435434

Diff for: compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -657,10 +657,9 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
657657

658658
let num_redundant_lt_args = lt_arg_spans.len() - self.num_expected_lifetime_args();
659659
let msg_lifetimes = format!(
660-
"remove {} {} argument{}",
661-
if num_redundant_lt_args == 1 { "this" } else { "these" },
662-
"lifetime",
663-
pluralize!(num_redundant_lt_args),
660+
"remove {these} lifetime argument{s}",
661+
these = pluralize!("this", num_redundant_lt_args),
662+
s = pluralize!(num_redundant_lt_args),
664663
);
665664

666665
err.span_suggestion(
@@ -700,10 +699,9 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
700699
let num_redundant_gen_args =
701700
gen_arg_spans.len() - self.num_expected_type_or_const_args();
702701
let msg_types_or_consts = format!(
703-
"remove {} {} argument{}",
704-
if num_redundant_gen_args == 1 { "this" } else { "these" },
705-
"generic",
706-
pluralize!(num_redundant_type_or_const_args),
702+
"remove {these} generic argument{s}",
703+
these = pluralize!("this", num_redundant_gen_args),
704+
s = pluralize!(num_redundant_gen_args),
707705
);
708706

709707
err.span_suggestion(

0 commit comments

Comments
 (0)