Skip to content

Commit 1ab5593

Browse files
author
gaolei
committed
factor out pluralisation remains after #64280
1 parent 19d0703 commit 1ab5593

File tree

17 files changed

+56
-45
lines changed

17 files changed

+56
-45
lines changed

src/librustc/lint/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use crate::lint::{LintPass, LateLintPass, LintArray};
88
use crate::middle::stability;
99
use crate::session::Session;
10-
use errors::{Applicability, DiagnosticBuilder};
10+
use errors::{Applicability, DiagnosticBuilder, pluralise};
1111
use syntax::ast;
1212
use syntax::source_map::Span;
1313
use syntax::symbol::Symbol;
@@ -524,7 +524,7 @@ pub(crate) fn add_elided_lifetime_in_path_suggestion(
524524
};
525525
db.span_suggestion(
526526
replace_span,
527-
&format!("indicate the anonymous lifetime{}", if n >= 2 { "s" } else { "" }),
527+
&format!("indicate the anonymous lifetime{}", pluralise!(n)),
528528
suggestion,
529529
Applicability::MachineApplicable
530530
);

src/librustc/middle/resolve_lifetime.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::ty::{self, DefIdTree, GenericParamDefKind, TyCtxt};
1717
use crate::rustc::lint;
1818
use crate::session::Session;
1919
use crate::util::nodemap::{DefIdMap, FxHashMap, FxHashSet, HirIdMap, HirIdSet};
20-
use errors::{Applicability, DiagnosticBuilder};
20+
use errors::{Applicability, DiagnosticBuilder, pluralise};
2121
use rustc_macros::HashStable;
2222
use std::borrow::Cow;
2323
use std::cell::Cell;
@@ -3047,7 +3047,7 @@ pub fn report_missing_lifetime_specifiers(
30473047
span,
30483048
E0106,
30493049
"missing lifetime specifier{}",
3050-
if count > 1 { "s" } else { "" }
3050+
pluralise!(count)
30513051
)
30523052
}
30533053

src/librustc/traits/error_reporting.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::ty::subst::Subst;
3333
use crate::ty::SubtypePredicate;
3434
use crate::util::nodemap::{FxHashMap, FxHashSet};
3535

36-
use errors::{Applicability, DiagnosticBuilder};
36+
use errors::{Applicability, DiagnosticBuilder, pluralise};
3737
use std::fmt;
3838
use syntax::ast;
3939
use syntax::symbol::{sym, kw};
@@ -1186,7 +1186,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
11861186
_ => format!("{} {}argument{}",
11871187
arg_length,
11881188
if distinct && arg_length > 1 { "distinct " } else { "" },
1189-
if arg_length == 1 { "" } else { "s" }),
1189+
pluralise!(arg_length))
11901190
}
11911191
};
11921192

