Skip to content

Commit 18717fc

Browse files
committed
Correct plural of arguments in format_args!
1 parent 5ddc7b4 commit 18717fc

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/libsyntax/ext/format.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,21 @@ impl<'a, 'b> Context<'a, 'b> {
215215
}
216216
}
217217

218+
fn describe_num_args(&self) -> String {
219+
if self.args.len() == 1 {
220+
"there is 1 argument".to_string()
221+
} else {
222+
format!("there are {} arguments", self.args.len())
223+
}
224+
}
225+
218226
fn verify_arg_type(&mut self, arg: Position, ty: ArgumentType) {
219227
match arg {
220228
Exact(arg) => {
221229
if self.args.len() <= arg {
222-
let msg = format!("invalid reference to argument `{}` (there \
223-
are {} arguments)", arg, self.args.len());
230+
let msg = format!("invalid reference to argument `{}` ({:s})",
231+
arg, self.describe_num_args());
232+
224233
self.ecx.span_err(self.fmtsp, msg.as_slice());
225234
return;
226235
}

src/test/compile-fail/ifmt-bad-arg.rs

+11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ fn main() {
2929
format!("{foo}", foo=1, foo=2); //~ ERROR: duplicate argument
3030
format!("", foo=1, 2); //~ ERROR: positional arguments cannot follow
3131

32+
// bad number of arguments, see #15780
33+
34+
format!("{0}");
35+
//^~ ERROR invalid reference to argument `0` (there are 0 arguments)
36+
37+
format!("{0} {1}", 1);
38+
//^~ ERROR invalid reference to argument `1` (there is 1 argument)
39+
40+
format!("{0} {1} {2}", 1, 2);
41+
//^~ ERROR invalid reference to argument `2` (there are 2 arguments)
42+
3243
// bad syntax of the format string
3344

3445
format!("{"); //~ ERROR: expected `}` but string was terminated

0 commit comments

Comments
 (0)