src/librustc/ty/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl<'tcx> ty::TyS<'tcx> {
196196
let n = tcx.lift_to_global(&n).unwrap();
197197
match n.try_eval_usize(tcx, ty::ParamEnv::empty()) {
198198
Some(n) => {
199-
format!("array of {} element{}", n, if n != 1 { "s" } else { "" }).into()
199+
format!("array of {} element{}", n, pluralise!(n)).into()
200200
}
201201
None => "array".into(),
202202
}

src/librustc_lint/unused.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use lint::{LintPass, EarlyLintPass, LateLintPass};
99

1010
use syntax::ast;
1111
use syntax::attr;
12-
use syntax::errors::Applicability;
12+
use syntax::errors::{Applicability, pluralise};
1313
use syntax::feature_gate::{AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
1414
use syntax::print::pprust;
1515
use syntax::symbol::{kw, sym};
@@ -48,7 +48,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
4848
}
4949

5050
let ty = cx.tables.expr_ty(&expr);
51-
let type_permits_lack_of_use = check_must_use_ty(cx, ty, &expr, s.span, "", "", false);
51+
let type_permits_lack_of_use = check_must_use_ty(cx, ty, &expr, s.span, "", "", 1);
5252

5353
let mut fn_warned = false;
5454
let mut op_warned = false;
@@ -135,21 +135,21 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
135135
span: Span,
136136
descr_pre: &str,
137137
descr_post: &str,
138-
plural: bool,
138+
plural_len: usize,
139139
) -> bool {
140140
if ty.is_unit() || cx.tcx.is_ty_uninhabited_from(
141141
cx.tcx.hir().get_module_parent(expr.hir_id), ty)
142142
{
143143
return true;
144144
}
145145

146-
let plural_suffix = if plural { "s" } else { "" };
146+
let plural_suffix = pluralise!(plural_len);
147147

148148
match ty.sty {
149149
ty::Adt(..) if ty.is_box() => {
150150
let boxed_ty = ty.boxed_ty();
151151
let descr_pre = &format!("{}boxed ", descr_pre);
152-
check_must_use_ty(cx, boxed_ty, expr, span, descr_pre, descr_post, plural)
152+
check_must_use_ty(cx, boxed_ty, expr, span, descr_pre, descr_post, plural_len)
153153
}
154154
ty::Adt(def, _) => {
155155
check_must_use_def(cx, def.did, span, descr_pre, descr_post)
@@ -202,7 +202,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
202202
for (i, ty) in tys.iter().map(|k| k.expect_ty()).enumerate() {
203203
let descr_post = &format!(" in tuple element {}", i);
204204
let span = *spans.get(i).unwrap_or(&span);
205-
if check_must_use_ty(cx, ty, expr, span, descr_pre, descr_post, plural) {
205+
if check_must_use_ty(
206+
cx,
207+
ty,
208+
expr,
209+
span,
210+
descr_pre,
211+
descr_post,
212+
plural_len
213+
) {
206214
has_emitted = true;
207215
}
208216
}
@@ -216,7 +224,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
216224
descr_pre,
217225
plural_suffix,
218226
);
219-
check_must_use_ty(cx, ty, expr, span, descr_pre, descr_post, true)
227+
check_must_use_ty(cx, ty, expr, span, descr_pre, descr_post, n as usize + 1)
220228
}
221229
// Otherwise, we don't lint, to avoid false positives.
222230
_ => false,

src/librustc_resolve/check_unused.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
use crate::Resolver;
2727
use crate::resolve_imports::ImportDirectiveSubclass;
2828

29+
use errors::pluralise;
30+
2931
use rustc::util::nodemap::NodeMap;
3032
use rustc::{lint, ty};
3133
use rustc_data_structures::fx::FxHashSet;
@@ -295,7 +297,7 @@ impl Resolver<'_> {
295297
}).collect::<Vec<String>>();
296298
span_snippets.sort();
297299
let msg = format!("unused import{}{}",
298-
if len > 1 { "s" } else { "" },
300+
pluralise!(len),
299301
if !span_snippets.is_empty() {
300302
format!(": {}", span_snippets.join(", "))
301303
} else {

src/librustc_resolve/resolve_imports.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{Resolver, ResolutionError, Segment, ModuleKind};
1111
use crate::{names_to_string, module_to_string};
1212
use crate::diagnostics::Suggestion;
1313

14-
use errors::Applicability;
14+
use errors::{Applicability, pluralise};
1515

1616
use rustc_data_structures::ptr_key::PtrKey;
1717
use rustc::ty;
@@ -728,7 +728,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
728728

729729
let msg = format!(
730730
"unresolved import{} {}",
731-
if paths.len() > 1 { "s" } else { "" },
731+
pluralise!(paths.len()),
732732
paths.join(", "),
733733
);
734734

src/librustc_typeck/astconv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
13461346
span,
13471347
E0191,
13481348
"the value of the associated type{} {} must be specified",
1349-
if associated_types.len() == 1 { "" } else { "s" },
1349+
pluralise!(associated_types.len()),
13501350
names,
13511351
);
13521352
let (suggest, potential_assoc_types_spans) =

src/librustc_typeck/check/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::util::common::ErrorReported;
1717
use crate::util::nodemap::FxHashMap;
1818
use crate::astconv::AstConv as _;
1919

20-
use errors::{Applicability, DiagnosticBuilder};
20+
use errors::{Applicability, DiagnosticBuilder, pluralise};
2121
use syntax::ast;
2222
use syntax::symbol::{Symbol, kw, sym};
2323
use syntax::source_map::Span;
@@ -1178,7 +1178,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11781178

11791179
struct_span_err!(tcx.sess, span, E0063,
11801180
"missing field{} {}{} in initializer of `{}`",
1181-
if remaining_fields.len() == 1 { "" } else { "s" },
1181+
pluralise!(remaining_fields.len()),
11821182
remaining_fields_names,
11831183
truncated_fields_error,
11841184
adt_ty)

src/librustc_typeck/check/method/suggest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::check::FnCtxt;
55
use crate::middle::lang_items::FnOnceTraitLangItem;
66
use crate::namespace::Namespace;
77
use crate::util::nodemap::FxHashSet;
8-
use errors::{Applicability, DiagnosticBuilder};
8+
use errors::{Applicability, DiagnosticBuilder, pluralise};
99
use rustc::hir::{self, ExprKind, Node, QPath};
1010
use rustc::hir::def::{Res, DefKind};
1111
use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
@@ -560,7 +560,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
560560
let help = format!("{an}other candidate{s} {were} found in the following \
561561
trait{s}, perhaps add a `use` for {one_of_them}:",
562562
an = if candidates.len() == 1 {"an" } else { "" },
563-
s = if candidates.len() == 1 { "" } else { "s" },
563+
s = pluralise!(candidates.len()),
564564
were = if candidates.len() == 1 { "was" } else { "were" },
565565
one_of_them = if candidates.len() == 1 {
566566
"it"

src/librustc_typeck/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub mod intrinsic;
8888
mod op;
8989

9090
use crate::astconv::{AstConv, PathSeg};
91-
use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
91+
use errors::{Applicability, DiagnosticBuilder, DiagnosticId, pluralise};
9292
use rustc::hir::{self, ExprKind, GenericArg, ItemKind, Node, PatKind, QPath};
9393
use rustc::hir::def::{CtorOf, Res, DefKind};
9494
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
@@ -4843,5 +4843,5 @@ fn fatally_break_rust(sess: &Session) {
48434843
}
48444844

48454845
fn potentially_plural_count(count: usize, word: &str) -> String {
4846-
format!("{} {}{}", count, word, if count == 1 { "" } else { "s" })
4846+
format!("{} {}{}", count, word, pluralise!(count))
48474847
}

src/librustc_typeck/check/pat.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::check::FnCtxt;
22
use crate::util::nodemap::FxHashMap;
3-
use errors::{Applicability, DiagnosticBuilder};
3+
use errors::{Applicability, DiagnosticBuilder, pluralise};
44
use rustc::hir::{self, PatKind, Pat, HirId};
55
use rustc::hir::def::{Res, DefKind, CtorKind};
66
use rustc::hir::pat_util::EnumerateAndAdjustIterator;
@@ -684,8 +684,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
684684
}
685685

686686
fn e0023(&self, pat_span: Span, res: Res, subpats: &'tcx [P<Pat>], fields: &[ty::FieldDef]) {
687-
let subpats_ending = if subpats.len() == 1 { "" } else { "s" };
688-
let fields_ending = if fields.len() == 1 { "" } else { "s" };
687+
let subpats_ending = pluralise!(subpats.len());
688+
let fields_ending = pluralise!(fields.len());
689689
let res_span = self.tcx.def_span(res.def_id());
690690
struct_span_err!(
691691
self.tcx.sess,
@@ -1103,10 +1103,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11031103
E0527,
11041104
"pattern requires {} element{} but array has {}",
11051105
min_len,
1106-
if min_len != 1 { "s" } else { "" },
1106+
pluralise!(min_len),
11071107
size,
11081108
)
1109-
.span_label(span, format!("expected {} element{}", size, if size != 1 { "s" } else { "" }))
1109+
.span_label(span, format!("expected {} element{}", size, pluralise!(size)))
11101110
.emit();
11111111
}
11121112

@@ -1117,14 +1117,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11171117
E0528,
11181118
"pattern requires at least {} element{} but array has {}",
11191119
min_len,
1120-
if min_len != 1 { "s" } else { "" },
1120+
pluralise!(min_len),
11211121
size,
11221122
).span_label(
11231123
span,
11241124
format!(
11251125
"pattern cannot match array of {} element{}",
11261126
size,
1127-
if size != 1 { "s" } else { "" },
1127+
pluralise!(size),
11281128
),
11291129
).emit();
11301130
}

src/libsyntax/ext/tt/transcribe.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndJoint};
88

99
use smallvec::{smallvec, SmallVec};
1010

11+
use errors::pluralise;
1112
use rustc_data_structures::fx::FxHashMap;
1213
use rustc_data_structures::sync::Lrc;
1314
use syntax_pos::hygiene::{ExpnId, Transparency};
@@ -348,10 +349,10 @@ impl LockstepIterSize {
348349
"meta-variable `{}` repeats {} time{}, but `{}` repeats {} time{}",
349350
l_id,
350351
l_len,
351-
if l_len != 1 { "s" } else { "" },
352+
pluralise!(l_len),
352353
r_id,
353354
r_len,
354-
if r_len != 1 { "s" } else { "" },
355+
pluralise!(r_len),
355356
);
356357
LockstepIterSize::Contradiction(msg)
357358
}

src/libsyntax/parse/diagnostics.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::ptr::P;
1111
use crate::symbol::{kw, sym};
1212
use crate::ThinVec;
1313
use crate::util::parser::AssocOp;
14-
use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
14+
use errors::{Applicability, DiagnosticBuilder, DiagnosticId, pluralise};
1515
use rustc_data_structures::fx::FxHashSet;
1616
use syntax_pos::{Span, DUMMY_SP, MultiSpan, SpanSnippetError};
1717
use log::{debug, trace};
@@ -532,15 +532,15 @@ impl<'a> Parser<'a> {
532532
self.eat_to_tokens(&[&end]);
533533
let span = lo.until(self.token.span);
534534

535-
let plural = number_of_gt > 1 || number_of_shr >= 1;
535+
let total_num_of_gt = number_of_gt + number_of_shr * 2;
536536
self.diagnostic()
537537
.struct_span_err(
538538
span,
539-
&format!("unmatched angle bracket{}", if plural { "s" } else { "" }),
539+
&format!("unmatched angle bracket{}", pluralise!(total_num_of_gt)),
540540
)
541541
.span_suggestion(
542542
span,
543-
&format!("remove extra angle bracket{}", if plural { "s" } else { "" }),
543+
&format!("remove extra angle bracket{}", pluralise!(total_num_of_gt)),
544544
String::new(),
545545
Applicability::MachineApplicable,
546546
)

src/libsyntax/parse/parser/path.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::symbol::kw;
99

1010
use std::mem;
1111
use log::debug;
12-
use errors::{Applicability};
12+
use errors::{Applicability, pluralise};
1313

1414
/// Specifies how to parse a path.
1515
#[derive(Copy, Clone, PartialEq)]
@@ -347,20 +347,19 @@ impl<'a> Parser<'a> {
347347
let span = lo.with_hi(
348348
lo.lo() + BytePos(snapshot.unmatched_angle_bracket_count)
349349
);
350-
let plural = snapshot.unmatched_angle_bracket_count > 1;
351350
self.diagnostic()
352351
.struct_span_err(
353352
span,
354353
&format!(
355354
"unmatched angle bracket{}",
356-
if plural { "s" } else { "" }
355+
pluralise!(snapshot.unmatched_angle_bracket_count)
357356
),
358357
)
359358
.span_suggestion(
360359
span,
361360
&format!(
362361
"remove extra angle bracket{}",
363-
if plural { "s" } else { "" }
362+
pluralise!(snapshot.unmatched_angle_bracket_count)
364363
),
365364
String::new(),
366365
Applicability::MachineApplicable,

src/libsyntax/parse/parser/ty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::symbol::{kw};
1111

1212
use rustc_target::spec::abi::Abi;
1313

14-
use errors::{Applicability};
14+
use errors::{Applicability, pluralise};
1515

1616
/// Returns `true` if `IDENT t` can start a type -- `IDENT::a::b`, `IDENT<u8, u8>`,
1717
/// `IDENT<<u8 as Trait>::AssocTy>`.
@@ -397,7 +397,7 @@ impl<'a> Parser<'a> {
397397
}
398398

399399
if !negative_bounds.is_empty() || was_negative {
400-
let plural = negative_bounds.len() > 1;
400+
let negative_bounds_len = negative_bounds.len();
401401
let last_span = negative_bounds.last().map(|sp| *sp);
402402
let mut err = self.struct_span_err(
403403
negative_bounds,
@@ -420,7 +420,7 @@ impl<'a> Parser<'a> {
420420
}
421421
err.span_suggestion_hidden(
422422
bound_list,
423-
&format!("remove the trait bound{}", if plural { "s" } else { "" }),
423+
&format!("remove the trait bound{}", pluralise!(negative_bounds_len)),
424424
new_bound_list,
425425
Applicability::MachineApplicable,
426426
);

0 commit comments

Comments
 (0)